[英]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.