如何在ggplot2中使用线图和误差线针对一个因子绘制几个连续变量?

时间:2019-06-07 23:19:31

标签: r ggplot2 errorbar line-plot

我试图用R中的ggplot2绘制线图,其中多个变量的相对丰度是时间(T0,T1,T2,T3,T4)。按列,每个样本中都有大量不同的细菌类群。

我的数据框的外观是:

> head(df)
               X f__Entomoplasmatales.Incertae.Sedis f__Propionibacteriaceae f__Corynebacteriaceae f__Paenibacillaceae
1   Jan2016.acr2                                   0             0.000000000           0.002253267         0.004055881
2  Jan2016.acr30                                   0             0.000000000           0.000000000         0.000000000
3  Jan2016.acr56                                   0             0.001461406           0.394712369         0.000000000
4  Jan2016.acr62                                   0             0.000194742           0.000389484         0.000000000
5  Jan2016.acr90                                   0             0.000000000           0.000636132         0.000000000
6 Jan2017.acr127                                   0             0.000000000           0.000000000         0.000000000
  f__Streptococcaceae f__Staphylococcaceae f__Xanthomonadaceae f__Pseudomonadaceae f__Endozoicomonadaceae f__Alteromonadaceae
1         0.000000000          0.000000000                   0         0.000000000             0.94637224         0.000901307
2         0.000000000          0.000000000                   0         0.000000000             0.70676032         0.176470588
3         0.025773881          0.013285506                   0         0.008104158             0.04118507         0.000000000
4         0.000194742          0.000000000                   0         0.000000000             0.98364167         0.000000000
5         0.000000000          0.006149279                   0         0.008269720             0.94868533         0.000000000
6         0.000000000          0.000000000                   0         0.000000000             0.98713398         0.000000000
  f__Enterobacteriaceae f__Vibrionaceae f__Moraxellaceae f__Sphingomonadaceae f__Reyranellaceae f__Rhizobiaceae time
1                     0     0.001351960      0.000000000          0.000000000       0.003154574               0   T0
2                     0     0.000000000      0.007901668          0.002633889       0.000000000               0   T0
3                     0     0.007174173      0.330941942          0.000000000       0.000000000               0   T2
4                     0     0.000292113      0.002434275          0.000000000       0.000000000               0   T2
5                     0     0.000000000      0.005513147          0.000000000       0.000000000               0   T1
6                     0     0.000000000      0.000000000          0.000000000       0.000000000               0   T1

我尝试下面的代码将单个列(f__Endozoicomondaceae)绘制为线形图,但是我想在一个具有不同色线的图中绘制所有不同的列。我附上了情节。有什么建议吗?

# Functions
sderr <- function(x) {sd(x)/sqrt(length(x))}

data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sderr(x[[col]]), na.rm=TRUE)
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

df_endo <- data_summary(df, varname="f__Endozoicomonadaceae", groupnames=c("time"))

ggplot(df_endo, aes(time, f__Endozoicomonadaceae, group = 1)) +
  geom_line() +
  geom_point() + 
  geom_errorbar(aes(ymin=f__Endozoicomonadaceae-sd, ymax=f__Endozoicomonadaceae+sd), width=.05,
                position = position_dodge(0.05)) +
  ylab("Relative Abundance") + xlab("Time") + theme_bw() 

enter image description here

1 个答案:

答案 0 :(得分:0)

非常感谢,我找到了解决方法:

undefined