r - 我在 3 个不同的时间点收集了 7 个不同的病毒浓度数据点。如何用 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')
输出:
推荐阅读
- unity3d - 在 unity 2020 1.10f 中使用 shadergraph 时,所有着色器都呈现黑色
- python - 构建节点树的搅拌机 python 脚本在命令行和 django 中工作,但在 Apache 后面失败(在 www-data 用户下)
- flutter - 您如何将交互式查看器包含到盒子中?
- javascript - TypeError:无法读取未定义的属性“范围”
- semantic-ui - Semantic-UI/Fomantic-UI:如何向列表项添加换行符
- module - 如何在 ReScript 中编写简短的本地打开?
- javascript - Date() VS代码与控制台中的不同输出
- python - tkinter:在我使用两帧时将滚动条添加到一帧
- reactjs - 如何在玩笑中使用 forwardref 模拟子组件组件?
- javascript - 添加按钮以在列表中插入新帖子