0

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!

Tori
  • 1
  • Can you post the code you've tried and sample data? Please edit the question with the code you ran (no *"etc..."*, please) and the output of `dput(My_Data_1)`. Or, if it is too big with the output of `dput(head(My_Data_1, 20))`. And the same for `My_Data_2`. – Rui Barradas Feb 07 '23 at 12:56
  • 1
    Try something like: `ggplot() + geom_point(data = My_Data_1, aes(x=X, y=Y)) + geom_point(data = My_Data_2, aes(x=X, y=Y))` – Stewart Macdonald Feb 07 '23 at 12:57

1 Answers1

0

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")
Scasto
  • 34
  • 3