0

I'm needing to plot two regressions on the same axes. For this I have 3 columns in my dataset (let's call them A, B & C). I want to plot B against A and then C against A, and have these as different colour regression lines, with the data points being the same colour as their corresponding lines.

To be more specific, to create individual plots I used the following code for the first one:

P1 <- ggplot(data=volboth, aes(x=control, y=vol30)) + 
geom_point(alpha=1, size=4, color="maroon") + 
ggtitle("Correlation Plot: Ground Survey (Control) vs 30m UAV Survey") + 
labs(x = expression(paste("Volume - Control Data - ", m^{3})), 
     y = expression(paste("Volume - Aerial Data - ", m^{3}))) + 
xlim(0, 5) + 
ylim(0, 5) + 
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

And then the following for the second plot:

P2 <- ggplot(data=volboth, aes(x=vol10, y=control)) + 
geom_point(alpha=1, size=4, color="maroon") +
 ggtitle("Correlation Plot: Ground Survey (Control) vs 10m UAV Survey") + 
labs(x = expression(paste("Volume - Aerial Data - ", m^{3})), 
     y = expression(paste("Volume - Control Data - ", m^{3}))) + 
xlim(0, 5) + 
ylim(0, 5) + 
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

Any ideas of how to combine both plots onto same axes, and to apply corresponding visual themes? I'm open to using standard R (not ggplot2) if that makes things easier.

Roland
  • 127,288
  • 10
  • 191
  • 288
Luke
  • 35
  • 1
  • 1
  • 4
  • Just as a first step towards answering my own question, I did the following, which added the two regression lines I need as well as the different colour points. What it doesn't do is give the regression lines the corresponding coour to its points: – Luke Oct 31 '15 at 14:37
  • ggplot(volboth) + geom_jitter(aes(control,vol10), colour="blue") + geom_smooth(aes(control,vol10), method=lm, se=FALSE) + geom_jitter(aes(control,vol30), colour="green") + geom_smooth(aes(control,vol30), method=lm, se=FALSE) + labs(x = "Actual", y = "Remote") – Luke Oct 31 '15 at 14:38
  • 3
    Standard advice for using ggplot2: `melt` your data.frame to have one column for x values, one for y values, and one column that maps your values to groups/colors. If you need more specific help you'd need to provide reproducible input data. – Roland Oct 31 '15 at 14:50
  • 1
    If you're unsure how to do what @Roland said, read this: [how to make a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Heroka Oct 31 '15 at 20:31

1 Answers1

0
require(ggplot2)
#first, some sample data
volboth <- data.frame(control=(0:100)/20,vol10=(50:150)/50,vol30=(120:20)/30)

#next, make a plot
P1 <- ggplot(data=volboth, aes(x=control, y=vol30)) + 
  geom_point(alpha=1, size=4, color="maroon") + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE) +
#Now add a second layer, with same x, but other y (and blue color for clarity)
  geom_point(aes(y=vol10),alpha=1, size=4, color="blue") + 
  geom_smooth(aes(y=vol10),method=lm, se=FALSE, fullrange=TRUE) +
  ggtitle("Correlation Plot: Ground Survey (Control) vs 30m UAV Survey") + 
  labs(x = expression(paste("Volume - Control Data - ", m^{3})), 
       y = expression(paste("Volume - Aerial Data - ", m^{3}))) + 
  xlim(0, 5) + 
  ylim(0, 5)

print(P1)

Which gives me this graph: enter image description here
I have used geom_point here, but as you have worked out yourself if your points are closely together, geom_jitter might be a better alternative.

RHA
  • 3,677
  • 4
  • 25
  • 48