Last active
August 10, 2019 12:31
-
-
Save uribo/663e1812b456041b66a12d8794c195a3 to your computer and use it in GitHub Desktop.
Go Andoさんのおしゃれな図を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: Go Ando (@goando) https://twitter.com/goando/status/1159674083363053568 | |
# 1枚目 --------------------------------------------------------------------- | |
library(ggrepel) | |
library(ggchicklet) | |
library(tidyverse) | |
df_label <- | |
tibble( | |
x = c(6, 20), | |
y = c(215, 40), | |
label = c("日の出", "日の入")) | |
df_dummy <- tibble( | |
hour = as_factor(seq.int(0, 23)), | |
count = c(24, 46, 51, 53, 114, 218, 204, 115, 106, 72, 71, 93, 90, 77, 33, 30, 24, 22, 24, 25, 51, 34, 19, 20), | |
grp = c(rep("A", times = 5), | |
"B", | |
rep("C", 13), | |
"B", | |
rep("A", 4))) | |
p_base <- | |
ggplot() + | |
geom_chicklet(data = df_dummy, | |
aes(hour, count, group = grp, fill = grp), | |
width = 0.85, radius = grid::unit(8, "pt"), color = "transparent") + | |
scale_fill_manual(values = c("A"= "#5E5E5F", | |
"B" = "#E39C4D", | |
"C" = "#E7BD5B")) + | |
guides(fill = FALSE) + | |
xlab("時刻") + | |
ylab("人数") + | |
labs(title = "登り始めの時間の分布", | |
caption = "@goandoさんの図を元に\n@uriboがggplot2で再現したものです。データはダミー。") + | |
scale_y_continuous(limits = c(0, 250), expand = expansion(add = c(0, 20))) | |
p_out <- | |
p_base + | |
geom_label_repel(data = df_label, aes(x = x, y = y, label = label), family = "IPAexGothic", | |
nudge_x = 0, nudge_y = 4, | |
fill = "#EAE7DD", | |
size = 3.8, | |
box.padding = 2, | |
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "last"), | |
direction = "y", segment.color = "#92908E") + | |
theme(text = element_text(family = "YuGo-Bold", colour = "#92908E"), | |
plot.background = element_rect(fill = "#F2EFE5"), | |
panel.background = element_rect(fill = "#F2EFE5"), | |
plot.title = element_text(family = "YuGo-Bold", size = 32, hjust = 0.5, colour = "#5E5E5F"), | |
axis.title = element_text(face = "bold", size = 16, colour = "#92908E"), | |
axis.title.x = element_text(hjust = 0.99), | |
axis.title.y = element_text(vjust = 1.0, hjust = 0.5, angle = 360), | |
axis.ticks = element_blank(), | |
axis.text = element_text(size = 14, face = "bold", colour = "#92908E", family = "Arial Narrow"), | |
axis.line.x = element_blank(), | |
panel.grid.major.y = element_line(colour = "#92908E", linetype = "dotted", size = 0.5), | |
panel.grid.minor.y = element_blank(), | |
panel.grid.major.x = element_blank()) | |
ggsave("plot1.png", p_out, | |
width = 7, | |
height = 5.2, | |
dpi = 300) |
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
# 4枚目 ----------------------------------------------------------------------- | |
library(ggforce) | |
p_out <- | |
iris %>% | |
ggplot(aes(Petal.Length, Petal.Width)) + | |
geom_mark_circle(aes(fill = Species, label = Species), label.fill = "#F8F7F9") + | |
scale_fill_manual(values = c("setosa" = "#D4E1EF", "versicolor" = "#D4E1EF", "virginica" = "#D4E1EF")) + | |
geom_point(shape = 21, color = "white", stroke = 0.2, fill = "#69A3E6", size = 2.5) + | |
guides(fill = FALSE) + | |
scale_y_continuous(limits = range(iris$Petal.Width), expand = expansion(add = c(1, 1.2))) + | |
theme(axis.title = element_text(face = "bold", size = 14, colour = "#92908E"), | |
text = element_text(family = "YuGo-Bold", colour = "#92908E"), | |
axis.ticks = element_blank(), | |
panel.grid.major.y = element_line(colour = "#92908E", size = 0.2), | |
panel.grid.minor.y = element_blank(), | |
axis.text = element_text(size = 12, face = "bold", colour = "#92908E", family = "Arial Narrow"), | |
plot.background = element_rect(fill = "#F8F7F9"), | |
panel.background = element_rect(fill = "#F8F7F9"), | |
axis.line.x = element_blank(), | |
panel.grid.major.x = element_blank()) | |
ggsave("plot2.png", p_out, | |
width = 7, | |
height = 5.2, | |
dpi = 300) |
Author
uribo
commented
Aug 9, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment