简体   繁体   中英

Plotting gene expression values and adding a smoother line

I would like to plot clusters of genes based on their expression values. My matrix is like this after melting (with reshape ) the original dataframe:

time    gene    value
A1.01   TMCS09g1008676  0.423176672
A1.02   TMCS09g1008676  0.911415197
A1.03   TMCS09g1008676  1.042786687
A1.04   TMCS09g1008676  0.859630996
A1.05   TMCS09g1008676  0.624891793
A1.01   TMCS09g1008677  0.304568066
A1.02   TMCS09g1008677  1.134582618
A1.03   TMCS09g1008677  1.626528999
A1.04   TMCS09g1008677  1.778379422
A1.05   TMCS09g1008677  1.922418792
A1.01   TMCS09g1008678  0.312127815
A1.02   TMCS09g1008678  0.567599868
A1.03   TMCS09g1008678  1.37594692
A1.04   TMCS09g1008678  1.655878776
A1.05   TMCS09g1008678  1.720470659

What i want to do is plotting on x-axis the time (5 time points), on y-axis the value (expression values), having thus 3 lines and adding a smoother line.

I tried with what was written in this post but having this error Error: Discrete value supplied to continuous scale

I am calling ggplot as follows:

ggplot(mydata, aes(as.factor(time), value)) +
geom_hline(yintercept = 0, linetype = 2, color = "red") +
# Line for each gene
geom_line(aes(group = gene), size = 0.5, alpha = 0.3, color = "blue") + 
# Trend line
geom_smooth(size = 2, se = FALSE, color = "orange") +
scale_x_continuous(breaks = factor(prova$time)) + 
theme_classic()

I think this is what you're looking for, 5 time points, 3 groups? You don't really have a lot of data to do loess() smoothing.

GG = ggplot(mydata, aes(x = as.factor(time), y = value, group = gene))+
geom_hline(yintercept = 0, linetype = 2, color = "red") +
# Line for each gene
geom_line(aes(group = gene), size = 0.5, alpha = 0.3, color = "blue") + 
geom_line(stat = 'smooth', method = 'loess', span = 2, size = 2, color = 'orange')+
# Trend line
scale_x_discrete(breaks = factor(mydata$time)) + 
theme_classic()

You're having an issue with continuous vs discrete x-axis because your time is a character, across which LOESS smoothing doesn't make sense. Instead, you can extract the number associated with time , convert it to numeric, and put that on your x-axis so you have a continuous scale there. Then you can optionally change the labels to recreate the "A1.04" style labels. I did this with sprintf , but I might not have gotten the full pattern that you need.

library(tidyverse)

df %>%
  mutate(time2 = str_extract(time, "(?<=\\.)\\d+$") %>% as.numeric()) %>%
  ggplot(aes(x = time2, y = value)) +
    geom_line(aes(group = gene), size = 0.5, alpha = 0.3, color = "blue") +
    geom_smooth(method = loess, se = F, color = "orange") +
    geom_hline(yintercept = 0, linetype = 2, color = "red") +
    scale_x_continuous(labels = function(x) sprintf("A1.%02d", x))

Created on 2018-05-25 by the reprex package (v0.2.0).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM