R语言 将标准差显示为半透明的连续图层

carvr3hs  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(118)

为了创建这个图,我使用了以下代码:

test_data %>%
  mutate(dot_color = ifelse(row_number() == 13, "red", "black")) %>%
  ggplot(aes(x = month, y = mean_score)) +
  geom_line(aes(group = 1), size = 1, color = "gray40") +
  geom_point(aes(color = dot_color), size = 5) +
  geom_errorbar(aes(ymin = mean_score - sd(mean_score), ymax = mean_score + sd(mean_score)), width = 0.2) +
  scale_color_identity() +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  xlab("Month") +
  ylab("Mean Score") +
  ggtitle("Mean Score Over Time") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

如何更改标准差的显示?我希望它显示为半透明的连续图层,而不仅仅是每个点上的一条线。
提前感谢您的帮助。
最好
马苏皮拉米
dput(test_data)在控制台中给出以下输出:

structure(list(month = c("2010_09", "2010_10", "2010_11", "2010_12", 
"2011_01", "2011_02", "2011_03", "2011_04", "2011_05", "2011_06", 
"2011_07", "2011_08", "2011_09", "2011_10", "2011_11", "2011_12", 
"2012_01", "2012_02", "2012_03", "2012_04", "2012_05", "2012_06", 
"2012_07", "2012_08", "2012_09"), mean_score = c(-0.0410314783067388, 
-0.058019985508201, -0.0356711755233494, -0.0372229691095461, 
-0.0449221657669101, -0.0361966978350602, -0.0528777079508544, 
-0.0421520168683535, -0.0371713600130145, -0.0420191475470994, 
-0.0518498479566862, -0.049552355942377, -0.06296103117506, -0.0564000134084205, 
-0.0417076530269435, -0.0583660412336386, -0.0552066721949353, 
-0.0504569837526206, -0.0327654798534799, -0.0407763154279434, 
-0.0474108095497317, -0.0474328848540265, -0.0450817442038999, 
-0.0473148373202061, -0.0497317904374364)), row.names = c(NA, 
-25L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000198c01bbb40>)
gdrx4gfi

gdrx4gfi1#

您可以使用与geom_errorbar()几乎相同的参数来使用geom_ribbon()

test_data %>%
  mutate(dot_color = ifelse(row_number() == 13, "red", "black")) %>%
  ggplot(aes(x = month, y = mean_score)) +
  geom_line(aes(group = 1), size = 1, color = "gray40") +
  geom_point(aes(color = dot_color), size = 5) +
  geom_ribbon(aes(ymin = mean_score - sd(mean_score), 
                  ymax = mean_score + sd(mean_score), 
                  group = 1), alpha = 0.3) +
  scale_color_identity() +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  xlab("Month") +
  ylab("Mean Score") +
  ggtitle("Mean Score Over Time") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

相关问题