I'd like to plot my error bars based on my SD values from each of my variables. I have tried few things (see bottom) but I believe there is a way to plot individual sd for each data point for each variable?
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))+
I have tried the following but I think maybe the preoblem is re-structuring the data?
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
- Note I have create a df with data only and one with data+sd
data_sd
which is the one we want to use.