如何在R中创建带有误差线和原始点的线图?
我正在尝试创建一个类似于此处所做的折线图: [1]:https://community.rstudio。 com/t/problems-with-a-simple-line-graph/75630。但是,我无法复制它。我理解代码,但无法执行我需要的内容。
下面是上述网站的代码:
example %>%
group_by(year) %>%
summarize(mean_val = mean(value),
sd = sd(value)) %>%
ggplot(aes(x=year, y=mean_val)) +
geom_line() +
geom_errorbar(aes(ymin=mean_val - sd, ymax = mean_val + sd), width = .1) +
geom_jitter(data = example, mapping = aes(x=year, y=value), color = "green", width = .1)
下面是我的代码行,试图模仿上面的代码:
example %>%
group_by(Time) %>%
summarize(VAS.panel.ASD,
mean_val = mean(Score),
sd = sd(Score))
下面是我的面板数据的示例:
example <- data.frame(ID = c(22308, 22308, 22308, 30958, 30958, 30958, 34708, 34708, 34708, 36158, 36158, 36158, 37308, 37308, 37308, 43508, 43508, 43508, ),
Score = c(4, 3, 7, 3, 2, 6, 0, 0, 5, 2, 1, 5, 4, 1, 8, 4, 2, 7),
Time = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3))
I'm trying to create a line graph similar to what was done here:
[1]: https://community.rstudio.com/t/problems-with-a-simple-line-graph/75630. However, I'm not able to replicate it. I understand the code, but am not able to execute what I need.
Below is the code form the site mentioned above:
example %>%
group_by(year) %>%
summarize(mean_val = mean(value),
sd = sd(value)) %>%
ggplot(aes(x=year, y=mean_val)) +
geom_line() +
geom_errorbar(aes(ymin=mean_val - sd, ymax = mean_val + sd), width = .1) +
geom_jitter(data = example, mapping = aes(x=year, y=value), color = "green", width = .1)
Below is is my line of code, trying to mimic the code above:
example %>%
group_by(Time) %>%
summarize(VAS.panel.ASD,
mean_val = mean(Score),
sd = sd(Score))
Below is a sample of my panel data:
example <- data.frame(ID = c(22308, 22308, 22308, 30958, 30958, 30958, 34708, 34708, 34708, 36158, 36158, 36158, 37308, 37308, 37308, 43508, 43508, 43508, ),
Score = c(4, 3, 7, 3, 2, 6, 0, 0, 5, 2, 1, 5, 4, 1, 8, 4, 2, 7),
Time = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的代码基于您提供的示例(以及 @stefan 建议),并且我更新了您的示例数据。
示例代码:
绘图:
示例数据:
My code is based on the example you provided (as well on @stefan suggestion), and I updated your sample data.
Sample code:
Plot:
Sample data: