2

I have three groups (Tissue) and two groups (Treatment) represented in a faceted boxplot. I would like to remove the legend for "Treatment", as it is already represented in the graph, and keep the legend for "Tissue".

box <- ggboxplot(subclusDF, 
                 x = 'Treatment', 
                 y = 'ATP4',
                 fill = "Tissue", 
                 color = 'Tissue', 
                 palette = qualitative_hcl(3, palette = 'Dark 3'),
                 add = 'jitter', 
                 shape = 'Treatment')
box <- box + 
    labs(title= 'GHRL') + 
    xlab(NULL) + 
    ylab("Expression") + 
    facet_grid(~Tissue)
box

box + 
  theme() +
  theme(
    plot.title = element_text(face = "bold", size = 12),
    legend.background = element_rect(fill = "white", size = 4, colour = "white"),
    legend.justification = c(0, 1),
    legend.title=element_text(NULL),
    legend.key = element_blank(),
    legend.position = c(0, 1),
    axis.text = (NULL),
    axis.ticks = element_line(colour = "grey70", size = 0.2),
    panel.grid.major = element_line(colour = "grey70", size = 0.2),
    panel.grid.minor = element_blank()
  )
box
E. Nygaard
  • 104
  • 6
Marion
  • 31
  • 1
  • 3
  • It would be good to include your code so we can give you specific feedback. Edit: check the [cookbook](http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#removing-the-legend). – o_v Dec 22 '21 at 13:21
  • Thanks for that, I want to specially remove one legend and not the other, so not sure the cookbook handles that, but will look again – Marion Dec 22 '21 at 14:53
  • 1
    You can try `+ scale_shape_discrete(guide = "none")`, but this is an untested guess because I cannot reproduce the issue. Or `+ guides(shape = "none")`. – teunbrand Dec 22 '21 at 15:53

1 Answers1

1

So, specifically you'd want to remove the legend for shape:


box + 
guides(shape = "none")

o_v
  • 112
  • 8
  • 1
    Just wanted to let you know, this code gives the following error: The `` argument of `guides()` cannot be `FALSE`. Use "none" instead as of ggplot2 3.3.4. Instead this works: guides(shape = "none") – SamuelR Mar 11 '23 at 08:01