简体   繁体   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?

I collected seven different samples containing varying concentrations of the Kunjin Virus.我收集了七个不同的样本,其中包含不同浓度的昆津病毒。

  • 3 samples are from the 24 hour time point: 667, 1330, 1670 3 个样本来自 24 小时时间点:667、1330、1670
  • 2 samples are from the 48 hour time point: 323000, 590000 2个样本来自48小时时间点:323000、590000
  • 2 samples are from the 72 hour time point: 3430000, 4670000 2 个样本来自 72 小时时间点:3430000、4670000

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.

相关问题 如何在图形上将1000个点绘制成不同颜色的点? - How Do I Graph 1000 Points Onto A Graph, With Different Points Being Different Colors? 如何使 r 中图表上的一系列点具有不同的颜色 - How to make a range of points on a graph i n r a different colour 我如何使用 R 在 R 中随机使用 3 个不同的 colors 为图形点着色 - How can i color points of a graph randomly with 3 different colors in R using R basic only 用来自不同数据框ggplot R的误差线绘制点 - Plotting points with error bars from different dataframes ggplot R 将误差线添加到不同层中的点-ggplot - Add error bars to points in different layers -ggplot 当我没有任何数据点时(在R中)如何绘制abline() - How do I plot an abline() when I don't have any data points (in R) 在R中的图形上绘制多组不同的数据点 - plotting multiple sets of different data points on a graph in R 如何将我的点与线连接,但在 gpplot 中按不同的组? - How do I connect my points with lines, but by different groups in gpplot? 如何制作遵循时间序列的条形图,但对来自不同因素的条形图使用不同的颜色 - How can I make a bar graph that follows a time series but uses a different color for bars from different factors 如何在 R 中绘制非常接近的数据点? - How do I plot data points that are very close together in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM