1
votes

I have seen posted regarding common legends in ggplot2. However, they all place the common legend at the bottom. I would like to change the position of the common legend to the top. My current code is below. In addition, I would like to export the resultant plot as a PNG file. Thank you in advance

#create data frame

N=3
pacsum<-data.frame(Subject=rep(1:N,each=9),Time=rep(0:8,N), Conc = rep(10:18,N))
pacsum$Place<-ifelse(pacsum$Subject==1,"High",
                 ifelse(pacsum$Subject==2,"Low","Middle"))

#create plots

g1<-ggplot(subset(pacsum, Subject == 1), aes(x=Time, y = Conc),group=interaction(Place))+
  geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
  xlab("Time")+ylab("Conc")+
  ggtitle("Graph 1")+
  theme(legend.position="top")

g2<-ggplot(subset(pacsum, Subject == 2), aes(x=Time, y = Conc),group=interaction(Place))+
 geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
 xlab("Time")+ylab("Conc")+
 ggtitle("Graph 2")+
 theme(legend.position="top")

g3<-ggplot(subset(pacsum, Subject == 3), aes(x=Time, y = Conc),group=interaction(Place))+
geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
xlab("Time")+ylab("Conc")+
ggtitle("Graph 3")+
theme(legend.position="top")

#extract Legend

  extract_legend <- function(my_ggp) {
  step1 <- ggplot_gtable(ggplot_build(my_ggp))
  step2 <- which(sapply(step1$grobs, function(x) x$name) == "guide-box")
  step3 <- step1$grobs[[step2]]
  return(step3)
  }

#create shared legend
shared_legend <- extract_legend(g1)

#create plots with common legend
grid.arrange(arrangeGrob(g1 + theme(legend.position="none"), g2+ theme(legend.position="none"), g3+ 
theme(legend.position="none"), ncol = 2),
shared_legend, nrow = 2, heights = c(10, 0.5))

Common Legend on Bottom

1
Instead of subsetting your data like this, have you considered using facets?tjebo
The data frame for this example could be faceted but my actual graphs are separate graphstcek

1 Answers

1
votes

You could try something like this (Updated):

library(patchwork)
library(ggplot2)

N=3
pacsum<-data.frame(Subject=rep(1:N,each=9),Time=rep(0:8,N), Conc = rep(10:18,N))
pacsum$Place<-ifelse(pacsum$Subject==1,"High",
                     ifelse(pacsum$Subject==2,"Low","Middle"))

g1<-ggplot(subset(pacsum, Subject == 1), aes(x=Time, y = Conc),group=interaction(Place))+
  geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
  xlab("Time")+ylab("Conc")+
  ggtitle("Graph 1")+
  theme(legend.position="none")

g2<-ggplot(subset(pacsum, Subject == 2), aes(x=Time, y = Conc),group=interaction(Place))+
  geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
  xlab("Time")+ylab("Conc")+
  ggtitle("Graph 2")+
  theme(plot.margin = margin(t=3,1,1,1, "lines"),
        legend.direction = "horizontal",
        legend.position = c(0.5, 1.1))

g3<-ggplot(subset(pacsum, Subject == 3), aes(x=Time, y = Conc),group=interaction(Place))+
  geom_point(aes(colour=factor(Place), fill=factor(Place),shape=factor(Place)),size=2.75)+
  xlab("Time")+ylab("Conc")+
  ggtitle("Graph 3")+
  theme(legend.position="none")

g <- g1+g2+g3
ggsave('Plot.png',plot = g)

enter image description here