0

I cant figure out how to place two correlation lines for each data.frame in R. Code looks like:

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot

I have also tried

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot + geom_smooth(method='lm')

And

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_smooth(method='lm') +
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) + 
geom_smooth(method='lm') +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot 

Both options just print the graph without the lines, any suggestions?

  • By correlation line I believe you are asking for the least-squares regression line. You can plot two variables and notice their relative correlation, but there is no such thing as a _correlation line_. – OTStats Feb 28 '19 at 18:00

2 Answers2

0

You need to provide the data and aesthetics to the geom_smooth() function, for instance:

+ geom_smooth(
  data = data.frame1,
  aes(x = variable1, y = variable2, color = "red"),
  method = "lm"
)
user2363777
  • 947
  • 8
  • 18
0

If you want to combine the two plots, it's better to combine the two data.frames before, rbinding them together after defining a new variable that tells what data.frame the data came from.

Something like the following.

library(ggplot2)

data.frame1$group <- "df1"
data.frame2$group <- "df2"
df_all <- rbind(data.frame1[, c("variable1", "variable2", "group")],
                data.frame2[, c("variable1", "variable2", "group")])


combinedplot <- ggplot(df_all, aes(x = variable1, y = variable2, colour = group)) +
  geom_point() + 
  labs(x = "Date", y = "PM 2.5 ugm3") +
  scale_color_manual(values = c('red', 'blue'))

combinedplot + geom_smooth(method = 'lm')

enter image description here

Data creation code.

set.seed(1234)    # Make the results reproducible
data.frame1 <- data.frame(variable1 = 1:20, variable2 = 2*(1:20) + rnorm(20) )
data.frame2 <- data.frame(variable1 = 1:25, variable2 = 0.75*(1:25) + rnorm(25) )
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66