Put two linear regression lines into one plot - r

I initially have two data frame("icelandma,swissfe") with observations from different countries and totally same variables. To compare the linear regression of intp.trust and confidence for these two countries. I combine these two data frame into one with this command:
merge1 <- rbind(icelandma, swissfe)
And then, I draw the linear regression plot with this command:
ggplot(data = merge1,aes(x=intp.trust,y=confidence))+
geom_point(size=0.5)+
geom_smooth(method = "lm",formula = y~x)+
facet_grid(countryname~.)
The plot is like this
The regression line is still in two plots, I'm wondering if there is any way to post these two lines in the same plot. Thanks for your help in advance!

Try
ggplot(data = merge1,aes(x=intp.trust,y=confidence, group = countryname))+
geom_point(size=0.5)+
geom_smooth(method = "lm",formula = y~x)
facet_wrap puts your plots in different panels by countryname~.
If you want to differentiate by countryname add color to your aes: aes(...,color = countryname).

Related

RStudio - Linear Regression with Groups

So I have 2 groups and an x and y variable. I am trying to run a linear regression to see if there is a significant relationship between the x and y variables within each group but I also want to look at the significance between groups. Then I would like to plot those results and provide a p-value, equation, and R^2 value on the graph. How would I go about accomplishing this?
I am able to plot the data on the same graph using this code:
ggplot(data_NeuroPsych, aes(x = Flanker_Ratio, y = Neuropsych_Delta, color = Group)) +
geom_point() +
geom_smooth(method = "lm", fill = NA)
Then using this open source code I was able to look at the results separately: https://github.com/kassambara/ggpubr/blob/master/R/stat_regline_equation.R#L7
The issue with the above is the data is not on the same plot and it does not look at the comparison between groups.

How to plot a polynomial over a scatterplot in R?

My intention is to plot a polynomial regression on a data
Lets say the x-axis is the third column of df that is df[,3] and the y-axis is fifth column df[,5]
I performed polynomial regression on this data and obtained a vector yreg which I want to plot over this scatter plot.
My question is how can we make it happen? All I have encountered so far are using built in regression models without explicitly defining the polynomial but in my case I have the regression polynomial in hand and I want to add it to the scatter plot of the x,y data
Here is how I plot the scatter plot using ggplot2
plt <- ggplot(g,aes(df[,3] , df[,5])) +
geom_point(color = "#69b3a2",size=0.5)
I tried the following:
plt <- ggplot(g,aes(df[,3] , df[,5])) +
geom_point(color = "#69b3a2",size=0.5) +
geom_smooth(formula = y~yreg,color="red")
But that did not work.

Using interplot to plot continuous*continuous interactions for multiple individuals

I want to produce a plot which demonstrates the effects of a continuous by continuous interaction, with one line for each individual in the dataset. I have managed to successfully plot the interaction at the population level using interplot:
m2<-lmer(RMR~No_Squares*Temperature+(Temperature|ID), data=female1)
interplot(m = m2, var1 = "No_Squares", var2 = "Temperature", ci=FALSE)
But I am at a loss as how to produce such a plot for each individual ID (i.e. to show the differences between individuals)
I have tried adding:
+ geom_smooth(aes(group = ID), method = "lm")
To the code, but this doesn't work.
Any ideas?

R: overlay multiple plots with same y axis ggplot2 (with datapoints and only geom_smooth lines)

Maybe someone knows a simple solution to this:
I have a R script that produces several plots with ggplot2 out of one dataframe. These plots on their own look somewhat like these 2 images:
The problem I have: This script generates about 20 plots, all are using different variables and values for the x-axies, and the same variable but different values for the y-axies.
In general the plots look like this:
plot.a <- ggplot(DF[which(DF$a>0&DF$a<200),], aes(x=a, y=myY))+
geom_point(shape=1, color=color2, alpha=alpha) +
labs(title='a vs y', x='a', y='y')+
geom_smooth(method = lm, se = FALSE, color = color)+ ..
plot.b <- ggplot(DF[which(DF$b>0),], aes(x=b, y=myY))+
geom_point(shape=1, color=color2, alpha=alpha) +
labs(title='b vs. y', x='b', y='y')+
geom_smooth(method = lm, se = FALSE, color = color)+ ..
As you can see the different plots use different columns of the same DF and also only relevant data (e.g. DF$b>0 on plot.b but on plot.a DF$a<200) so there are not all lines of data used in every plot.
Now I want to combine all these plots (about 20 plots) into one plot with the same y-axies but different x-axies. I'am mainly interested in the geom_smooth trend lines of all plots.
Is there a way to combine all these plots into a new ggplot (or only the geom_smooth lines), with the same y-axies and adding and displaying for every new smooth line a new x-axies?
For better reading of the new plot, is it possible to create a different color of every geom_smooth and the corresponding x-axies (e.g. color = plottype) with a legend?
Thanks in advance!

How to make ggplot2 run with 2 layers?

Here's my code:
tmp <- data.frame(t_year = rnorm(100,0,1),
labs = c(rep("Linear",50), rep("Spline",50)),
STUDY_PARTICIPANT_ID = rep(seq(1,50),2),
logpsa = rnorm(100,0.5,1),
mypredict = rnorm(100,1,2))
p <- ggplot(tmp) +
geom_line(aes(t_year,
mypredict,
group = as.factor(labs),
color = as.factor(labs))) +
geom_line(aes(t_year,
logpsa,
group = STUDY_PARTICIPANT_ID,
color = STUDY_PARTICIPANT_ID))
It only runs with either one of the geom_line(), but it doesn't when I tried to plot both. I was hoping it would treat them separately, but I don't think that's the case. Does anyone have any suggestion? I originally used geom_smooth() for the fitted lines, but I was unable to add a legend at the side of the ggplot. Therefore, I got the fitted values and put them in the dataset and was just going to plot them with geom_line(). All I wanted was just a label for my linear fit line and my spline. The data here doesn't show the trend, but it will give you the error messages that I was getting. Thank you for your patience with my first post.

Resources