I am plotting a line plot with a smooth trend line for gene expression data with two treatments and 3 samples. An example of the long format looks like this,
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
Here the issue is I can make a trend plot separately for each treatment but I have no clue how to plot the trend line of two treatments in the same graph. I did something like,
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')
CodePudding user response:
To get separate trend lines for each Group
(Treatment?) you have to add the group
aesthetic to stat_summary
.
Note: Additionally I mapped on the color
aes to differentiate the lines.
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
)
DATA
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
))