Home > database >  How to use geom_errorbar after using tidyverse and pivot_longer in R?
How to use geom_errorbar after using tidyverse and pivot_longer in R?

Time:05-16

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

I would love to get something like this enter image description here

CodePudding user response:

Yes, your pivot wasn't quite right. The easiest way to do it from your sample data is to first rename A and B as something like A_mean and B_mean, then use the names_sep method in pivot_longer. That way, you can call:

pivot_longer(-Date, names_sep = "_", names_to = c("group", ".value")) 

To leave you with one column each for Date, group (A/B), mean and sd.

The complete code would be something like this:

data %>%
  select(A, B, A_sd, B_sd, Date) %>%
  rename(A_mean = A, B_mean = B) %>%
  pivot_longer(-Date, names_sep = "_", names_to = c("group", ".value")) %>%
  ggplot(aes(Date, mean, linetype = group))   
  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 = scales::date_format("%b"))  
  theme_classic(base_size = 16)  
  ylab(bquote(A  ~ (units))) 
  geom_errorbar(aes(ymin = mean -sd, ymax = mean   sd), alpha = 0.5)  
  xlab("")

enter image description here

Note that I had to modify your input data slightly to change the Date column to actual dates, otherwise your scale_x_datetime call doesn't work.

  • Related