Scaling the color, shape and line type of multiple ggplot2 geometers

A partial solution to my problem is found here . I will show how this solution works and what features are still not enough to meet my needs. For consistency, I use a sample data that is similar to how it is done in another thread:

library(ggplot2)
library(reshape2)
library(tidyverse)

df <- data.frame(x = c(1:5), a = c(1,2,3,3,3), b = c(1,1.1,1.3,1.5,1.5))
df <- mutate(df, log2 = log2(x))
df <- df <- melt(df, c("x", "log2"))

The initial schedule data (created with the following code) has aesthetics shapeand colorin some legends that is undesirable:

ggplot(df) +
  geom_point(aes(x, value, colour = variable, shape = variable), size = 3) +
  geom_line(aes(x, log2, color = "log2(x)"), size = 1.5)

The solution suggests using guides()and override.aesto combine them into one legend, which, of course, is an improvement over the original plot:

ggplot(df) +
  geom_point(aes(x, value, colour = variable, shape = variable), size = 3) +
  geom_line(aes(x, log2, color = "log2(x)"), size = 1.5) +
  guides(shape = FALSE,
         colour = guide_legend(override.aes = list(shape = c(16, 17, NA),
                                                   linetype = c("blank", "blank", "solid"))))

, color geom_point, geom_line. , , -, - log2(x) -. , guides(), , , , :

ggplot(df) +
  geom_point(aes(x, value, shape = variable), color = "gray40", size = 3, data = filter(df, variable == "a")) +
  geom_point(aes(x, value, shape = variable), color = "gray70", size = 3, data = filter(df, variable == "b")) +
  geom_line(aes(x, log2, shape = "log2(x)"), color = "cadetblue2", size = 1.5) +
  guides(shape = guide_legend(override.aes = list(color = c("gray40", "gray70", "cadetblue2"),
                                                  shape = c(16, 17, NA),
                                                  linetype = c("blank", "blank", "solid"))))

shape geom_line ( ), , . , log2(x) . , . - , / , guides()?

, scale_color_manual(), scale_linetype_manual() scale_shape_manual() ( name, ). , 3 color, shape, linetype. , scale_xx_manual() ggplot , .

, .

+4
1

, . , , , .

ggplot(df) +
  geom_point(aes(x, value, colour = variable, shape = variable), size = 3) +
  geom_line(aes(x, log2, color = "log2(x)"), size = 1.5) +
  scale_color_manual(values = c("a" = "gray40", "b" = "gray70", "log2(x)" = "cadetblue2")) +
  guides(shape = FALSE,
         colour = guide_legend(override.aes = list(shape = c(16, 17, NA),
                                                   linetype = c("blank", "blank", "solid"))))

Edit:

, :

ggplot(df) +
  geom_point(aes(x, value, colour = variable, shape = variable, linetype = variable), size = 3) +
  geom_line(aes(x, log2, color = "log2(x)", linetype = "log2(x)", shape = "log2(x)"), size = 1.5) +
  scale_color_manual(values = c("a" = "gray40", "b" = "gray70", "log2(x)" = "cadetblue2")) +
  scale_shape_manual(values = c("a" = 16, "b" = 17, "log2(x)" = NA)) +
  scale_linetype_manual(values = c("a" = "blank", "b" = "blank", "log2(x)" = "solid"))

ggplot , , (1, 2), , "a", "b" "log2 (x)" . , .

+3

Source: https://habr.com/ru/post/1690040/


All Articles