Home > Net >  ggplot line and point legend separately
ggplot line and point legend separately

Time:08-26

I tried searching for similar stackoverflows, but unable to find a solution.

I would like the line in the legend to not have dots, and the dot in the legend to not have a line.

Other solutions put guide with linetype and shape seems to work, but it doesn't work if I have different aes for different geom_lines it seems.

library(ggplot2)
my_shapes <- c('line1'=NA, 'line2'=NA, 'line3'=1)
scalar <- 11
ggplot(plotdt)   geom_bar(aes(x=Month, y=DataBar, fill=NAME), stat="identity")  
    geom_line(aes(x=Month, y=Data2/scalar, color="line1", group="1"), lwd=1.3)  
    geom_line(aes(x=Month, y=Data3/scalar, color="line2", group="2"), lwd=1.3) 
    geom_point(aes(x=Month, y=Data, color="line3", group="3"), size=3)  
    geom_text(aes(x     = Month,
                  y     = Data   vjust_ * sign(Data) 1,
                  label = round(Data, 1),
                  hjust = 0.5, fontface = "bold"), position=position_dodge(width=0.9))  
    scale_y_continuous(name = "Y Pri Axis", sec.axis = sec_axis(~.*scalar, name="Y Sec axis"))  
    ggplot2::labs(title = paste0("My Title"), x = "", y = "") 
    scale_fill_manual(name="Monthly Bar", values=c('orange','lightblue')) 
    scale_colour_manual(name = 'Lines',
                        values =c('line1'="green",'line2'="red", 'line3'="maroon"),
                        guide = guide_legend(override.aes = list(linetype=c('line1'="solid",'line2'="solid",'line3'="blank"),
                                                                 shape = my_shapes)),
                        labels = c(paste0("Cumulative FY",c("2021/22", "2022/23")),"Net Demand"))  
    scale_shape_manual(values = my_shapes)  
    guides(fill=guide_legend(ncol=1), color=guide_legend(nrow=3, byrow=TRUE)) 
    theme_bw()   theme(legend.title=element_blank(), legend.background = element_blank(), legend.position = "bottom")
dput(plotdt)
structure(list(Month = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), .Label = c("Apr", "May", 
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", 
"Mar"), class = "factor"), Data = c(21.51244, 21.51244, 11.77015, 
11.77015, 5.8888, 5.8888, 8.457395, 8.457395, NA, NA, NA, NA, 
NA, NA, NA, NA), Data2 = c(22.6712707042253, 22.6712707042253, 
41.7899507042253, 41.7899507042253, 64.1680157042254, 64.1680157042254, 
80.7971357042254, 80.7971357042254, 104.767484788732, 129.69382028169, 
158.774732112676, 196.205067112676, 210.943507112676, 225.626027112676, 
253.841026112676, 273.224921112676), Data3 = c(21.51244, 21.51244, 
33.28259, 33.28259, 39.17139, 39.17139, 47.628785, 47.628785, 
NA, NA, NA, NA, NA, NA, NA, NA), vjust_ = c(0.781182, 0.781182, 
0.781182, 0.781182, 0.781182, 0.781182, 0.781182, 0.781182, 0.781182, 
0.781182, 0.781182, 0.781182, 0.781182, 0.781182, 0.781182, 0.781182
), NAME = c("DataNAME1", "DataNAME2", "DataNAME1", "DataNAME2", 
"DataNAME1", "DataNAME2", "DataNAME1", "DataNAME2", NA, NA, NA, 
NA, NA, NA, NA, NA), DataBar = c(15.19244, 6.32, 16.04015, -4.27, 
11.6948, -5.806, 14.107395, -5.65, NA, NA, NA, NA, NA, NA, NA, 
NA)), class = c("data.table", "data.frame"), row.names = c(NA, 
-16L), sorted = "Month", index = integer(0))

ggplotimage

CodePudding user response:

One option would be to use the override.aes argument of guide_legend to remove the "dots" for the lines and the "line" for the point, i.e. add override.aes = list(shape = c(NA, NA, 16), linetype = c("solid", "solid", "blank")) to guide_legend:

library(ggplot2)
my_shapes <- c("line1" = NA, "line2" = NA, "line3" = 1)
scalar <- 11
ggplot(plotdt)  
  geom_bar(aes(x = Month, y = DataBar, fill = NAME), stat = "identity")  
  geom_line(aes(x = Month, y = Data2 / scalar, color = "line1", group = "1"), lwd = 1.3)  
  geom_line(aes(x = Month, y = Data3 / scalar, color = "line2", group = "2"), lwd = 1.3)  
  geom_point(aes(x = Month, y = Data, color = "line3", group = "3"), size = 3)  
  geom_text(aes(
    x = Month,
    y = Data   vjust_ * sign(Data)   1,
    label = round(Data, 1),
    hjust = 0.5, fontface = "bold"
  ), position = position_dodge(width = 0.9))  
  scale_y_continuous(name = "Y Pri Axis", sec.axis = sec_axis(~ . * scalar, name = "Y Sec axis"))  
  ggplot2::labs(title = paste0("My Title"), x = "", y = "")  
  scale_fill_manual(name = "Monthly Bar", values = c("orange", "lightblue"))  
  scale_colour_manual(
    name = "Lines",
    values = c("line1" = "green", "line2" = "red", "line3" = "maroon"),
    guide = guide_legend(override.aes = list(
      linetype = c("line1" = "solid", "line2" = "solid", "line3" = "blank"),
      shape = my_shapes
    )),
    labels = c(paste0("Cumulative FY", c("2021/22", "2022/23")), "Net Demand")
  )  
  scale_shape_manual(values = my_shapes)  
  guides(fill = guide_legend(ncol = 1), color = guide_legend(nrow = 3, byrow = TRUE, 
                                                             override.aes = list(shape = c(NA, NA, 16), linetype = c("solid", "solid", "blank"))))  
  theme_bw()  
  theme(legend.title = element_blank(), legend.background = element_blank(), legend.position = "bottom")

  • Related