-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding stat_joy, Lincoln weather data, start for a vignette
- Loading branch information
1 parent
bab95c7
commit bb1077b
Showing
9 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ Suggests: | |
knitr | ||
VignetteBuilder: knitr | ||
Collate: | ||
'data.R' | ||
'ggjoy.R' | ||
'geoms.R' | ||
'stats.R' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
``` |