0

I would like to add two (same) legends in ggplot and also want to change legend title and labels. I have tried this:

library(ggplot2)
ggplot(ToothGrowth, aes(x = len, color=factor(dose), fill= factor(dose))) +
  geom_density(alpha=0.4) +
  theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3", size = 2, linetype = "solid"),
      panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"),
      panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white"),
      plot.background = element_rect(fill = "bisque2"),
      text = element_text(colour="blue4"), axis.title = element_text(size = rel(1.25)), axis.text = element_text(colour="blue4", size = 12),
      legend.position=c(.90,.85), legend.background = 
      element_rect(fill="lightsalmon", colour = "tomato3", size = 1.25),
      legend.title = element_text(colour="navy", face="bold"),
      legend.text = element_text( colour="midnightblue", face="bold"), strip.background = element_rect(fill="olivedrab1", colour = "darkorchid3", size = 2, linetype = "solid"), 
      strip.text = element_text(colour="coral4", size=12,  angle=0, face="bold")) +
   scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
   facet_wrap(~supp)

but I got this plot: enter image description here

I want this plot: enter image description here

Can somebody help me? Thank you.

Baltazár Tivadar
  • 189
  • 1
  • 2
  • 10
  • what about [this?](https://stackoverflow.com/questions/14840542/place-a-legend-for-each-facet-wrap-grid-in-ggplot2) – erocoar Jan 07 '18 at 15:59

1 Answers1

1

As @erocoar and others have suggested, grid.arrange from gridExtra is useful here. Borrowing heavily from from the linked question:

library(gridExtra)
out <- by(data = ToothGrowth, INDICES = ToothGrowth$supp, FUN = function(m) {
    m <- droplevels(m)
    m <- ggplot(m, aes(x = len, fill= factor(dose)), color=factor(dose)) + 
    geom_density(alpha=0.4) +
    theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3", 
                                      size = 2, linetype = "solid"),
          panel.grid.major = element_line(size = 0.5, linetype = 'solid', 
                                      colour = "white"),
          panel.grid.minor = element_line(size = 0.25, linetype = 'solid', 
                                      colour = "white"),
          plot.background = element_rect(fill = "bisque2"),
          text = element_text(colour="blue4"), 
          axis.title = element_text(size = rel(1.25)), 
          axis.text = element_text(colour="blue4", size = 12),
          legend.position=c(.90,.85), 
          legend.background = element_rect(fill="lightsalmon",
                              colour = "tomato3", size = 1.25),
          legend.title = element_text(colour="navy", face="bold"),
          legend.text = element_text( colour="midnightblue", face="bold"), 
          strip.background = element_rect(fill="olivedrab1", 
                                      colour = "darkorchid3", size = 2, 
                                      linetype = "solid"), 
          strip.text = element_text(colour="coral4", size=12,  angle=0, 
                                face="bold")) +
   scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
   xlim(0,35) +
   ylim(0,0.2) +
   ggtitle(m$supp)
})
do.call(grid.arrange, list(grobs = out, ncol = 2))

Some things to note.

  • I moved the color argument outside of the aes() call and this removed the extra legend.
  • I manually set the x and y limits for a consistent look.
  • I needed to add a title.
  • To get the plots side by side I had to add a second argument to do.call(). When supplying more than one argument it needs to be in a list.

I hope this helps.

Bart
  • 136
  • 1
  • 8