Ggplot2: how to create the right legend after using scale_xx_manual

I have a plot with three different lines. I want to have dots on one of these lines too. I also want two lines without dots to be thicker than one without glasses. I managed to get the plot that I want, but I could not resist the legend.

library(ggplot2)

y <- c(1:10, 2:11, 3:12)
x <- c(1:10, 1:10, 1:10)
testnames <- c(rep('mod1', 10), rep('mod2', 10), rep('meas', 10))
df <- data.frame(testnames, y, x)

ggplot(data=df, aes(x=x, y=y, colour=testnames)) +
   geom_line(aes(size=testnames)) +
   scale_size_manual("", values=c(0.5,1,1)) +
   geom_point(aes(alpha=testnames), size=5, shape=4) +
   scale_alpha_manual("", values=c(1, 0, 0))

enter image description here

I can remove the second (black) legend:

ggplot(data = df, aes(x=x, y=y, colour=testnames)) +
   geom_line(aes(size=testnames)) +
   scale_size_manual("", values=c(0.5,1,1), guide='none') +
   geom_point(aes(alpha=testnames), size=5, shape=4) +
   scale_alpha_manual("", values=c(1, 0.05, 0.05), guide='none')

enter image description here

But I really want the merger of two legends - The Legend of flowers, intersecting only the first variable ( meas), and the line mod1and mod2the thickness of the first row. I tried the manual and redefined, but with little luck.

+4
source share
1 answer

, mod1 mod2. , NA scale_shape_manual:

ggplot(data = df, aes(x = x, y = y, colour = testnames, size = testnames)) +
  geom_line() +
  geom_point(aes(shape = testnames), size = 5) +
  scale_size_manual(values=c(0.5, 2, 2)) +
  scale_shape_manual(values=c(8, NA, NA)) 

:

enter image description here


. , .

+7

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


All Articles