Skip to content

Commit a209b7b

Browse files
authored
Update dataviz-storytelling.md
1 parent c566a47 commit a209b7b

File tree

1 file changed

+0
-114
lines changed

1 file changed

+0
-114
lines changed

_tutorials/dataviz-storytelling.md

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -978,120 +978,6 @@ ggsave(trends_diet, filename = "trends_diet.png",
978978
height = 5, width = 8)
979979
```
980980

981-
Knowing the sample size for each diet category is another useful bit of information, especially to support the spirit of open and transparent science. We can use `group_by()` and `tally()` to get the sample size numbers.
982-
983-
```r
984-
diet_sum <- bird_models_traits %>% group_by(diet) %>%
985-
tally()
986-
```
987-
988-
Now that we know the numbers, we can visualise them. A barplot would be a classic way to do that, the second option present here - the area graph - is another option. Both can work well depending on the specific occasion, but the area graph does a good job at quickly communicating which categories are overrepresented and which - underrepresented.
989-
990-
```r
991-
(diet_bar <- ggplot(diet_sum, aes(x = diet, y = n,
992-
colour = diet,
993-
fill = diet)) +
994-
geom_bar(stat = "identity") +
995-
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
996-
scale_fill_manual(values = wes_palette("Cavalcanti1")) +
997-
guides(colour = FALSE))
998-
999-
(diet_area <- ggplot(diet_sum, aes(area = n, fill = diet, label = n,
1000-
subgroup = diet)) +
1001-
geom_treemap() +
1002-
geom_treemap_subgroup_border(colour = "white", size = 1) +
1003-
geom_treemap_text(colour = "white", place = "center", reflow = T) +
1004-
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
1005-
scale_fill_manual(values = wes_palette("Cavalcanti1")) +
1006-
guides(fill = FALSE)) # this removes the colour legend
1007-
# later on we will combine multiple plots so there is no need for the legend
1008-
# to be in twice
1009-
1010-
# To display the legend, just remove the guides() line:
1011-
(diet_area <- ggplot(diet_sum, aes(area = n, fill = diet, label = n,
1012-
subgroup = diet)) +
1013-
geom_treemap() +
1014-
geom_treemap_subgroup_border(colour = "white", size = 1) +
1015-
geom_treemap_text(colour = "white", place = "center", reflow = T) +
1016-
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
1017-
scale_fill_manual(values = wes_palette("Cavalcanti1")))
1018-
1019-
ggsave(diet_area, filename = "diet_area.png",
1020-
height = 5, width = 8)
1021-
```
1022-
1023-
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/diet_bar.png" alt="Img" style="width: 500px;"/> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/diet_area2.png" alt="Img" style="width: 500px;"/></center>
1024-
1025-
__We've covered spatial representation of the data (our map), as well as the kinds of species (the diet figures), now we can cover another dimention - time! We can make a timeline of the individual studies to see what time periods are best represented.__
1026-
1027-
```r
1028-
# Timeline
1029-
# Making the id variable a factor
1030-
# otherwise R thinks its a number
1031-
bird_models_traits$id <- as.factor(as.character(bird_models_traits$id))
1032-
1033-
(timeline_aus <- ggplot() +
1034-
geom_linerange(data = bird_models_traits, aes(ymin = minyear, ymax = maxyear,
1035-
colour = diet,
1036-
x = id),
1037-
size = 1) +
1038-
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
1039-
labs(x = NULL, y = NULL) +
1040-
theme_bw() +
1041-
coord_flip())
1042-
```
1043-
1044-
Well this looks untidy! The values are not sorted properly and it looks like a mess, but that happens often when making figures, part of the figure beautification journey. We can fix the graph with the code below.
1045-
1046-
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/timeline3.png" alt="Img" style="width: 600px;"/></center>
1047-
1048-
```r
1049-
# Create a sorting variable
1050-
bird_models_traits$sort <- bird_models_traits$diet
1051-
bird_models_traits$sort <- factor(bird_models_traits$sort, levels = c("VertFishScav",
1052-
"FruiNect",
1053-
"Omnivore",
1054-
"Invertebrate",
1055-
"PlantSeed"),
1056-
labels = c(1, 2, 3, 4, 5))
1057-
1058-
bird_models_traits$sort <- paste0(bird_models_traits$sort, bird_models_traits$minyear)
1059-
bird_models_traits$sort <- as.numeric(as.character(bird_models_traits$sort))
1060-
```
1061-
1062-
This sorting variable will help us arrange the studies first by species' diet, then by when each study started.
1063-
1064-
```r
1065-
(timeline_aus <- ggplot() +
1066-
geom_linerange(data = bird_models_traits, aes(ymin = minyear, ymax = maxyear,
1067-
colour = diet,
1068-
x = fct_reorder(id, desc(sort))),
1069-
size = 1) +
1070-
scale_colour_manual(values = wes_palette("Cavalcanti1")) +
1071-
labs(x = NULL, y = NULL) +
1072-
theme_bw() +
1073-
coord_flip() +
1074-
guides(colour = F) +
1075-
theme(panel.grid.minor = element_blank(),
1076-
panel.grid.major.y = element_blank(),
1077-
panel.grid.major.x = element_line(),
1078-
axis.ticks = element_blank(),
1079-
legend.position = "bottom",
1080-
panel.border = element_blank(),
1081-
legend.title = element_blank(),
1082-
axis.title.y = element_blank(),
1083-
axis.text.y = element_blank(),
1084-
axis.ticks.y = element_blank(),
1085-
plot.title = element_text(size = 20, vjust = 1, hjust = 0),
1086-
axis.text = element_text(size = 16),
1087-
axis.title = element_text(size = 20)))
1088-
1089-
ggsave(timeline_aus, filename = "timeline.png",
1090-
height = 5, width = 8)
1091-
```
1092-
1093-
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/timeline2.png" alt="Img" style="width: 600px;"/></center>
1094-
1095981
__For our final figure using our combined dataset of population trends and species' traits, we will make a figure classic - the scatterplot. Body mass can sometimes be a good predictor of how population trends and extinction risk vary, so let's find out if that's true for the temporal changes in abundance across monitored populations of Australian birds.__
1096982

1097983
```r

0 commit comments

Comments
 (0)