如何在 ggplot2 中绘制组内的非线性回归线和总数据?
How to plot non-linear regression lines within groups and total data in ggplot2?
我有一个简单的数据集,其中包含两个连续变量(囊泡和细胞)和一个包含两个级别(HC 和 RA)的分组变量,在此处模拟:
###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
我使用带有非线性回归线的 ggplot2 绘制了数据散点图(显示 here),分别使用以下方法拟合每个组:
###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
我的问题是,除了绘制每组的非线性回归函数外,我还如何绘制适合 all[ 的回归线? =22=] 数据即忽略分组变量贡献的数据建模?
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))
我有一个简单的数据集,其中包含两个连续变量(囊泡和细胞)和一个包含两个级别(HC 和 RA)的分组变量,在此处模拟:
###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
我使用带有非线性回归线的 ggplot2 绘制了数据散点图(显示 here),分别使用以下方法拟合每个组:
###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
我的问题是,除了绘制每组的非线性回归函数外,我还如何绘制适合 all[ 的回归线? =22=] 数据即忽略分组变量贡献的数据建模?
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))