如何在R中创建带有误差线和原始点的线图?

发布于 2025-01-14 02:05:06 字数 1183 浏览 1 评论 0原文

我正在尝试创建一个类似于此处所做的折线图: [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2025-01-21 02:05:06

我的代码基于您提供的示例(以及 @stefan 建议),并且我更新了您的示例数据。

示例代码:

 gg<-example %>%
  group_by(Time) %>%
  summarize(mean_val = mean(Score),
            sd = sd(Score)) %>%  
  ggplot(aes(x=Time, y=mean_val)) +
  geom_line(lwd=2) +
  geom_errorbar(aes(ymin=mean_val - sd, ymax = mean_val + sd), width = .1, lwd=2) +
  geom_jitter(data = example, mapping = aes(x=Time, y=Score), color = "green", width = .2, size=5)


gg+labs(x="Time",y="Mean value")+
      theme_minimal()+
      theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
            axis.title.x = element_text(face="bold", size=16, color="black"),
            axis.text.y = element_text(face="bold", size=12, color="black"),
            axis.title.y = element_text(face="bold", size=16, color="black"),
            strip.text = element_text(size=10, face="bold"),
            plot.title = element_text(size=20, face="bold"))

绘图:

在此处输入图像描述

示例数据:

example<-structure(list(ID = c(22308, 22308, 22308, 30958, 30958, 30958, 
34708, 34708, 34708, 36158, 36158, 36158, 37308, 37308, 37308, 
43508, 43508, 43508), year = c(1992, 1992, 1993, 1993, 1993, 
2000, 2000, 2000, 2005, 2002, 2001, 2005, 2004, 2001, 2008, 2004, 
2002, 2007), 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), Value = c(4, 3, 7, 3, 2, 6, 0, 0, 5, 2, 1, 
5, 4, 1, 8, 4, 2, 7)), class = "data.frame", row.names = c(NA, 
-18L))

My code is based on the example you provided (as well on @stefan suggestion), and I updated your sample data.

Sample code:

 gg<-example %>%
  group_by(Time) %>%
  summarize(mean_val = mean(Score),
            sd = sd(Score)) %>%  
  ggplot(aes(x=Time, y=mean_val)) +
  geom_line(lwd=2) +
  geom_errorbar(aes(ymin=mean_val - sd, ymax = mean_val + sd), width = .1, lwd=2) +
  geom_jitter(data = example, mapping = aes(x=Time, y=Score), color = "green", width = .2, size=5)


gg+labs(x="Time",y="Mean value")+
      theme_minimal()+
      theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
            axis.title.x = element_text(face="bold", size=16, color="black"),
            axis.text.y = element_text(face="bold", size=12, color="black"),
            axis.title.y = element_text(face="bold", size=16, color="black"),
            strip.text = element_text(size=10, face="bold"),
            plot.title = element_text(size=20, face="bold"))

Plot:

enter image description here

Sample data:

example<-structure(list(ID = c(22308, 22308, 22308, 30958, 30958, 30958, 
34708, 34708, 34708, 36158, 36158, 36158, 37308, 37308, 37308, 
43508, 43508, 43508), year = c(1992, 1992, 1993, 1993, 1993, 
2000, 2000, 2000, 2005, 2002, 2001, 2005, 2004, 2001, 2008, 2004, 
2002, 2007), 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), Value = c(4, 3, 7, 3, 2, 6, 0, 0, 5, 2, 1, 
5, 4, 1, 8, 4, 2, 7)), class = "data.frame", row.names = c(NA, 
-18L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文