How to draw three differents non-linear regression with ggplot2 - r

I am trying to draw three differents non-linear regression with ggplot2 (like I did with graphpad below (dotted line) (because graphpad can't compare non-linear regression between groups):
So far, I drew this graph:
With the following code:
gp <- ggplot(datapoidsmono, aes(x = time, y = weight)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
scale_shape_manual(values=c(15,16,17), name ="Treatment", labels=c("A", "B", "C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") +
theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank())
I can't figure out how to draw a non-linear regression for each groups.
The following code did not return any drawn line (no error either):
ggplot(datapoidsmono, aes(time, weight, color = group)) +
geom_point() +
stat_smooth(method = "lm", se=FALSE)
Nor did this one (found here):
ggplot(datapoidsmono, aes(x = time, y = weight, colour=group)) +
stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)') +
stat_smooth(color = 1, method = 'nls', formula = 'y~a*exp(b*x)') +
geom_point(aes(fill=group))
Any idea or clue would be useful to continue! Thanks
Update
As suggested by #PoGibas, I added "group=group" in aes inside first line which worked pefectly to draw the lines!
I tried mulltiple solution to get the perfect fit:
gp + ggplot(aes(group=group))
stat_smooth(method = "lm", formula = y ~ x, size = 1, se = FALSE,colour = "black") +
stat_smooth(method = "lm", formula = y ~ x + I(x^2),size = 1, se = FALSE, colour = "blue") +
stat_smooth(method = "loess", formula = y ~ x, size = 1, se = FALSE, colour = "red") +
stat_smooth(method = "gam", formula = y ~ s(x), size = 1, se = FALSE, colour = "green") +
stat_smooth(method = "gam", formula = y ~ s(x, k = 3), size = 1, se = FALSE, colour = "violet") +
stat_smooth(method = "auto", se=F, colour = "yellow")
But I figured that simply gp + stat_smooth() did perfectly the job (the LOESS method is used).
Now I am trying to change aspect (to a dotted line) and color of the fit...
I tryed gp + stat_smooth(se=F, aes(fill = group)) but now I have another legend box and my lines are always with the same color...
I also tryed to add linetype=group in the aes, but when I use scale_linetype_manual(values=c("dotted", "dotted", "dotted")), every line is dotted (included errorbar)
The complete code is:
ggplot(datapoidsmono, aes(x = time, y = weight, group=group, linetype=group)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
scale_shape_manual(values=c(15,16,17), name ="Treatment", labels=c("A", "B", "C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") +
theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
stat_smooth(se=F, aes(fill = group)) +
scale_linetype_manual(values=c("dotted", "dotted", "dotted"))

Thanks to #PoGibas and this post, I added
group=group, color=group in the aes of ggplot and it gave me a good result.
ggplot(datapoidsmono, aes(x = time, y = weight, group=group, color=group)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
scale_shape_manual(values=c(15,16,17), name ="Treatment", labels=c("A", "B", "C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") +
theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
stat_smooth(se=F, linetype="dotted")
Here is the final graph:
NB: the fit proposed by graphpad (see first graph) is more stat_smooth(method = "lm", formula = y ~ x + I(x^2),size = 1, se = FALSE) than the one I finally chose (LOESS)

Related

Is there a way to plot different regression models in the same plot?

I have a data set in a scatterplot. I want to "overlap" graphs which include some values and not others, but all in the same data set. Essentially, comparing the correlation with n=5, n=8, and n=10. Is there a way to set my regression line for each of these using the same data?
geom_point(aes(fill = Vaccine), pch = 21, color = 'black', size = 4) +
scale_fill_manual(values = color_list3) +
geom_smooth(method='lm', se=F, color="black", size = 0.5) +
stat_poly_eq(formula = y ~ x,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
theme_classic() +
theme(legend.title = element_text(face = "bold", hjust = 0.5),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black")) +
coord_capped_cart(bottom = 'both', left = 'both') +
labs(x = 'Log antibodies (GTM)', y = 'Vaccine efficacy')

ggplot2 add manual legend for two data series

I have this dataframe:
Control Stress days sd_control sd_stress
X1 0.9702100 0.9343627 X1 0.001900535 0.07035645
X2 0.9666619 0.8595523 X2 0.014946893 0.04066567
X3 0.9165654 0.7160598 X3 0.072655343 0.07025344
X4 0.9208237 0.6668044 X4 0.050870831 0.08736982
X5 0.8766547 0.7660685 X5 0.073588197 0.04868614
X6 0.9599553 0.7937444 X6 0.041559836 0.05326769
X7 0.9736297 0.8188934 X7 0.003817743 0.06272428
and based on this data I've done this plot:
With the following code:
significance <- data.frame(days=c("X2","X3","X4","X6"),value=c(1.02,1.02,1.02,1.02))
ggplot(my_data, aes(x=days,y=Control,group=1)) +
geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
width=0.2, size=0.5) +
geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
width=0.2, size=0.5) +
geom_point(shape=23,color='gray45',fill='gray45',size=4) +
geom_line(color='gray45',size=1) +
geom_point(data=my_data,aes(x=days,y=Stress),size=4,shape=22,fill='gray',color='gray',
show.legend = TRUE) +
geom_line(data = my_data, aes(x=days,y=Stress),color='gray',size=1) +
geom_point(data=significance, aes(x=days,y=value),shape='*',size=6) +
labs(x='\nDAT',y='RWC\n') +
scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04),
expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
scale_x_discrete(expand = c(0.07, 0),labels = c(0,7,14,21,27,35,42)) +
ggtitle('Relative Water Content\n') +
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_rect(fill = 'white'),
plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
axis.title = element_text(family = 'Calibri',face = 'bold'),
axis.text = element_text(family = 'Calibri')
)
I want to add a legend in the bottom-right on the plot that describres the Control and Stress Treatmentes with the same shape of the points. I've tried several approaches that I've found here as set a color vector and scale_colour_manual attributes but none of them worked. Any suggestion?
The issue is that you use the color, fill and shape arguments.
To get a legend you have to map on aesthetics, i.e. inside aes().
After doing so ggplot will add lgends(s) automatically and you can apply scale_xxx_manual to get the desired colors, fill and shapes.
However, as this results in 3 legends (was not able to figure out why the merging of the legends failed) I use guides to keep only one of them and guide_legend to style the legend. Try this:
library(ggplot2)
library(scales)
ggplot(my_data, aes(x=days, group=1)) +
geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
width=0.2, size=0.5) +
geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
width=0.2, size=0.5) +
geom_point(aes(y=Control, color = "Control", fill = "Control", shape = "Control"), size=4) +
geom_line(aes(y=Control, color = "Control"),size=1) +
geom_point(aes(y=Stress, color = "Stress", fill = "Stress", shape = "Stress"), size=4) +
geom_line(aes(y=Stress, color = "Stress"), size=1) +
geom_point(data=significance, aes(y=value),shape='*',size=6) +
scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_shape_manual(values = c("Control" = 23, "Stress" = 22)) +
guides(shape = FALSE, fill = FALSE,
color = guide_legend(override.aes = list(shape = c("Control" = 23, "Stress" = 22),
fill = c("Control" = 'gray45', "Stress" = 'gray')))) +
labs(x='\nDAT',y='RWC\n') +
scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04),
expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
scale_x_discrete(expand = c(0.07, 0), labels = c(0,7,14,21,27,35,42)) +
ggtitle('Relative Water Content\n') +
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_rect(fill = 'white'),
plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
axis.title = element_text(family = 'Calibri',face = 'bold'),
axis.text = element_text(family = 'Calibri')
)

