nsgrantham / ggbraid Goto Github PK
View Code? Open in Web Editor NEWBraided ribbons in ggplot2
Home Page: https://nsgrantham.github.io/ggbraid/
License: Other
Braided ribbons in ggplot2
Home Page: https://nsgrantham.github.io/ggbraid/
License: Other
Expand the required aesthetics of stat_braid()
from c("x", "ymin", "ymax")
to c("x|y", "ymin|xmin", "ymax|xmax")
using the internal has_flipped_aes()
and flip_data()
functions from ggplot2
Hi and thanks for ggbraid, it's a very useful package
I'm facing a problem with setting the y axis limits. My code looks like
datos.2022 %>% ggplot(aes(x=fecha)) +
geom_line(aes(y=SST.2022), color = "gray8", size = 1) + geom_line(aes(y=SST.mean)) +
geom_braid(aes(y=fecha, ymin = SST.mean, ymax = SST.2022, fill = SST.mean < SST.2022)) +
scale_fill_manual(guide = "none", values=c("blue","red")) +
guides(linetype = "none", fill = "none") +
scale_y_continuous(limits = c(10,30))
But this gives the following error messages
`geom_braid()` using method = 'line'
Warning messages:
1: In max(data$group) : no non-missing arguments to max; returning -Inf
2: Computation failed in `stat_braid()`
Caused by error in `seq.default()`:
! 'to' must be a finite number
and this plot
Everything works fine if I remove the scale_y_continuous
line and the y limits are set by ggplot.
Any solution for this?
Thanks
I discovered this error when trying to solve #18 — at first I thought it was related, but it appears to be its own thing.
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(filter(df_long, key == "site_A"), aes(x = date, y = value)) +
geom_braid(
aes(ymin = value, ymax = 0, fill = value < 0),
na.rm = NA, # the default
method = "line",
alpha = 0.3
)
Error in if (!na.rm) { : missing value where TRUE/FALSE needed
At the moment I do not know what's causing this, I will have to dig in further.
Hi, Neal! I like your work. ggbraid package is really good. Didn't you consider categorical independent variable? When I put numbers instead of months it works seamlessly. But when I put names of months (as an ordered factor) it does not work. What did I do wrong?
Here is my data.
df_long <- tibble(
x = c(month.abb, month.abb),
y = c(57.6085, 47.78275, 50.84225, 47.7515, 51.35225, 44.7345, 45.33275,
44.57025, 42.1275, 50.073, 48.94750, 50.5105, 53.61, 47.128, 48.326,
46.223, 44.68, 47.043, 47.297, 44.008, 48.372, 59.047, 63.438, 67.663),
z = c(rep("Baseline", 12), rep("Current", 12))
)
df_long$x <- factor(df_long$x, levels = month.abb)
library(tidyverse)
library(ggbraid)
df_long <- tibble(
x = c(1:8, 1:8),
y = c(2, 5, 6, 3, 7, 1, 4, 2, 1, 6, 5, 3, 3, 2, 1, 0),
z = c(rep("a", 8), rep("b", 8))
)
df_wide <- pivot_wider(df_long, names_from = z, values_from = y)
df_wide
# A tibble: 16 × 3
x a b
<dbl> <dbl> <dbl>
1 1 2 NA
2 2 5 NA
3 3 6 NA
4 4 3 NA
5 5 7 NA
6 6 1 NA
7 7 4 NA
8 8 2 NA
9 1.5 NA 1
10 2.5 NA 6
11 3.5 NA 5
12 4.5 NA 3
13 5.5 NA 3
14 6.5 NA 2
15 7.5 NA 1
16 8.5 NA 0
ggplot() +
geom_line(aes(x, y, linetype = z), data = df_long) +
geom_braid(aes(x, ymin = a, ymax = b, fill = a <= b), data = df_wide, alpha = 0.6, method = "line", na.rm=TRUE) +
guides(linetype = "none", fill = "none")
Warning messages:
1: Ignoring unknown parameters: direction
2: In max(data$group) : no non-missing arguments to max; returning -Inf
3: Computation failed in `stat_braid()`:
'to' must be a finite number
Currently ggbraid requires that both lines have an observation at every x
. If this is violated, then after pivoting the data wider, there will be an NA
at each x
for which a given line does not have an observation.
I envision two ways to handle this:
geom_braid()
should behave similarly to the way geom_ribbon()
behaves in this situationgeom_braid()
, something like na.impute
, which is FALSE
by default but when TRUE
will impute the NA
s in each line, leading to a properly braided ribbon.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
##############################
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')
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!
Hi, thanks for your work! very useful packages.
Just have a suggestion, when the method sets to 'step', it seems it is incompatible with the direction in geom_step.
For example:
tibble(x = 1:10,
y = 1:10) %>%
ggplot(aes(x = x, y = y)) +
geom_braid(aes(ymax = y + 0.5,
ymin = y - 0.5),
fill = "grey70", method = "step", direction = 'mid') +
geom_step(direction = 'mid')
It produces:
Warning message:
Ignoring unknown parameters: direction
Hi, thanks for your package, it is very useful!
I was wondering if you plan to republish your package to CRAN? Just curious as I use your package in my package, and need to decide if I use the github repo or the CRAN install long term (preferably CRAN 😄 )
Reprex:
set.seed(42)
data <- data.frame(x = 1:10,
y = rnorm(10) + 1:10/10)
library(ggplot2)
library(ggbraid)
ggplot(data, aes(x, y)) +
geom_line() +
geom_braid(aes(ymin = y, ymax = predict(lm(y ~ x)),
fill = predict(lm(y ~ x)) < y))
#> `geom_braid()` using method = 'line'
ggplot(data, aes(x, y)) +
geom_line() +
geom_braid(aes(ymin = y, ymax = predict(lm(y ~ x)),
fill = after_stat(ymin < ymax)))
#> `geom_braid()` using method = 'line'
#> Error in `f()`:
#> ! Aesthetics can not vary with a ribbon
I think the issue is that StatBraid returns the unchanged data if there is no fill
or colour
column so it doesn't add the intersection points.
Created on 2022-03-23 by the reprex package (v2.0.1)
The hoops article at https://nsgrantham.github.io/ggbraid/articles/hoops.html is missing descriptive text to motivate the problem and describe the steps.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.