[英]How to plot two trend lines in a single graph in ggplot2?
我正在为具有两种处理和 3 个样本的基因表达数据绘制一条带有平滑趋势线的线图。 长格式的示例如下所示,
Group Gene Sample exp
C Gene_1 Sample1 0.8833248
C Gene_2 Sample1 2.9193536
C Gene_3 Sample1 -4.27416
S Gene_1 Sample1 -1.6297201
S Gene_2 Sample1 3.6535838
S Gene_3 Sample1 -4.27416
C Gene_1 Sample2 -0.3275709
C Gene_2 Sample2 3.4885281
.
.
C Gene_3 Sample3 -2.923909
S Gene_1 Sample3 0.3514516
S Gene_2 Sample3 2.981017
S Gene_3 Sample3 -3.1599246
这里的问题是我可以为每种治疗分别制作趋势图,但我不知道如何在同一张图中绘制两种治疗的趋势线。 我做了类似的事情,
C_S<-ggplot(CS_long_fmt, aes(x=Sample, y=exp)) +
geom_line(aes(group=Gene)) +
stat_summary(aes(x=as.numeric(Sample)), fun=mean, geom='line',
size=1, color='blue')
要为每个Group
(治疗?)获得单独的趋势线,您必须将group
美学添加到stat_summary
。
注意:另外,我在color
aes 上映射以区分线条。
library(ggplot2)
ggplot(CS_long_fmt, aes(x = Sample, y = exp)) +
geom_line(aes(group = Gene)) +
stat_summary(aes(x = Sample, group = Group, color = Group),
fun = mean, geom = "line",
size = 1
)
数据
CS_long_fmt <- structure(list(Group = c(
"C", "C", "C", "S", "S", "S", "C", "C",
"C", "S", "S", "S"
), Gene = c(
"Gene_1", "Gene_2", "Gene_3", "Gene_1",
"Gene_2", "Gene_3", "Gene_1", "Gene_2", "Gene_3", "Gene_1", "Gene_2",
"Gene_3"
), Sample = c(
"Sample1", "Sample1", "Sample1", "Sample1",
"Sample1", "Sample1", "Sample2", "Sample2", "Sample3", "Sample3",
"Sample3", "Sample3"
), exp = c(
0.8833248, 2.9193536, -4.27416,
-1.6297201, 3.6535838, -4.27416, -0.3275709, 3.4885281, -2.923909,
0.3514516, 2.981017, -3.1599246
)), class = "data.frame", row.names = c(
NA,
-12L
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.