How to avoid repeated text due to strip in R?

I want to add a text to my forest plot in R that has strip in it but the text is repeated on every strip . how can I add only the text to one strip or just on the plot? My code is as below:
My data is like:
Group Mean LowerLimit UpperLimit
M 1.172827 1.083498 1.268857
H 5.142589 4.333141 6.148088
h<-"XXXX"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit),
+
ggtitle(PlotTitle)+
geom_point(aes(fill=Group, color=Group), shape=22, size=3)+
geom_pointrange(aes(col=Group), fatten = 3)+
geom_hline(aes(),yintercept =1, linetype="longdash")+
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label=h,
check_overlap = T)+
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1)+
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm"))+
coord_flip()
p
In your parameters for geom_text try changing label=h to label = ''
library(ggplot2)
df4 <- data.frame(Group = c("M", "H"),
Mean = c(1.172827, 5.142589),
LowerLimit = c(1.083498, 4.333141),
UpperLimit = c(1.268857, 6.148088))
PlotTitle = "Insert plot title here"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit)) +
ggtitle(PlotTitle) +
geom_point(aes(fill=Group, color=Group), shape=22, size=3) +
geom_pointrange(aes(col=Group), fatten = 3) +
geom_hline(aes(),yintercept =1, linetype="longdash") +
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label='',
check_overlap = T) +
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1) +
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm")) +
coord_flip()
p
which yields this image:

Layout of plots with a unique legend using ggplot

