forked from clauswilke/dataviz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizing_amounts.Rmd
50 lines (39 loc) · 2.1 KB
/
visualizing_amounts.Rmd
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
```{r echo = FALSE, message = FALSE}
# run setup script
source("_common.R")
```
# Visualizing amounts
(ref:titanic-passengers-by-class-sex) Numbers of female and male passengers on the Titanic traveling in 1st, 2nd, and 3rd class.
```{r titanic-passengers-by-class-sex, fig.width = 5.5, fig.cap = '(ref:titanic-passengers-by-class-sex)'}
titanic_groups <- titanic_all %>% filter(class != "*") %>%
select(class, sex) %>%
group_by(class, sex) %>%
tally() %>% arrange(class, desc(sex)) %>%
mutate(sex = factor(sex, levels = c("female", "male"))) %>%
group_by(class) %>%
mutate(nlabel = cumsum(n) - n/2) %>%
ungroup() %>%
mutate(class = paste(class, "class"))
ggplot(titanic_groups, aes(x = class, y = n, fill = sex)) +
geom_col(position = "stack", color = "white", size = 1, width = 1) +
geom_text(aes(y = nlabel, label = n), color = "white", size = 14/.pt) +
scale_x_discrete(expand = c(0, 0), name = NULL) +
scale_y_continuous(expand = c(0, 0), breaks = NULL, name = NULL) +
scale_fill_manual(values = c("#D55E00", "#0072B2"),
breaks = c("female", "male"),
labels = c("female passengers ", "male passengers"),
name = NULL) +
theme_minimal_grid() +
theme(panel.grid.major = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(size = 14),
#legend.position = c(0.13, 0.6),
#legend.justification = c(0, 0),
legend.position = "bottom",
legend.justification = "center",
legend.background = element_rect(fill = "white"))
```
I have also added the actual numerical values that each bar represents. Whenever your plot is meant to display only a small number of key values, it makes sense to add the actual numbers to the plot. This substantially increases the amount of information conveyed by your plot without adding much visual noise.
Mention that class cannot be reordered, because it is an ordered factor and it has its own intrinsic order.
## Don't use a logscale y axis with a bargraph
You can use either a bargraph or a logscale y axis, but not both in combination.