Reputation: 39
I have 3-column gene expression data in a long format (data_long)
. It looks something like this where column1 represents gene names, column2: Sample, and column 3 the expression value:
Genes sample value
A44 A 7357.891
AAR A 6950.868
A44 B 6771.266
AAR B 4978.734
A44 C 9441.985
AAR C 6061.017
I wanted to generate a line plot grouping all genes based on sample with + mean of all genes in each sample plotted in different color. Here is what I was able to get using
ggplot(data_long, aes(x=sample, y=value)) +
geom_line(aes(group=Genes), size=0.5, alpha=0.3, color="black") +
stat_summary(aes(x=as.numeric(sample)), fun=mean, geom='line',size=1, color='orange') +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_y_continuous(limits = c(0, 40000))
Now, I want to add Group column and fill the background based on Group.
Genes Group sample value
A44 A1 A 7357.891
AAR A1 A 6950.868
A44 A1 B 6771.266
AAR A1 B 4978.734
A44 A2 C 9441.985
AAR A2 C 6061.017
I am getting an error when I say fill = "Group". Here is what I want. Please help me on this.
Upvotes: 0
Views: 522
Reputation: 37913
I'm not quite sure what is going wrong in your case: when trying to reproduce the plot with your code I ran into errors. After some tweaking (code below), I managed to get a plot and adding a background colour seemed straightforward then.
library(ggplot2)
data_long <- "Genes Group sample value
A44 A1 A 7357.891
AAR A1 A 6950.868
A44 A1 B 6771.266
AAR A1 B 4978.734
A44 A2 C 9441.985
AAR A2 C 6061.017"
data_long <- read.table(text = data_long, header = TRUE)
# example toy data
data_long <- data.frame(expand.grid(Genes = c("A1","A2","B1","B2"),
sample = c("a1","a2","b1")),
value = rnorm(n = 12, mean = 15000, sd = 3000))
data_long$Group <- factor(ifelse(data_long$sample %in% c("a1","a2"), "A", "B"))
ggplot(data_long, aes(x=sample, y=value)) +
geom_tile(aes(fill = Group, y = 7000), height = Inf, alpha = 0.3) +
geom_line(aes(group=Genes), size=0.5, alpha=0.3, color="black") +
stat_summary(aes(group = -1), fun=mean, geom='line',size=1, color='orange') +
theme_classic()
[![enter image description here][1]][1]
Created on 2021-09-14 by the reprex package (v2.0.1)
Upvotes: 1