2

I posted a question about how to create a legend with the text the same colour as the lines in the chart. I have been able to get the legend text to have the same colour as lines but not do any other formatting to the legend. I would like to have the legend on the bottom, be bigger, and bold. Also, be able to add notes to the bottom of the plot. See example code below.

Note: I edited the function found here

require(grid); require(gridExtra); require(ggplot2)
gglabcol <- 
function(plot1, ColourList) 

     {
     g <- ggplotGrob(plot1)

     # legend grobs
     g.b <- g[["grobs"]][[which(g$layout$name=="guide-box")]]
     l <- g.b[["grobs"]][[1]][["grobs"]]

     # get grobs for legend symbols (extract colour)
     lg <- ColourList

     # get grobs for legend labels 
     lb <- l[sapply(l, function(i) grepl("guide.label", i))]

     # get change colour of labels to colour of symbols
     for(i in seq_along(lg)) {

       lb[[i]]$gp$col <-  lg[i]

       g.b[["grobs"]][[1]][["grobs"]][sapply(g.b[["grobs"]][[1]][["grobs"]],
                      function(i) grepl("guide.label", i))][[i]] <- lb[[i]]
       }

     # overwrite original legend
     g[["grobs"]][[which(g$layout$name=="guide-box")]] <- g.b

     grid.draw(g)

     invisible(g)
 }



df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42))

p1<-ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + 
geom_line()+
scale_color_manual("sex",values=c("green", "blue"))

ColourList<-c("green", "blue")
p1<-gglabcol(p1, ColourList)



p1<-p1+theme(legend.text = element_text(size = 14, face = 'bold'))+
        theme(legend.position="bottom",legend.title=element_text(size=14))

Footnote<-"Note: Shaded area represents one standard deviation from the mean."

p1<-arrangeGrob(p1, sub = textGrob(Footnote, x = 0, hjust = -0.1, vjust=0.1, gp = gpar(fontface = "italic", fontsize = 10)))
Community
  • 1
  • 1
user2946746
  • 1,740
  • 3
  • 21
  • 36
  • If there are more than one grob then using "[[" will fail at the `g[["grobs"]][[which(g$layout$name=="guide-box")]]` step – IRTFM Mar 26 '15 at 20:21
  • And where is object-'Footnote'? – IRTFM Mar 26 '15 at 20:22
  • Appears that your function trashes the plot-object: Before the call: `class(p1) #[1] "gg" "ggplot"`, ... After the call: `class(p1) #[1] "gtable" "grob" "gDesc"` – IRTFM Mar 26 '15 at 20:30
  • I'm not sure how to fix the function so it doesn't trash the plot object and returns `class(p1) #[1] "gg" "ggplot"`. I'm sure that's the problem with the code but i'm currently unsure how to fix it. – user2946746 Mar 26 '15 at 20:40
  • you should create the plot first, including changing the legend position etc, and then use the function to change the colours.. So move `theme(legend.text = element_text(size = 14, face = 'bold')) + theme(legend.position="bottom",legend.title=element_text(size=14))` onto the original plot `p1`, then use the function to change the colours - - works fine – user20650 Mar 27 '15 at 20:21

0 Answers0