Remove legend elements of one specific geom: “show.legend = FALSE” does not do the job

前端 未结 2 1926
醉梦人生
醉梦人生 2021-01-19 07:32

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.

2条回答
  •  终归单人心
    2021-01-19 08:11

    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
    

提交回复
热议问题