簡體   English   中英

如何使用 ggplot2 中的折線圖和誤差條針對一個因子繪制多個連續變量?

[英]How to plot several continuous variables against a factor with a line plot and error bars in ggplot2?

我正在嘗試使用 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"))

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

在此處輸入圖片說明

非常感謝,我找到了解決方案:

df <- df[,-1]
df_long <- df %>% gather(Family, Relabund, -time)
df_relabund <- data_summary(df_long, varname="Relabund", groupnames=c("time", "Family"))

ggplot(df_relabund, aes(x = time, y = Relabund, color = Family, group = Family)) +
  geom_point() +
  geom_line() +
#  scale_color_brewer(palette = 'Dark2') +
  theme_classic(base_size = 12) +
  geom_errorbar(aes(ymin=Relabund-sd, ymax=Relabund+sd), width=.05,
              position = position_dodge(0.05)) +
  ylab("Relative Abundance") + xlab("Time") + theme_bw() 
```[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/p7ZjT.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM