[英]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?
I collected seven different samples containing varying concentrations of the Kunjin Virus.我收集了七个不同的样本,其中包含不同浓度的昆津病毒。
How do I create a dotplot reflecting this data including error bars in R?如何创建反映此数据的点图,包括 R 中的误差条? I'm using ggplot2.
我正在使用 ggplot2。
My code so far is:到目前为止我的代码是:
data1 <-data.frame(hours, titer)
ggplot(data1, aes(x=hours, y=titer, colour = hours)) + geom_point()
I would suggest you next approach.我建议你下一个方法。 If you want error bars you can compute it based on mean and standard deviation.
如果你想要误差线,你可以根据均值和标准差来计算它。 In the next code is sketched the way to do that.
在下一个代码中勾画了这样做的方法。 I have used one standard deviation but you can set any other value.
我使用了一个标准偏差,但您可以设置任何其他值。 Also as you want to see different samples, I have used
facet_wrap()
.另外,由于您想查看不同的示例,我使用了
facet_wrap()
。 Here the code:这里的代码:
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')
Output:输出:
If you have a common y-axis scale, you can try this:如果你有一个通用的 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')
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.