Remove legend elements of one specific geom: “show

2019-07-12 23:08发布

I have written an answer here and would like to improve it. What I would like to do is to remove the legend for geom_path but it is not working with show.legend = FALSE. The color elements of geom_path remain in the legend (Down and Up). Am I doing something wrong?

Is there alternatively a way to manually tell ggplot just to show lets say the last two elements of the legend (y2015, y2016)?

My Code and the Output:

library(ggplot2)
library(reshape2)
library(dplyr)

ggplot2df <- read.table(text = "question y2015 y2016
                        q1 90 50
                        q2 80 60
                        q3 70 90
                        q4 90 60
                        q5 30 20", header = TRUE)


df <- ggplot2df %>% 
  mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down"))%>%
  melt(id = c("question", "direction"))


ggplot(df, aes(x=question, y = value, color = variable, group = question )) + 
geom_point(size=4) + 
geom_path(aes(color = direction), arrow=arrow(), show.legend = FALSE)  

enter image description here

标签: r ggplot2
2条回答
仙女界的扛把子
2楼-- · 2019-07-12 23:42

Another possibility (but more overall work) is to manually specify the colors for the arrows:

library(ggplot2)
library(tidyr)
library(dplyr)

ggplot2df <- read.table(text = "question y2015 y2016
q1 90 50
q2 80 60
q3 70 90
q4 90 60
q5 30 20", header = TRUE)

ggplot2df %>% 
  mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down")) %>%
  gather(variable, value, -question, -direction) -> df

gg <- ggplot(df, aes(x=question, y = value, group = question)) 
gg <- gg + geom_point(aes(color=variable), size=4) 
gg <- gg + geom_path(color=c("red", "red", "green", rep("red", 4), "green", "red", "red"),
                     arrow=arrow(), show.legend=FALSE)  
gg

enter image description here

查看更多
Melony?
3楼-- · 2019-07-12 23:46

I think what's going on is that because both variable and direction are mapped to color, the legend has four different color values. Removing the path legend just removes the arrows, while removing just the point legend removes the points. But either way, all four colors still show up in the legend, as points or arrows, respectively, because the underlying mapping still has four values, regardless of whether you choose to manifest those four values as points, arrows, or both in the legend.

One way around this would be to use a fill aesthetic for the points. Then the path legend will have only two values. To do this, you have to use a point style with a filled interior (pch values 21 - 25). You'll also need to change the colors of either the color aesthetic or fill aesthetic, so that they won't be the same:

ggplot(df, aes(x=question, y = value, group = question)) + 
  geom_point(size=4, aes(fill=variable), pch=21, color=NA) + 
  geom_path(aes(color = direction), arrow=arrow(), show.legend=FALSE) +
  scale_fill_manual(values=hcl(c(105,285), 100, 50))

enter image description here

查看更多
登录 后发表回答