I was trying to create a layout with plots sharing the same legend. The legend is on the top of the first plot, however, the next plot has a different scale. How can I solve this?
library(ggplot2)
library(gridExtra)
grid.arrange(
ggplot(mpg, aes(displ, cty)) +
geom_point(aes(shape = "Data")) +
stat_smooth(aes(linetype = "Regression"), method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
scale_shape_manual(values = 1) +
labs(shape = "", linetype = "") +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
legend.position = "top")
,
ggplot(mpg, aes(displ, cty)) +
geom_point(shape = 1) +
stat_smooth(method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
)
If the plots also have the same axes labels, facet_wrap may be a good option.
library(ggplot2)
data = rbind(data.frame("id" = 1, mpg), data.frame("id" = 2, mpg))
ggplot(data, aes(displ, cty)) +
geom_point(aes(shape = "Data")) +
stat_smooth(aes(linetype = "Regression"), method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
scale_shape_manual(values = 1) +
labs(shape = "", linetype = "") +
theme_classic() +
facet_wrap(~id, ncol = 1 ) +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
legend.position = "top",
strip.background = element_blank(),
strip.text.x = element_blank()) #these two lines remove the facet strips
grid.arrange doesn't try to align plot panels; it's a generic function meant for all kinds of grid graphics, and in this case since the top plot has a legend it gets shrunk to fit in the available space (by default 1/2 of the page here). For the specific case of ggplots I would use egg::ggarrange,
library(ggplot2)
library(egg)
ggarrange(
ggplot(mpg, aes(displ, cty)) +
geom_point(aes(shape = "Data")) +
stat_smooth(aes(linetype = "Regression"), method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
scale_shape_manual(values = 1) +
labs(shape = "", linetype = "") +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
legend.position = "top")
,
ggplot(mpg, aes(displ, cty)) +
geom_point(shape = 1) +
stat_smooth(method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
)
I don't know how to use grid.arrange, but here's a solution using my cowplot package. The idea is to separate the legend out from the plot and then put the three elements into one column. A similar approach would work with grid.arrange, I assume.
library(cowplot)
p1 <- ggplot(mpg, aes(displ, cty)) +
geom_point(aes(shape = "Data")) +
stat_smooth(aes(linetype = "Regression"), method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
scale_shape_manual(values = 1) +
labs(shape = "", linetype = "") +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
legend.position = "top")
p2 <- ggplot(mpg, aes(displ, cty)) +
geom_point(shape = 1) +
stat_smooth(method = "lm",
formula = y ~ x, se = FALSE, colour = 1, size = 0.5) +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size = 0.5),
aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10))
legend <- get_legend(p1)
plot_grid(legend, p1 + theme(legend.position = "none"), p2,
ncol=1, rel_heights = c(0.1, 1, 1))

Add legend to manually added lines using ggplot

I'm trying to add the corresponding legend for 3 manually added lines using ggplot. My code is the following:
library(ggplot2)
df = data.frame(error = c(0.0832544999, 0.0226680026, 0.0082536264, 0.0049199958, 0.0003917755, 0.0003859976, 0.0003888253, 0.0003953918, 0.0003958398), sDev = c(8.188111e-03, 2.976161e-03, 1.466221e-03, 2.141425e-03, 2.126976e-05, 2.139364e-05, 2.169059e-05, 2.629895e-05, 2.745938e-05))
minimum <- 6
best.model <- 5
gplot <- ggplot(df, aes(x=1:length(error), y=error)) +
scale_x_continuous(breaks = seq_along(df$error)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev),
width = 0.1) +
geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum]), linetype = "dashed") +
geom_vline(xintercept = minimum, linetype = "dotted", color = "red", size = 1) +
geom_vline(xintercept = best.model, linetype = "dotted", color = "blue", size = 1) +
theme_gray(base_size = 18) +
theme(axis.text = element_text(color = "black")) +
labs(x = "# of parameters", fontface = "bold") +
labs(y = "CV error") +
labs(title = "Cross-validation error curve")
I'd like to know how to add the legends for the 3 dotted lines in black, red, and blue.
Thanks a lot in advance!
The trick is to use appropriate mapping:
gplot <- ggplot(df, aes(x=1:length(error), y=error)) +
scale_x_continuous(breaks = seq_along(df$error)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev),
width = 0.1) +
geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum], linetype="a", colour="a")) +
geom_vline(data= data.frame(type="b", col="b", minimum=minimum),
aes(linetype=type, colour=col, xintercept = minimum), size = 1, show_guide = TRUE) +
geom_vline(data= data.frame(type="b", col="b", best.model=best.model),
aes(linetype="c", colour="c", xintercept = best.model), size = 1, show_guide = TRUE) +
scale_colour_manual(name="Legend", values = c("a" = "black", "b" = "red", "c" = "blue")) +
scale_linetype_manual(name="Legend", values = c("a" = "dashed", "b" = "dotted", "c" = "dotted")) +
theme_gray(base_size = 18) +
theme(axis.text = element_text(color = "black"),
legend.key.height = grid::unit(0.1, "npc")) +
labs(x = "# of parameters", fontface = "bold") +
labs(y = "CV error") +
labs(title = "Cross-validation error curve")

Resources