Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active July 22, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutannihilation/891f5a7dd8ea876efc4f7e9f0641051e to your computer and use it in GitHub Desktop.
Save yutannihilation/891f5a7dd8ea876efc4f7e9f0641051e to your computer and use it in GitHub Desktop.
第63回 Tokyo.RのLTのスライド用のコード
library(ggjoy)
library(dplyr)
library(purrr)
# data ------------------------------------------------------------
set.seed(10)
l <- rerun(26, rnorm(1000, mean = runif(1), sd = sqrt(runif(1))))
names(l) <- LETTERS
# Bだけは山が2つある分布
l[[2]] <- c(rnorm(500, mean = -1, sd = 0.3), rnorm(500, mean = 1, sd = 0.3))
d <- l %>%
map_df(~ list(value = .), .id = "id")
# 3つしかないバージョンのデータ
x <- d %>%
dplyr::filter(id %in% c("A", "B", "C"))
# plot ---------------------------------------------------------
# まとめてdensity plot
ggplot(x, aes(value)) +
geom_density(fill = alpha("skyblue4", 0.9)) +
theme_minimal()
# fillで区別
ggplot(x, aes(value)) +
geom_density(aes(fill = id), alpha = 0.7) +
theme_minimal()
# fillで区別するには多すぎる
ggplot(d, aes(value)) +
geom_density(aes(fill = id), alpha = 0.7) +
theme_minimal()
# 箱ひげ図
ggplot(d, aes(id, value)) +
geom_boxplot() +
theme_minimal()
# violin
ggplot(d, aes(id, value)) +
geom_violin(fill = alpha("skyblue4", 0.9)) +
theme_minimal()
# facet_wrap
ggplot(d, aes(value)) +
geom_density(fill = alpha("skyblue4", 0.9)) +
theme_minimal() +
facet_wrap(~ id)
# facet_grid
ggplot(d, aes(value)) +
geom_density(fill = alpha("skyblue4", 0.9)) +
theme_minimal() +
facet_grid(id ~ .)
# goem_joy
ggplot(d, aes(value, id)) +
geom_joy(fill = alpha("skyblue4", 0.9), colour = "white", scale = 3) +
theme_minimal()
# scale = 10
ggplot(d, aes(value, id)) +
geom_joy(fill = alpha("skyblue4", 0.9), colour = "white", scale = 10) +
theme_minimal()
# 交互に色を付ける
d_with_fill <- d %>%
# 交互に色がskyblue3, skyblue4になるようにする
mutate(fill = if_else(group_indices(., id) %% 2 == 0,
alpha("skyblue3", 0.9),
alpha("skyblue4", 0.9)))
ggplot(d_with_fill, aes(value, id, fill = fill)) +
geom_joy(colour = "white", scale = 10) +
scale_fill_identity() +
theme_minimal()
# おまけ:joystogram -------------------------------------------------
ggplot(d_with_fill, aes(value, id, fill = fill)) +
geom_joy(colour = "white", scale = 10, stat = "binline") +
scale_fill_identity() +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment