Skip to content

Commit

Permalink
stat_joy improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 24, 2017
1 parent 9e3a3c4 commit eab8643
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
13 changes: 13 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ggjoy 0.2.0
----------------------------------------------------------------
Numerous improvements:
- New stat `binline` that can be used to draw histogram joyplots.
- Various improvements in `stat_joy`. In particular, it now works properly
with multiple panels. It also now has parameters `from` and `to` to limit the
range of density estimation, just like `density()`.
- Improvements in the vignettes
- New geoms `geom_ridgeline_gradient` and `geom_joy_gradient` that can handle gradient fills.

ggjoy 0.1.0
----------------------------------------------------------------
First complete implementation ready for initial release
29 changes: 17 additions & 12 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#'
#' @param geom The geometric object to use to display the data.
#' @param bandwidth Bandwidth used for density calculation. If not provided, is estimated from the data.
#' @param from,to The left and right-most points of the grid at which the density is to be estimated,
#' as in [density()]. If not provided, there are estimated from the data range and the bandwidth.
#' @inheritParams geom_ridgeline
#' @importFrom ggplot2 layer
#' @export
stat_joy <- function(mapping = NULL, data = NULL, geom = "joy",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, bandwidth = NULL, ...)
inherit.aes = TRUE, bandwidth = NULL, from = NULL, to = NULL, ...)
{
layer(
stat = StatJoy,
Expand All @@ -23,7 +25,10 @@ stat_joy <- function(mapping = NULL, data = NULL, geom = "joy",
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(bandwidth = bandwidth, na.rm = na.rm, ...)
params = list(bandwidth = bandwidth,
from = from,
to = to,
na.rm = na.rm, ...)
)
}

Expand All @@ -49,18 +54,18 @@ StatJoy <- ggproto("StatJoy", Stat,
params$bandwidth <- bw
}

if (is.null(params$min)) {
params$min <- min(data$x, na.rm=TRUE) - 3 * params$bandwidth
if (is.null(params$from)) {
params$from <- min(data$x, na.rm=TRUE) - 3 * params$bandwidth
}

if (is.null(params$max)) {
params$max <- max(data$x, na.rm=TRUE) + 3 * params$bandwidth
if (is.null(params$to)) {
params$to <- max(data$x, na.rm=TRUE) + 3 * params$bandwidth
}

data.frame(
bandwidth = params$bandwidth,
min = params$min,
max = params$max
from = params$from,
to = params$to
)
},

Expand All @@ -72,13 +77,13 @@ StatJoy <- ggproto("StatJoy", Stat,

list(
bandwidth = pardata$bandwidth,
min = pardata$min,
max = pardata$max,
from = pardata$from,
to = pardata$to,
na.rm = params$na.rm
)
},

compute_group = function(data, scales, min, max, bandwidth = 1) {
compute_group = function(data, scales, from, to, bandwidth = 1) {
# ignore too small groups
if(nrow(data) < 3) return(data.frame())

Expand All @@ -88,7 +93,7 @@ StatJoy <- ggproto("StatJoy", Stat,
}
panel_id <- as.numeric(panel)

d <- density(data$x, bw = bandwidth[panel_id], from = min[panel_id], to = max[panel_id], na.rm = TRUE)
d <- density(data$x, bw = bandwidth[panel_id], from = from[panel_id], to = to[panel_id], na.rm = TRUE)
data.frame(x = d$x, density = d$y)
}
)
Expand Down
5 changes: 4 additions & 1 deletion man/stat_joy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions vignettes/gallery.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ library(forcats)
Catalan_elections %>%
mutate(Alt = (as.numeric(as.factor(Year))-1) %% 2 + 1) %>%
ggplot(aes(y = as.factor(Year) %>% fct_rev())) +
geom_joy(aes(x = Percent, fill = paste(Option, Alt)), alpha = .8, color = "white") +
geom_joy(aes(x = Percent, fill = paste(Option, Alt)),
alpha = .8, color = "white", from = 0, to = 100) +
labs(x = "Vote (%)",
y = "Election Year",
title = "Indy vs Unionist vote in Catalan elections",
subtitle = "Analysis unit: municipalities (n = 949)",
caption = "Marc Belzunces (@marcbeldata) | Source: Idescat") +
scale_y_discrete(expand = c(0.01, 0)) +
scale_x_continuous(limits = c(0, 100), expand = c(0.01, 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_fill_manual(breaks = c("Indy 1", "Unionist 1"),
labels = c(`Indy 1` = "Indy", `Unionist 1` = "Unionist"),
values = c("#ff0000", "#ff8080", "#0000ff", "#8080ff"),
Expand Down
14 changes: 12 additions & 2 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ ggplot(iris, aes(x = Sepal.Length, y = Species)) +
```


## Theme
## Themes

Joyplots tend to require some theme modifications to look good. Most importantly, the y-axis labels should be vertically aligned so that they are flush with the axis ticks rather than vertically centered. The ggjoy package provides a theme `theme_joy` that does this and a few other theme modifications.
Joyplots tend to require some theme modifications to look good. Most importantly, the y-axis tick labels should be vertically aligned so that they are flush with the axis ticks rather than vertically centered. The ggjoy package provides a theme `theme_joy` that does this and a few other theme modifications.

```{r message=FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_joy() + theme_joy()
Expand All @@ -128,6 +128,16 @@ ggplot(iris, aes(x = Sepal.Length, y = Species)) +
scale_y_discrete(expand = c(0.01, 0))
```

If you prefer to use a different theme than `theme_joy`, for example `theme_minimal`, it is still advisable to adjust the alignment of the axis tick labels and the axis scales.
```{r message=FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_joy() +
theme_minimal(base_size = 14) + theme(axis.text.y = element_text(vjust = 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_y_discrete(expand = c(0.01, 0))
```


## Stats

The default stat used with `geom_joy` is `stat_joy`. However, it may not do exactly what you want it to do, and there are other stats that can be used that may be better for your respective application.
Expand Down

0 comments on commit eab8643

Please sign in to comment.