ggplot2:合并两个传说

时间:2017-05-07 19:05:44

标签: r ggplot2 legend

我试图用ggplot2绘制一个有两组不同点的区域,但我总是得到两个不同的传说。我读过thisthis,但我还有两个传说。 代码和图表下方。
非常感谢你

library(ggplot2)
library(dplyr)
set.seed(1)
df <- data.frame(x = letters,
                 y = 1:26 +runif(26),
                 z =  2*(1:26) +  runif(26),
                 jj = 1:26,
                 hh = 1:26*2,
                 x1 = 1:26)

some_names <- df %>% 
  filter(row_number() %% 10 == 1) %>% 
  select(x,x1)

p <- df %>% 
  ggplot(aes(x1)) +
  geom_ribbon(aes(ymin = y, ymax = z, fill = "area")) +
  geom_point(aes(y = jj, colour = "points1")) + 
  geom_point(aes(y = hh, colour = "points2")) +
  scale_x_continuous(breaks = some_names %>% select(x1) %>% unlist %>% unname,
                     labels =  some_names %>% select(x) %>% unlist %>% unname )

p + scale_fill_manual(name = "legend",
                      values = c("area" = "red","points1" = NA,"points2" = NA)) +
  scale_colour_manual(name = "legend",
                      values = c("area" = NA ,"points1" = "blue","points2" = "purple")) 

enter image description here

0 个答案:

没有答案