Home > database >  ggplot2 unable to color legend icons
ggplot2 unable to color legend icons

Time:12-02

I'm trying to use ggplot2 to make some sort of timeline using values from a dataframe (df). I've managed to plot the data exactly how I want it (the different colored line segments connecting the x-marks in this exact order, i.e., from left to right: 'early', 'unknown', 'late', 'sub'). The startpoint and endpoint columns in the dataframe are used to define the positions of the points and line segments.

The problem is that the legend doesn't show the color of the 'x' icons, they are just grey. I've tried adding scale_color_manual() and scale_fill_manual() commands but they don't seem to change anything. The legend does display the correct color when I change the shape to shape = 21, however, I really want the shape to be 4 (x icons). I don't care about the shape of the legend though but scale_shape_manual() again didn't change anything about the legend. I have also tried placing different color arguments inside and outside the aes() argument of ggplot(), geom_segment() and/or geom_point().

How can I make the icons from the legend show the correct color?

Below I added a piece of code to reproduce the problem.

library(ggplot2)
library(RColorBrewer)

## Define dataframe
df <- data.frame(Var = c("sub","late","unknown","early"),
                 Time = c(10,267,0,1256),
                 Endpoint = c(1533,1523,1256,1256),
                 Startpoint = c(1523,1256,1256,0))
colorscheme <- RColorBrewer::brewer.pal(9, "Set1")[c(1,4,2,3)]

## Make plot
  ggplot(df, aes(x="", y=Endpoint, fill=Var), color =colorscheme)  
  geom_segment( aes(x="", xend="", y=Startpoint, yend=Endpoint), color = colorscheme)  
  geom_point(aes(x="", y=Endpoint),size=5, shape=4 , color = colorscheme)  
  coord_flip()

this plot is the output from the code above, the x-marks in the legend are just grey and not colored

Thanks in advance for any suggestions!

CodePudding user response:

Try this:

ggplot(df, aes(x = "", y = Endpoint, color = Var), colorscheme)  
  geom_segment(aes(x = "", xend = "", y = Startpoint, yend = Endpoint), show.legend = FALSE)  
  geom_point(aes(x = "", y = Endpoint), size = 5, shape = 4)  
  coord_flip()

In this way legend will show only X

CodePudding user response:

You should use color instead of fill. To remove the line from the legend, use guides(color = guide_legend(override.aes = list(linetype = 0))) or use show.legend = F in geom_segment.

Also, arguments passed in ggplot need not to be repeated afterward.

ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme)  
  geom_segment(aes(xend="", y=Startpoint, yend=Endpoint))  
  geom_point(size=5, shape=4)  
  coord_flip()  
  guides(color = guide_legend(override.aes = list(linetype = 0)))

 #or

ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme)  
  geom_segment(aes(xend="", y=Startpoint, yend=Endpoint))  
  geom_point(size=5, shape=4)  
  coord_flip()

enter image description here

  • Related