Reputation: 1
I'm very new to R and I have to deal with quite big datasets from a previous work. During these previous studies, the person worked on excel and I have to adapt everything on R.
Especially, I did 2 simple linear regressions. To simplify, the first one represents Y as a function of X from one dataframe, let's call it My_Data_1, and the second one is Y' as a function of X, with Y' a variable in the dataframe My_Data_2. In other words, X,Y and Y' come from different dataframes (among many other variables).
I'd like to compare the two regressions by plotting them on a single graph using ggplot2.
However, I don't know how to procede because the dataframe import is done in the ggplot, such as: ggplot(data = My_Data_1, aes(x=X, y=Y)) + geom_point() + etc...
I tried to put only x in the ggplot() and to put Y and Y' in geom_point() but it doesn't solve anything: Y' is unknown in this case because only one dataframe is imported in ggplot. I didn't find solution. One would be to create a new table but I'd like to know if there is another way to do so.
I hope it was clear enough... Thanks by advance for your help!
Upvotes: 0
Views: 771
Reputation: 32
You can use multiple layers inside one plot. For example you could use two different geom_smooth()
layers. Each layer can be build from different data.
For example your first layer could look like this:
geom_smooth(data= df1, aes(x=x, y=y),method ="lm", se = FALSE, color = "blue")
And your second layer could look like this:
geom_smooth(data= df2, aes(x=x, y=y),method ="lm", se = FALSE, color = "red")
Same goes for your geom_point()
layer.
In the end you can piece it together with +
.
ggplot()+
geom_smooth(data= df1, aes(x=x, y=y),method ="lm", se = FALSE, color = "blue") +
geom_smooth(data= df2, aes(x=x, y=y),method ="lm", se = FALSE, color = "red") +
labs(title= "Two Regression Lines", x = "x Value", y = "Y Value")
Upvotes: 0