-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTidyTuesday - 14-7-2020.R
178 lines (131 loc) · 6.73 KB
/
TidyTuesday - 14-7-2020.R
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
163
164
165
166
# Upload data -------------------------------------------------------------
astronauts <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-14/astronauts.csv')
# Upload packages ---------------------------------------------------------
pacman::p_load(readxl, lubridate, tidyverse, ggplot2, hrbrthemes, ggfittext,patchwork,
hrbrthemes, scales,ggtext, ggpubr)
# Prepare the data --------------------------------------------------------
total_occ<-astronauts%>% mutate(age=(year_of_selection - year_of_birth),
age=case_when(
age >= 21 & age <= 30 ~ "21-30",
age >= 31 & age <= 40 ~ "31-40",
age >= 41 & age <= 50 ~ "41-50",
age >= 51 & age <= 60 ~ "51-60"),
age=as_factor(age))%>%
group_by(age,military_civilian) %>%summarize(total=sum(total_number_of_missions)) %>%
mutate(ftotal=total/sum(total),
pcnt=round(ftotal*100, 2),
label = paste0(pcnt, "%"))
# First graph -------------------------------------------------------------
g<-ggplot(total_occ, aes(x = age, y = pcnt, fill = military_civilian)) +
#stacked bar
geom_bar(stat = "identity") +
geom_text(aes(x =age, y = pcnt, label = label), position = position_stack(vjust = 0.5), color="white",family = "Comfortaa", size = 3.5) +
#coord_flip() +
scale_x_discrete(position = "top") +
theme(
plot.title = element_text(margin = margin(b = 1),
color = "#22222b",face = "bold",size = 9,
hjust = 0.5,
family = "Arial"),
plot.subtitle = element_text(margin = margin(t=5,b = 1),
color = "#22222b", size = 7, family = "Arial",
hjust = 0.5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.background = element_rect(fill = "#f7f7f7", color = NA),
legend.position = "bottom",
plot.background = element_rect(fill = "#f7f7f7", color = NA), # color removes the border
plot.margin = unit(c(1, 1, 1, 1), "cm"),
axis.ticks = element_blank(),
axis.text.x = element_text(family = "Comfortaa", color="#22211d",
size=10,face = "bold")
) +
guides(fill = guide_legend(title = "Species",
title.position = "top",
title.hjust = 0.5,
label.position = "bottom",
keywidth = 3, keyheight = 0.5)) +scale_fill_manual(values = c("#a0c4a9", "#e6d492"))
g_first_graph<-g+
annotate(
geom = "text", x = 1, y = 105, label = "737 total missions",
hjust = "middle", colour = "black", size = 3
)+
annotate(
geom = "text", x = 2, y = 105, label = "2,858 total missions",
hjust = "middle", colour = "black", size = 3
) +
annotate(
geom = "text", x = 3, y = 105, label = "201 total missions",
hjust = "middle", colour = "black", size = 3
) +
annotate(
geom = "text", x = 4, y = 105, label = "13 total missions",
hjust = "middle", colour = "black", size = 3
)
# Second graph ------------------------------------------------------------
total_nationality<-astronauts%>% mutate(age=(year_of_selection - year_of_birth),
nationality=case_when(
nationality == "U.S." ~ "United States",
nationality == "U.S.S.R/Russia" ~ "U.S.S.R/Russia",
TRUE ~ "Others"))%>%
group_by(nationality,military_civilian) %>%summarize(total=sum(total_number_of_missions))
gnationality<-ggplot(total_nationality, aes(x = nationality, y = total, fill = military_civilian)) +
#stacked bar
geom_bar(width= 0.4,stat = "identity",position = 'stack') +
geom_text(aes(x =nationality,
y = total, label = comma_format()(total)), position = position_stack(vjust = 0.5), color="white", family = "Comfortaa", size = 3.5)+
coord_flip() +
theme(
plot.title = element_text(margin = margin(b = 1),
color = "#22222b",face = "bold",size = 9,
hjust = 0.5,
family = "Arial"),
plot.subtitle = element_text(margin = margin(t=5,b = 1),
color = "#22222b", size = 7, family = "Arial",
hjust = 0.5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
##axis.text.y = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.background = element_rect(fill = "#f7f7f7", color = NA),
legend.position = "bottom",
plot.background = element_rect(fill = "#f7f7f7", color = NA), # color removes the border
plot.margin = unit(c(2, 2, 2, 2), "cm"),
axis.ticks = element_blank(),
axis.text.y = element_text(family = "Comfortaa", color="#22211d",
size=10,face = "bold")
) +
guides(fill = guide_legend(title = "Species",
title.position = "top",
title.hjust = 0.5,
label.position = "bottom",
keywidth = 3, keyheight = 0.5)) +scale_fill_manual(values = c("#a0c4a9", "#e6d492")) +
annotate(
geom = "text", x = 1, y = 2000, label = "Total number of missions: \n3809",
hjust = "middle", colour = "grey", size = 7
)
# Multiple plots ----------------------------------------------------------
ggarrange(g_first_graph,gnationality, ncol=2, nrow=1, common.legend = TRUE, legend="bottom") +
theme_ipsum() +
labs(x = "",y = "",
title = "Astronaut Database - Analysis of the total number of missions",
subtitle = "",
caption = "Source:Tidy Tuesday\nVisualization: JuanmaMN (Twitter @Juanma_MN)") +
theme(
plot.title = element_text(margin = margin(b = 8),
color = "#22222b",face = "bold",size = 14,
hjust = 0.5,
family = "Arial"),
plot.background = element_rect(fill = "#f7f7f7", color = NA))