1

I just need to put two different curves (one GLM segmented regression and a normal GLM of the same data) into a single panel (one graph, not different facets). I have been able only to place the two graphs next to each other (below), but what I need is to include the blue curve into the other graph. Any thoughts on how to get around this issue? Hopefully, this might be a very straightforward question for someone with experience in ggplot2.

Two graphs side by side

The closest I got to merge these two curves was by converting some data into a data.frame as recommended in some post (using library(reshape2, Plot two graphs in same plot in R), and I managed to get this (below). But it has been difficult to plot the Conf. Intervals for the GLM (green line here) and the points, so I wonder if there is an easier path to join the above curves (I scaled y axis).

I have also tried some other suggestions found online such as just adding the two graphs using add= TRUE and so on. Thanks in advance.

Both lines in the same plot, but no confidence intervals

Further information:
Thanks much for responding. The data consist of a gradient of oil palm proportion (x.oilpalm) in a landscape (the explanatory variable) and the response is species richness (spp.obs_NP).

head(dati)     

x y
1 50.155835 8
2 34.648817 10
3 80.927821 11
4 53.352809 8
5 1.303226 10
6 31.818881 9

So for the GLM plot (left-hand side). I used this code:

p1 <- ggplot(newdata, aes(x=x.oilpalm, y=fit)) +
geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = .25) +   
geom_line(size = 1) +
geom_point(data = vars, aes(y = spp.obs_NP, x = x.oilpalm)) +
labs(x = "Percentage of oil palm coverage", y = "Mammalian species richness")  
+
theme_bw()    
print(p1)

p2 <- p1 + theme(axis.text.x = element_text(color="black", size = 12),
             axis.text.y = element_text(color="black", size = 12),
             axis.title.y = element_text(size = 13),
             axis.title.x = element_text(size =13))
p2

for figure in the right side (segmented GLM regression)

p6 <- ggplot(dati, aes(x = x, y = y)) +
  geom_point() +
  geom_line(data = dat22, color = 'blue') +
  ylab("Mammalian species richness") +
  xlab("Percentage of oil palm coverage")+theme_bw() 

p7 <- p6 + theme(axis.text.x = element_text(color="black", size = 12),
             axis.text.y = element_text(color="black", size = 12),
             axis.title.y = element_text(size = 13),
             axis.title.x = element_text(size =13))
p7

putting together the two graph side by side

first I rescale p7 so the Y axis is also 16 maximum

  p8a <- ggplot(dati, aes(x = x, y = y, ymax= y+ 3.5)) + 
  geom_point() +
  geom_line(data = dat22, color = 'blue',linetype ="dashed", lwd = 1) +
  ylab("Mammalian species richness") +
  xlab("Percentage of oil palm coverage")+theme_bw() 
  p9a <- p8a + theme(axis.text.x = element_text(color="black", size = 12),
               axis.text.y = element_text(color="black", size = 12),
               axis.title.y = element_text(size = 13),
               axis.title.x = element_text(size =13))
  p9a  

then I just used gridExtra to put the two graphs together

library(gridExtra)
 aa <- grid.arrange( p2,  p9a, ncol=2)
 p22 <- aa + scale_y_continuous()

*please don't worry about the legends, I can make it tidier if I was going to use this style. But I need just one figure. This is, overlapping that blue curve (right) on the left panel.

SOLUTION (see comments)

enter image description here enter image description here

Community
  • 1
  • 1
lep
  • 67
  • 1
  • 1
  • 6
  • sorry I just noted, I cannot post figures, hope I made myself clear. I just need to put two fitted curves into a single figure. Thanks – lep Jul 13 '18 at 06:29
  • 3
    Please create some simple example data as suggested [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), and show the code you're using to achieve your current results (I think particularly the first example image). – Marius Jul 13 '18 at 06:39
  • 4
    As it is, this looks like an easy task. But if you don't post any data it will be nearly impossible to help. – erc Jul 13 '18 at 07:40
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Axeman Jul 13 '18 at 09:47
  • Thank you. I have included more information. – lep Jul 14 '18 at 02:13
  • You're already feeding different data to each `geom_line()`. Just put everything together using the element arithmetic: `ggplot(...) + geom_point() + geom_line( data = vars, aes(y = spp.obs_NP, x = x.oilpalm) ) + geom_line( data = dat22, aes(x = x, y = y) ) + ...` – Artem Sokolov Jul 14 '18 at 02:18
  • Hi @ArtemSokolov, it worked really well. Thanks much. (I'll try to paste the result above in the question if it allow me) – lep Jul 14 '18 at 03:41

0 Answers0