Merging two plots into one, each with a separate legend using R

757 views Asked by At

I'm have made two separate scatter plots using ggplot2 and I need to combine them into one single plot. Each plot is for a population of lizards under three different treatments (backgrounds). for each plot I have the following:

csMS = data.frame()
ellMS = data.frame()
centroidsMS = data.frame()  

csplotMS = ggplot(csMS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3, shape = 17) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 3, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
        axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Murray Sunset NP") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.5, .85))

I tried

csASMS = csplotAS + csplotMS

but I get an error message: "Error in p + o : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("+.gg", "Ops.data.frame") for "+" "

I also tried

csASMS = grid.arrange(csplotAS, csplotMS)

but this places one plot on top of the other, but I need to combine both plots so that they are basically just one plot but with two separate legends as each plot has different conventions to indicate the different lizard populations.

Any help will be greatly appreciated.

****EDIT**** Dec 12/ 2014

I have managed to combine the two plots into one but still have the problem of the separate legends. To try to simplify the question and as per cdeterman's request I'm adding a simpler form of the code with some sample data:

data frames: p1 and p2

> p1
  treatment x y
1     Black 1 1
2    Orange 2 2
3    Yellow 3 3

> p2
  treatment x y
1    Black  4 4
2    Orange 5 5
3    Yellow 6 6

I used the following code to make a plot that includes both data frames:

plot = ggplot(p1, aes(x = x, y =  y, colour = treatment)) +     geom_point(size = 3) + #colour by background, circles size 3
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "p1") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the     legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = p2, aes(x = x, y = y, colour = treatment), size = 3, shape = 17)

This produces a graph with each data frame represented in a different symbol (circles for p1 and triangles for p2) but with only one combined legend with triangles superimposed over circles). How can I get two separate legends, one for each data frame?

Thank you!

1

There are 1 answers

0
user3713629 On

After doing some research and trying different things I was able to solve PART of my problem. To add two plots together one needs to be plotter first and the other one on top of the first one using

geom.point()

my new code looks like this:

csplotASMS = ggplot(csAS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 1) + #adding the ellipses
  geom_point(data = centroidsAS, size = 4) + #added centroids     
  geom_errorbar(data = centroidsAS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsAS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Alice Springs") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = csMS, aes(x = RG, y = GB, colour = Background), size = 3, shape = 17) +
  geom_path(data = ell.MS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 4, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) #add x error bars

and the graph depicts a scatterplot for two populations, each with three treatments. Because tratments are the same for both populations I want to use the same colours but different symbols to denote the differences in populations. One population is circles and the other one is triangles.

Now, the part I can't answer yet is how to have two separate legends, one for each "plot". i.e. one for the circles and one for the triangles. At the moments there is a "combined legend showing triangles superimposed on circles. Each legend should have its own title.