How to plot non-linear regression lines within groups and total data in ggplot2?

与世无争的帅哥 提交于 2020-01-15 05:38:30

问题


I have a simple data set with two continous variables (Vesicle and Cells), and a single grouping variable with two levels (HC and RA), simulated here:

###Simulate Vesicle variable###
Vesicle.hc <- sort(runif(23, 0.98, 5)) #HC group
Vesicle1.ra <- sort(runif(5, 0.98, 3)) #RA group
Vesicle <- c(Vesicle.hc, Vesicle1.ra)  #Combined

###Simulate Cells variable###
z <- seq(23)
Cells.hc <- (rnorm(23, 50 + 30 * z^(0.2), 8))*runif(1, 50000, 400000) #HC group
Cells.ra <- c(8.36e6, 6.35e6, 1.287e7, 1.896e7, 1.976e7)               #RA group
Cells <- c(Cells.hc, Cells.ra)                                         #Combined

###Define groups and create dataframe###
Group <- rep("HC",23)                                #HC group
Group1 <- rep("RA",5)                                #RA Group
Group <- c(Group, Group1)                            #Combined
df <- data.frame(Cells, Vesicle, Group)              #Data frame

I have plotted a scatterplot of the data using ggplot2 with non-linear regression lines (shown here), fitted to each group individually using:

###Plot data###
library(ggplot2)
ggplot(df, aes(x = Cells, y = Vesicle, colour=Group)) +
  xlab("Stimulated neutrophils") +
  ylab("MV/cell") +
  stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)',                     #Fit nls model
              method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) +  #Starting values
  geom_point(size=4, pch=21,color = "black", stroke=1.5, aes(fill=Group))   #Change point style

My question is, in addition to plotting the non-linear regression functions of each group, how can I also plot a regression line fit to all the data i.e. modelling the data ignoring the contribution of the grouping variable?


回答1:


ggplot(df, aes(x = Cells, y = Vesicle, colour=Group)) +
    xlab("Stimulated neutrophils") +
    ylab("MV/cell") +
    stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)',
                method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) +
    stat_smooth(color = 1, method = 'nls', formula = 'y~a*exp(b*x)',
                method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) +
    geom_point(size=4, pch=21,color = "black", stroke=1.5, aes(fill=Group))



来源:https://stackoverflow.com/questions/38378161/how-to-plot-non-linear-regression-lines-within-groups-and-total-data-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!