Common Legend for Clustered bar Graphs Using ggplot 2 in R -


i attempting add legend 2 clustered bar graphs using ggplot2 , gridextra packages. both graphs have same legend, time (pre-post).

i can 2 graphs same size sitting next each other, if leave out legend.position = "none" argument on right graph, legend included contracts graph. can suggest method of including legend right of right graph?

cell means , column names first graph follows

graph 1     time groupdc1c0         cwsqtot 1   pre  high expectancy    43.54545 2   post high expectancy    28.81818 3   pre  low expectancy     43.31111 4   post low expectancy     36.55556  graph 2     time  groupdc1c0        cwsqcrav 1   pre   high expectancy   4.977273 2   post  high expectancy   1.659091 3   pre   low expectancy    4.955556 4   post  low expectancy    3.688889 

the code 2 graphs in ggplot2 follows

    require(ggplot2) clusteredbarstot <- ggplot(cellmeanstot, aes(groupdc1c0,cwsqtot)) +    geom_bar(aes(fill = time), stat = "identity", position = "dodge", size = .5) +   scale_fill_manual(values = c("#999999", "#666666")) +   expand_limits(y = 50) +   xlab("") + ylab("cwsq score") +   theme_bw()   clusteredbarscrav <- ggplot(cellmeanscrav, aes(groupdc1c0,cwsqcrav)) +    geom_bar(aes(fill = time), stat = "identity", position = "dodge", size = .5) +   scale_fill_manual(values = c("#999999", "#666666")) +   expand_limits(y = 6) +   xlab("") + ylab("cwsq craving score") +   theme_bw()   titlefont <- element_text(face = "bold", color = "black", size = 16, vjust = 1.5) titlefontx <- element_text(face = "bold", color = "black", size = 16, vjust = 0.01) axistextfont <- element_text(face = "plain", color = "black", size = 13) # y-axis numbers size axistextfontx <- element_text(face = "plain", color = "black", size = 11, angle = 45, hjust = 1, vjust = 1) #x-axis day labels legendtitle <- element_text(face = "bold", color = "black", size = 14) legendtext <- element_text(face = "plain", color = "black", size = 13)   # add fix-ups graph (fonts, angles on axis labels etc.) via theme argument clusbartot <- clusteredbarstot + theme(title = titlefont,                        axis.title = titlefont,                       axis.title.x = titlefontx,                       axis.text  = axistextfont,                       axis.text.x = axistextfontx,                       legend.position = "none",                       panel.grid.minor = element_blank(),                        panel.grid.major = element_blank())    clusbarcrav <- clusteredbarscrav + theme(title = titlefont,                                         axis.title = titlefont,                                        axis.title.x = titlefontx,                                        axis.text  = axistextfont,                                        axis.text.x = axistextfontx,                                        legend.position = "none",                                        panel.grid.minor = element_blank(),                                        panel.grid.major = element_blank())   require(gridextra)  #####side side bar graphs grid.arrange(clusbartot, clusbarcrav, ncol=2)  ##### draw lines on totals graph grid.lines(c(0.205, 0.365), c(0.87, 0.87)) grid.lines(c(0.205, 0.205), c(0.87, 0.855)) grid.lines(c(0.365, 0.365), c(0.87, 0.855)) ####### draws label on total graph grid.text(expression(italic(p)==".006"),           x= unit(0.29, "npc"), y = unit(0.9, "npc"),            gp=gpar(fontsize = 11))  ##### draw lines on craving graph grid.lines(c(0.695, 0.861), c(0.845, 0.845)) grid.lines(c(0.695, 0.695), c(0.845, 0.83)) grid.lines(c(0.861, 0.861), c(0.845, 0.83)) ####### draws label on craving graph grid.text(expression(italic(p)~"< .001"), x= unit(0.785, "npc"), y = unit(0.875, "npc"),           gp=gpar(fontsize = 11)) 

i saw on stackexchange post had written code method of laying out graphs common legend. however, these graphs appear in 1 column legend in separate column. tried playing around code , entering in 2 graphs in function don't understand r enough reverse engineer code that. code follows:

grid_arrange_shared_legend <- function(...) {   plots <- list(...)   g <- ggplotgrob(plots[[1]] + theme(legend.position="bottom"))$grobs   legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]   lheight <- sum(legend$height)   grid.arrange(     do.call(arrangegrob, lapply(plots, function(x)       x + theme(legend.position="none"))),     legend,     ncol = 2,     heights = unit.c(unit(1, "npc") - lheight, lheight)) }  grid_arrange_shared_legend(clusbartot, clusbarcrav) 

as can tell naff comments in code still pretty new r , programming.

you on right way one. if want change position of legend bottom (as requested in question refer to) right change code in way legend position should right (third line of code) , want have 3 columns, new code should follow:

grid_arrange_shared_legend <- function(...) {   plots <- list(...)   g <- ggplotgrob(plots[[1]] + theme(legend.position="right"))$grobs   legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]   lheight <- sum(legend$height)   grid.arrange(     do.call(arrangegrob, lapply(plots, function(x)       x + theme(legend.position="none"))),     legend,     ncol = 3,     heights = unit.c(unit(1, "npc") - lheight, lheight)) }  grid_arrange_shared_legend(clusbartot, clusbarcrav) 

Comments