簡體   English   中英

我在 3 個不同時間點收集了 7 個不同的病毒濃度數據點。 我如何在 R 中用誤差線繪制這個圖?

[英]I have 7 different data points for virus concentration collected at 3 different time points. How do I graph this with error bars in R?

我收集了七個不同的樣本,其中包含不同濃度的昆津病毒。

  • 3 個樣本來自 24 小時時間點:667、1330、1670
  • 2個樣本來自48小時時間點:323000、590000
  • 2 個樣本來自 72 小時時間點:3430000、4670000

如何創建反映此數據的點圖,包括 R 中的誤差條? 我正在使用 ggplot2。

到目前為止我的代碼是:

data1 <-data.frame(hours, titer)
ggplot(data1, aes(x=hours, y=titer, colour = hours)) + geom_point()

我建議你下一個方法。 如果你想要誤差線,你可以根據均值和標准差來計算它。 在下一個代碼中勾畫了這樣做的方法。 我使用了一個標准偏差,但您可以設置任何其他值。 另外,由於您想查看不同的示例,我使用了facet_wrap() 這里的代碼:

library(ggplot2)
library(dplyr)
#Data
df <- data.frame(sample=c(rep('24 hour',3),rep('48 hour',2),rep('72 hour',2)),
                 titer=c(667, 1330, 1670,323000, 590000,3430000, 4670000),
                 stringsAsFactors = F)
#Compute error bars
df <- df %>% group_by(sample) %>% mutate(Mean=mean(titer),SD=sd(titer))
#Plot
ggplot(df,aes(x=sample,y=titer,color=sample,group=sample))+
  geom_errorbar(aes(ymin=Mean-SD,ymax=Mean+SD),color='black')+
  geom_point()+
  scale_y_continuous(labels = scales::comma)+
  facet_wrap(.~sample,scales='free')

輸出:

在此處輸入圖片說明

如果你有一個通用的 y 軸刻度,你可以試試這個:

#Plot 2
ggplot(df,aes(x=sample,y=titer,color=sample,group=sample))+
  geom_errorbar(aes(ymin=Mean-SD,ymax=Mean+SD),color='black')+
  geom_point()+
  scale_y_continuous(labels = scales::comma)+
  facet_wrap(.~sample,scales = 'free_x')

輸出:

在此處輸入圖片說明

暫無
暫無

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

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