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.
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).
I have a long-format data set (“uni_l_all”) and I’m trying to get a plot with 4 graphs, showing different trajectories contingent on high vs. low self-efficacy (“self_efficacy”, binary variable) and on training group vs. control group (“train2”, binary variable).
Grouping by self_efficacy to get two graphs works well.
But when I try to introduce “train2”, I still receive only 2 (crazy looking) graphs.
Do you have an idea how to solve this? How can I add train2 in my functions?
mean_uni_l_all <- group_by(uni_l_all, self_efficacy, time) %>%
summarise(ent_act = mean(ent_act, na.rm = TRUE))
ggplot(na.omit(mean_uni_l_all), aes(x = time, y = ent_act, colour = ese_mean, group = self_efficacy)) +
geom_point() + geom_line()
I'm trying to plot observed and predicted variables on the same plot in lattice. My data is a repeated dataset and I've tried a few things which haven't worked. Any assistance would be much appreciated. Code is given below.
library(nlme)
library(lattice)
# add random conc predictions to data
Theoph$predConc <- rnorm(132, 5)
# My attempt at plotting both predConc and conc against time on the same plot
lattice::xyplot(predConc + conc ~ Time | Subject, groups=Subject, data=Theoph, type="l", layout = c(4,4))
As you can see, it doesn't seem to be doing what I want it to do. Ideally I would like the "conc" and "predConc" to be in different colours but appear together on each panel for each Id so I can compare the two easily.
As was suggested in the comments, it is fixed simply by dropping groups = Subject.
lattice::xyplot(predConc + conc ~ Time | Subject, data = Theoph, type = "l",
auto.key = TRUE)
What I have is a 3-Levels Repeated Measures Factor and a continuous variable (Scores in psychological questionnaire, measured only once pre-experiment, NEO), which showed significant interaction together in a Linear Mixed Effects Model with a Dependent Variable (DV; State-Scores measured at each time level, IAS).
To see the nature of this interaction, I would like to create a plot with time levels on X-Axis, State-Score on Y-Axis and multiple curves for the continuous variable, similar to this. The continuous variable should be categorized in, say quartiles (so I get 4 different curves), which is exactly what I can't achieve. Until now I get a separate curve for each value in the continuous variable.
My goal is also comparable to this, but I need the categorial (time) variable not as separate curves but on the X-Axis.
I tried out a lot with different plot functions in R but did'nt manage to get what I want, maybe because I am not so skilled in dealing with R.
F. e.
gplot(Data_long, aes(x = time, y = IAS, colour = NEO, group = NEO)) +
geom_line()
from the first link shows me dozens of curves (one for each value in the measurement NEO) and I can't find how to group continuous variables in a meaningful way in that gplot function.
Edit:
Original Data:
http://www.pastebin.ca/2598926
(I hope it is not too inconvenient.)
This object (Data_long) was created/converted with the following line:
Data_long <- transform(Data_long0, neo.binned=cut(NEO,c(25,38,46,55,73),labels=c(".25",".50",".75","1.00")))
Every value in the neo.binned col seems to be set correctly with enough cases per quantile.
What I then tried and didn't work:
ggplot(Data_long, aes(x = time, y = ias, color = neo.binned)) + stat_summary(fun.y="median",geom="line")
geom_path: Each group consist of only one observation. Do you need to adjust the group >aesthetic?
I got 92 subjects and values for NEO between 26-73. Any hints what to enter for cut and labels function? Quantiles are 0% 25% 50% 75% 100% 26 38 46 55 73.
Do you mean something like this? Here, your data is binned according to NEO into three classes, and then the median of IAS over these bins is drawn. Check out ?cut.
Data_long <- transform(Data_long, neo.binned=cut(NEO,c(0,3,7,10),labels=c("lo","med","hi")))
Plot everything in one plot.
ggplot(Data_long, aes(x = time, y = IAS, color = neo.binned))
+ stat_summary(aes(group=neo.binned),fun.y="median",geom="line")
And stealing from CMichael's answer you can do it all in multiple (somehow you linked to facetted plots in your question):
ggplot(Data_long,aes(x=time,y=IAS))
+ stat_summary(fun.y="median",geom="line")
+ facet_grid(neo.binned ~ .)
Do you mean facetting #ziggystar initial Plot?
quantiles = quantile(Data_long$NEO,c(0.25,0.5,0.75))
Data_long$NEOQuantile = ifelse(Data_long$NEO<=quantiles[1],"first NEO Quantile",
ifelse(Data_long$NEO<=quantiles[2],
"second NEO Quantile",
ifelse(Data_long$NEO<=quantiles[3],
"third NEO Quantile","forth NEO Quantile")))
require(ggplot2)
p = ggplot(Data_long,aes(x=time,y=IAS)) + stat_quantile(quantiles=c(1),formula=y ~ x)
p = p + facet_grid(.~NEOQuantile)
p