首页 > 解决方案 > 我在 3 个不同的时间点收集了 7 个不同的病毒浓度数据点。如何用 R 中的误差线绘制它?

问题描述

我收集了七种不同的样本,其中含有不同浓度的昆津病毒。

如何创建反映此数据的点图,包括 R 中的误差线?我正在使用ggplot2。

到目前为止,我的代码是:

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

标签: rggplot2graphing

解决方案


我会建议你下一个方法。如果你想要误差线,你可以根据平均值和标准差来计算它。在接下来的代码中勾画了实现这一点的方法。我使用了一个标准偏差,但您可以设置任何其他值。另外,由于您想查看不同的示例,我使用了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')

输出:

在此处输入图像描述


推荐阅读