1

I'm trying to remove the legend of lines, but I don't know why it can't remove, and I only want to keep the points of "supp".

library(ggpubr)    
a<-ggline(ToothGrowth, x = "dose", y = "len", 
           add = c("mean_se", "jitter"),
           color = "supp", palette = "jco")

b<-ggline(ToothGrowth, x = "dose", y = "len", 
           add = c("mean_se", "jitter"),
           color = "supp", palette = "jco")

ggarrange(plot_grid(a + theme(legend.position="none")), b,common.legend = TRUE, labels = c("A", "B"),legend = "right")

2 Answers2

2

You can use following code

library(ggpubr)    
ggline(ToothGrowth, x = "dose", y = "len", 
       add = c("mean_se", "jitter"),
       color = "supp", palette = "jco")+ 
  theme(legend.position="none")
UseR10085
  • 7,120
  • 3
  • 24
  • 54
2

If you want to remove the lines from the legend and keep the points, you could use guides and override the linetype like this:

library(ggpubr)    
#> Loading required package: ggplot2
ggline(ToothGrowth, x = "dose", y = "len", 
       add = c("mean_se", "jitter"),
       color = "supp", palette = "jco") +
  guides(color = guide_legend(override.aes = list(linetype = 0)))

Created on 2022-11-25 with reprex v2.0.2


Code to comment:

library(ggpubr)  
library(cowplot)

a<-ggline(ToothGrowth, x = "dose", y = "len", 
          add = c("mean_se", "jitter"),
          color = "supp", palette = "jco")

b<-ggline(ToothGrowth, x = "dose", y = "len", 
          add = c("mean_se", "jitter"),
          color = "supp", palette = "jco")

ggarrange(plot_grid(a + theme(legend.position="none")), 
          b + guides(color = guide_legend(override.aes = list(linetype = 0))), 
          common.legend = TRUE, 
          labels = c("A", "B"),
          legend = "right")

Created on 2022-11-25 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • ggarrange(plot_grid(albc + theme(legend.position="none")), albs,common.legend = TRUE, labels = c("A", "B"),legend = "right") when I want to use "ggrange" but it dosen't work – Enting Chang Nov 25 '22 at 09:39
  • Hi @EntingChang, Check the code above in my answer. You should add the `guides` part to your second plot. – Quinten Nov 25 '22 at 09:46