nsgrantham / ggbraid

Braided ribbons in ggplot2
https://nsgrantham.github.io/ggbraid/
Other
78 stars 1 forks source link

error in facet_grid when exist NA values, dissapear if replace with zero (example) #18

Closed hectorsegal closed 2 years ago

hectorsegal commented 2 years ago

test.csv library(tidyverse) library(ggbraid) library(rio) directorio<-"E:/dato2021/desarrollo/ensayo_ggbraid" setwd(directorio) data <- rio::import("test.csv") data %>% mutate(lubridate::ymd(date,'%Y-%m-%d')) glimpse(data) df_long <- pivot_longer(data, cols=-1, names_to="key",values_to="value") %>% arrange(key) ggplot(df_long, aes(x=date,y=value)) + geom_path() + geom_hline(yintercept = 0) + geom_hline(aes(yintercept = 1) ,linetype="dotted")+ geom_hline(aes(yintercept = -1) ,linetype="dotted")+ geom_hline(aes(yintercept = 2) ,linetype="dashed")+ geom_hline(aes(yintercept = -2) ,linetype="dashed")+ geom_braid( aes(ymin = value, ymax = 0, fill=value < 0),na.rm=FALSE, method = "line", alpha=0.3 )+

facet_grid( key ~ .,scales="free_y")+ theme_bw()+ scale_fill_manual(values = c("red", "blue")) + guides(fill = "none")+ labs( title="titulo", subtitle=paste0("subtitulo"))+ ylab("index")+xlab("Date")+ theme(strip.text.y = element_text(angle = 0, hjust = 0),legend.position='none')

############################### Error!

Error in scale_apply():

! scale_id must not be NA

##############################

If select one column (one site)

ggplot(df_long[260:518,], aes(x=date,y=value)) + geom_path() + geom_hline(yintercept = 0) + geom_hline(aes(yintercept = 1) ,linetype="dotted")+ geom_hline(aes(yintercept = -1) ,linetype="dotted")+ geom_hline(aes(yintercept = 2) ,linetype="dashed")+ geom_hline(aes(yintercept = -2) ,linetype="dashed")+ geom_braid( aes(ymin = value, ymax = 0, fill=value < 0),na.rm=FALSE, method = "line", alpha=0.3 )+

facet_grid( key ~ .,scales="free_y")+ theme_bw()+ scale_fill_manual(values = c("red", "blue")) + guides(fill = "none")+ labs( title="titulo", subtitle=paste0("subtitulo"))+ ylab("index")+xlab("Date")+ theme(strip.text.y = element_text(angle = 0, hjust = 0),legend.position='none')

If replace NA by 0

df_long1 <- df_long df_long1[is.na(df_long1$value),3] <- 0 ggplot(df_long1, aes(x=date,y=value)) + geom_path() + geom_hline(yintercept = 0) + geom_hline(aes(yintercept = 1) ,linetype="dotted")+ geom_hline(aes(yintercept = -1) ,linetype="dotted")+ geom_hline(aes(yintercept = 2) ,linetype="dashed")+ geom_hline(aes(yintercept = -2) ,linetype="dashed")+ geom_braid( aes(ymin = value, ymax = 0, fill=value < 0),na.rm=FALSE, method = "line", alpha=0.3 )+

facet_grid( key ~ .,scales="free_y")+ theme_bw()+ scale_fill_manual(values = c("red", "blue")) + guides(fill = "none")+ labs( title="titulo", subtitle=paste0("subtitulo"))+ ylab("index")+xlab("Date")+ theme(strip.text.y = element_text(angle = 0, hjust = 0),legend.position='none')

is OK!

nsgrantham commented 2 years ago

Hi @hectorsegal, thanks for opening this issue and providing everything I need to reproduce the problem.

I found the source of the problem — my code was not properly applying the na.rm action to the faceted grid panels individually, leading to the scale_id error.

I've pushed a fix to the fix-facet-grid-na-error branch here: https://github.com/nsgrantham/ggbraid/tree/fix-facet-grid-na-error. Please try out this new branch and let me know if this fixes your problem. (To do so, run remove.packages("ggbraid") and then devtools::install_github("nsgrantham/ggbraid@fix-facet-grid-na-error"))

Here's code that will produce the correct result:

library(tidyverse)
library(ggbraid)

df_long <- "https://github.com/nsgrantham/ggbraid/files/9417892/test.csv" %>%
  read_delim(delim = ";") %>%
  pivot_longer(site_A:site_C, names_to = "key", values_to = "value")

ggplot(df_long, aes(x = date, y = value)) +
  geom_path() +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = 1, linetype = "dotted") +
  geom_hline(yintercept = -1, linetype = "dotted") +
  geom_hline(yintercept = 2, linetype = "dashed") +
  geom_hline(yintercept = -2, linetype = "dashed") +
  geom_braid(
    aes(ymin = value, ymax = 0, fill = value < 0),
    na.rm = FALSE,
    method = "line",
    alpha = 0.3
  ) +
  facet_grid(key ~ ., scales = "free_y") +
  scale_fill_manual(values = c("red", "blue")) +
  guides(fill = "none")+
  theme_bw() +
  theme(strip.text.y = element_text(angle = 0, hjust = 0))
Screen Shot 2022-08-31 at 12 10 12 AM
hectorsegal commented 2 years ago

¡Excelent! Thank you