绘制基因表达值并添加更平滑的线

时间:2018-05-25 14:28:08

标签: r ggplot2

我想基于它们的表达值绘制基因簇。融化后(使用reshape)原始数据帧,我的矩阵就像这样:

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

我想要做的是在x轴上绘制时间(5个时间点),在y轴上绘制值(表达式值),从而具有3条线并添加更平滑的线。

我尝试使用this帖子中写的内容但遇到此错误Error: Discrete value supplied to continuous scale

我打电话给ggplot如下:

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()

2 个答案:

答案 0 :(得分:0)

我认为这是你正在寻找的,5个时间点,3组?你不需要做很多数据来做黄土()平滑。

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()

答案 1 :(得分:0)

你遇到连续与离散x轴的问题,因为你的时间是一个角色,在这个角色中,LOESS平滑是没有意义的。相反,您可以提取与time关联的数字,将其转换为数字,然后将其放在x轴上,这样就可以在那里进行连续缩放。然后,您可以选择更改标签以重新创建“A1.04”样式标签。我用sprintf做了这个,但我可能没有得到你需要的完整模式。

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))

reprex package(v0.2.0)创建于2018-05-25。