我想根据我的每个变量的 SD 值绘制我的误差线。我尝试了几件事(见底部),但我相信有一种方法可以为每个变量的每个数据点绘制单独的 sd?
Location = c(1,2,3,4,5,6,7)
A = c(1.23, 0.95,0.65,0.74,0.51,0.34,0.28)
B = c(6.77,7.56,3.88,6.52,4.38,11.94,14.97)
C = c(75.45,86.66,103.36,123.2,107.53,128.9,128.49)
SD_A =c(0.10,0.03,0.01,0.05,0.00,0.01,0.02)
SD_B=c(0.02,1.05,1.97,1.45,0.60,1.88,1.45)
SD_C = c(3.56,7.46,26.1,10,10.8,10,29.03)
data = data.frame(Location, A, B, C)
data_sd = data.frame(Location, A, B, C, SD_A, SD_B, SD_C)
library(ggplot2)
library(tidyr)
library(dplyr)
data %>% pivot_longer(.,-Location, names_to = "var",values_to = "val") %>%
filter(!is.na(val)) %>%
mutate(NewVar = var) %>%
add_row(., Location = c(1,1),
var = c("B","B"),
val = c(0,30),
NewVar = c("Out","Out")) %>%
ggplot(aes(x = Location, y = val, group = NewVar))+
geom_point(aes(Location, val, shape=var), size =2) +
ylab("")+
facet_wrap(.~var, strip.position = "left", ncol = 1, scales = "free_y", labeller = as_labeller(c(A= "A", B= "B", C= "C")))+
theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), strip.background = element_blank(),
strip.placement = "outside")+
scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") +scale_shape_manual(values=c(19, 0, 15))+
我尝试了以下方法,但我认为问题可能是重构数据?
geom_errorbar(aes(ymin = var-sd, ymax = var+sd),width = 0.2) # I do not have a sd column
geom_errorbarh(aes(xmin = xmin,xmax = xmax)) # I don't want xmax and xmin since the sd are provided already
- 请注意,我创建了一个仅包含数据的 df 和一个包含 data+sd 的 df,
data_sd
这是我们要使用的。