I am trying to use tidyverse more and and pivot_longer to later plot error bars over geom_errorbar (one dashed and one solid), but I believe I am not building the long format right or somehow the error_bars aren't well connected with the function.
data<- data.frame(
Date=(1:12),
A=c(0,0,0,28.0,20.6,15.8,38.9,9.5,16.2,20.6,22.6,15.1),
B= c(0,0,0,9.1,6.0,4.76,12.7,4.3,5.7,8.0,9.01,6.5),
A_sd= c(0,0,0,6.3,11.4,1.49,6.96,1.23,1.4,1.15,6.3,1.2),
B_sd=c(0,0,0,1.90,3.1,0.24,1.8,1.16,0.4,1.4,2.4,0.5))
LINETYPE1 <- c("A" = "solid", "B" = "dashed")
library(tidyverse)
data %>%
select(A, B, A_sd, B_sd, Date) %>%
pivot_longer(cols = -Date, -A_sd, -B_sd) %>%
ggplot(aes(Date, value, linetype = name)) +
geom_line() +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~./10, name= expression(paste("B", " (units)"))))+
scale_linetype_manual(name= "", values= LINETYPE1, labels=c(bquote(A ~ (pg ~cell ^-1)), bquote(B ~ (units)))) +
scale_x_datetime(labels = date_format("%b"))+
theme_classic()+
ylab(bquote(A ~ (units)))+
geom_errorbar(aes(ymin = A, ymax = A +A_sd),
position = position_dodge2(padding = 0.6, width = 0.5))+
geom_errorbar(aes(ymin = B, ymax = B+B_sd),
position = position_dodge2(padding = 0.6, width = 0.5))+
xlab("")
This now gives the error:
Error in build_longer_spec(data, !!cols, names_to = names_to, values_to = values_to, : object 'B_sd' not found

