-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpalmer_penguins.R
More file actions
162 lines (131 loc) · 4.6 KB
/
palmer_penguins.R
File metadata and controls
162 lines (131 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
library(tidyverse)
library(ggplot2)
library(ggridges)
library(cowplot)
library(scales)
library(magick)
library(extrafont)
library(ggpubr)
library(patchwork)
library(rsvg)
#read data
penguins <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv')
#tidy
df_penguins <- penguins %>%
select(species, bill_length_mm:flipper_length_mm,sex) %>%
pivot_longer(bill_length_mm:flipper_length_mm) %>%
drop_na(sex) %>%
mutate(
name = name %>%
str_replace_all("_", " ") %>%
str_remove_all(pattern = " mm") %>%
str_to_title() %>%
paste("(mm)")
) %>%
filter(name == "Bill Length (mm)")
gentoo <- df_penguins %>%
filter(species == "Gentoo")
chinstrap <- df_penguins %>%
filter(species == "Chinstrap")
adelie <- df_penguins %>%
filter(species == "Adelie")
#plots
p1 <- gentoo %>%
ggplot(aes(x = value, fill = sex)) +
geom_histogram(position = "identity",
alpha = 0.7,
bins = 25) +
scale_fill_manual(values = c("#66b3ff", "#8c8c8c"))+
ylab("number of penguins") +
xlab("length (mm)") +
theme_minimal() +
theme(legend.position = "bottom",
legend.text = element_text(size = 11),
legend.title = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_text(color = "white", size = 10),
plot.title = element_text(size = 20),
plot.subtitle = element_text( size = 12, hjust = 1)
)
p1
p2 <- chinstrap %>%
ggplot(aes(x = value, fill = sex)) +
geom_histogram(position = "identity",
alpha = 0.5,
bins = 25) +
scale_fill_manual(values = c("#66b3ff", "#8c8c8c"))+
ylab("number of penguins") +
xlab("length (mm)") +
theme_minimal() +
theme(legend.position = "none",
panel.grid.minor = element_blank(),
axis.title = element_text(color = "white", size = 10),
plot.title = element_text(size = 20),
plot.subtitle = element_text(size = 12, hjust = 1)
)
p2
p3 <- adelie %>%
ggplot(aes(x = value, fill = sex)) +
geom_histogram(position = "identity",
alpha = 0.5,
bins = 25) +
scale_fill_manual(values = c("#66b3ff", "#8c8c8c"))+
theme_minimal() +
ylab("number of penguins") +
xlab("length (mm)") +
theme(legend.position = "none",
panel.grid.minor = element_blank(),
plot.title = element_text( size = 20),
axis.title = element_text(size = 10, color = "grey", face = "italic"),
plot.subtitle = element_text(size = 12, hjust = 1)
)
p3
## Adelie Image
ggdraw() +
draw_image("adelie.png", width = 1.2, height = 1.3) +
draw_text("A D E L I E", y = 1.25, size = 16, family = "Leelawadee UI Semilight") -> adelie_img
## Gentoo Image
ggdraw() +
draw_image("gentoo.png", width = 1.2, height = 1.3) +
draw_text("G E N T O O", y = 1.25, size = 16, family = "Leelawadee UI Semilight") -> gentoo_img
## Chinstrap Image
ggdraw() +
draw_image("chinstrap.png", width = 1.2, height = 1.3) +
draw_text("C H I N S T R A P", y = 1.25, size = 16, family = "Leelawadee UI Semilight") -> chinstrap_img
#grid of plots
image_row <- plot_grid(adelie_img, gentoo_img, chinstrap_img, ncol=3)
#Title and subtitle
ggdraw() +
draw_text("Palmer Penguins Bill Length", x= 0.2, y = 0.85, size = 18, family = "Leelawadee UI") +
draw_text("Palmer Archipelago is a group of islands off the northwestern coast of the Antarctic Peninsula.",
y = 0.64, x = 0.39, size = 12, family = "Leelawadee UI Semilight") +
draw_text("The histograms show that females has shorter bills than males in every species",
y = 0.53, x = 0.33, size = 12, family = "Leelawadee UI Semilight") -> header_plot
#legend on the bottom
row1 <- plot_grid(p3,p1 + theme(legend.position="none"),p2, ncol=3)
#caption
ggdraw() +
draw_text("Visualization: Laura Navarro Soler | Data: Gorman, Williams & Fraser (2014)",
size = 12, x = 0.4, y = 0.2, hjust = 0,
color = "#8c8c8c", family = "Leelawadee UI Semilight") -> p6
#extract legend and plot it separately
legend <- get_legend(
p1 + theme(legend.box.margin = margin(0, 0, 0, 0))
)
#final plot
final_plot <- plot_grid(
header_plot,
image_row,
row1,
legend,
p6,
## plot settings
rel_heights = c(3.1,3.5,4.5,0.7,1),
nrow = 5
)
final_plot
#saving
ggsave("penguinsbill_plot.png",
final_plot,
height = 8, width = 10,
units = "in", dpi = 300)