-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacked bar plots.R
41 lines (35 loc) · 1.37 KB
/
stacked bar plots.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
#------------------
# Stacked bar plots
#------------------
data(diamonds)
diamonds2 <- diamonds %>%
group_by(cut) %>%
count(color) %>%
mutate(percentage = n/nrow(diamonds) * 100) %>%
rename(nobservations = n)
p1 <- ggplot(diamonds2, aes(x = cut, y = percentage, fill = color)) +
geom_bar(stat = 'identity') +
coord_flip() +
labs(title = 'Stacked bar plot with percentages',
subtitle = 'Diamonds dataset',
y="percentage of color", x="cut") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=10, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
p2 <- ggplot(diamonds2, aes(x = cut, y = nobservations, fill = color)) +
geom_bar(stat = 'identity') +
coord_flip() +
labs(title = 'Stacked bar plot with counts',
subtitle = 'Diamonds dataset',
y="count of color", x="cut") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=10, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
grid.arrange(p1, p2, nrow = 2)
#----
# end
#----