Skip to content

Commit

Permalink
adding stat_joy, Lincoln weather data, start for a vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 13, 2017
1 parent bab95c7 commit bb1077b
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Suggests:
knitr
VignetteBuilder: knitr
Collate:
'data.R'
'ggjoy.R'
'geoms.R'
'stats.R'
Expand Down
14 changes: 14 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Weather in Lincoln, Nebraska in 2016.
#'
#' A dataset containing weather information from Lincoln, Nebraska, from 2016.
#' Originally downloaded from Weather Underground by Austin Wehrwein, http://austinwehrwein.com/.
#' The variables are as follows:
#'
#' @format A tibble with 366 rows and 24 variables:
#' \describe{
#' \item{CST}{Day of the measurement}
#' \item{Max Temperature [F]}{Maximum temperature in Fahrenheit}
#' \item{Mean Temperature [F]}{Maximum temperature in Fahrenheit}
#' \item{Month}{The month in which the measurement was taken}
#' }
"lincoln_weather"
61 changes: 61 additions & 0 deletions R/stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Code for stat_joy based on stat_density_common in the "extending ggplot2" vignette

#' Stat for density joyplots
#'
#' This stat is the default stat used by \code{geom_joy}. It is very similar to \code{stat_density},
#' however there are a few differences. Most importantly, the density bandwidth is chosen across
#' the entire dataset.
#'
#' @importFrom ggplot2 layer
#' @export
stat_joy <- function(mapping = NULL, data = NULL, geom = "joy",
position = "identity", na.rm = TRUE, show.legend = NA,
inherit.aes = TRUE, bandwidth = NULL, ...)
{
layer(
stat = StatJoy,
data = data,
mapping = mapping,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(bandwidth = bandwidth, na.rm = na.rm, ...)
)
}


#' @importFrom ggplot2 ggproto Stat
#' @export
StatJoy <- ggproto("StatJoy", Stat,
required_aes = "x",
default_aes = aes(height = ..density..),

setup_params = function(data, params) {
if (is.null(params$bandwidth)) {
xdata <- na.omit(data.frame(x=data$x, group=data$group))
xs <- split(xdata$x, xdata$group)
bws <- vapply(xs, bw.nrd0, numeric(1))
bw <- mean(bws, na.rm = TRUE)
message("Picking joint bandwidth of ", signif(bw, 3))

params$bandwidth <- bw
}

min <- min(data$x, na.rm=TRUE) - 3 * params$bandwidth
max <- max(data$x, na.rm=TRUE) + 3 * params$bandwidth

list(
bandwidth = params$bandwidth,
min = min,
max = max,
#na.rm = params$na.rm
na.rm = TRUE
)
},

compute_group = function(data, scales, min, max, bandwidth = 1, na.rm = TRUE) {
d <- density(data$x, bw = bandwidth, from = min, to = max, na.rm)
data.frame(x = d$x, density = d$y)
}
)
7 changes: 7 additions & 0 deletions data-raw/lincoln-weather.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require(readr)

lincoln_weather <- read_csv("data-raw/lincoln-weather.csv")
lincoln_weather$Month<-months(as.Date(lincoln_weather$CST))
lincoln_weather$Month<-factor(lincoln_weather$Month, levels=rev(unique(lincoln_weather$Month)))

devtools::use_data(lincoln_weather, overwrite = TRUE)
367 changes: 367 additions & 0 deletions data-raw/lincoln-weather.csv

Large diffs are not rendered by default.

Binary file added data/lincoln_weather.rda
Binary file not shown.
22 changes: 22 additions & 0 deletions man/lincoln_weather.Rd

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

15 changes: 15 additions & 0 deletions man/stat_joy.Rd

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

44 changes: 44 additions & 0 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: "Introduction to ggjoy"
author: "Claus O. Wilke"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
fig_width: 5
vignette: >
%\VignetteIndexEntry{Introduction to ggjoy}
%\VignetteEngine{knitr::rmarkdown}
%\usepackage[utf8]{inputenc}
---

Basic usage:
```{r}
library(ggplot2)
library(ggjoy)
ggplot(iris, aes(x=Sepal.Length, y=Species, group=Species)) + geom_joy()
```


## Gallery of examples

Temperatures in Lincoln, Nebraska. Modified from a [blog post](http://austinwehrwein.com/data-visualization/it-brings-me-ggjoy/) by Austin Wehrwein.
```{r message=FALSE, fig.width = 7.5, fig.height = 5}
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`)) +
geom_joy(scale = 3, rel_min_height = 0.01) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_y_discrete(expand = c(0.01, 0)) +
labs(title = 'Temperatures in Lincoln NE',
subtitle = 'Mean temperatures (Fahrenheit) by month for 2016\nData: Original CSV from the Weather Underground') +
theme_joy(font_size = 13, grid = T) + theme(axis.title.y = element_blank())
```

Evolution of movie lengths over time. Data from the IMDB, as provided in the ggplot2movies package.
```{r message=TRUE, fig.width = 5, fig.height = 6}
library(ggplot2movies)
ggplot(movies[movies$year>1912,], aes(x=length, y=year, group=year)) +
geom_joy(scale=10, size=0.25, rel_min_height=0.03) +
theme_joy() +
scale_x_continuous(limits=c(1, 200), expand=c(0.01, 0)) +
scale_y_reverse(breaks=c(2000, 1980, 1960, 1940, 1920, 1900), expand=c(0.01, 0))
```

0 comments on commit bb1077b

Please sign in to comment.