Skip to content

Commit c566a47

Browse files
committed
2 parents dd40939 + bf03d7d commit c566a47

File tree

1 file changed

+175
-6
lines changed

1 file changed

+175
-6
lines changed

_tutorials/dataviz-storytelling.md

Lines changed: 175 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
layout: tutorial
3-
title: Efficient and beautiful data synthesis
4-
subtitle: Taking your tidyverse skills to the next level
5-
date: 2020-02-12 10:00:00
3+
title: Storytelling with Data
4+
subtitle: Data visualisation meets graphic design to tell scientific stories
5+
date: 2022-12-12 10:00:00
66
author: Gergana
77
redirect_from:
8-
- /2020/02/12/dataviz-beautification-synthesis.html
8+
- /2022/12/12/dataviz-beautification-synthesis.html
99
tags: data-vis intermediate advanced
1010
---
1111

@@ -21,10 +21,14 @@ tags: data-vis intermediate advanced
2121

2222
<div class="bs-callout-blue" markdown="1">
2323

24-
__The goal of this tutorial is to advance skills in data synthesis, particularly visualisation, manipulation, efficiently handling datasets and customising figures to make them both beautiful and informative. Here, we will focus on using packages from the `tidyverse` collection and a few extras, which together can streamline data visualisation and make your research pop out more!__
24+
__The goal of this tutorial is to advance skills in visualisation, manipulation, efficiently handling datasets and customising figures to make them both beautiful and informative. Here, we will focus on using packages from the `tidyverse` collection and a few extras, which together can streamline data visualisation and make your research pop out more!__
2525

2626
</div>
2727

28+
### Before we get to the code, here are some of the things that are important to consider when making graphs and telling a scientific story.
29+
30+
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/data-synthesis/handout_final.png" alt="Img" style="width: 700px;"/></center>
31+
2832
## All the files you need to complete this tutorial can be downloaded from <a href="https://github.com/ourcodingclub/CC-dataviz-beautification-synthesis" target="_blank">this repository</a>. __Click on `Code/Download ZIP` and unzip the folder, or clone the repository to your own GitHub account.__
2933

3034
`R` really shines when it comes to data visualisation and with some tweaks, you can make eye-catching plots that make it easier for people to understand your science. The `ggplot2` package, part of the `tidyverse` collection of packages, as well as its many extension packages are a great tool for data visualisation, and that is the world that we will jump into over the course of this tutorial.
@@ -1133,7 +1137,172 @@ ggsave(trends_mass, filename = "trends_mass.png",
11331137

11341138
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/trends_mass1.png" alt="Img" style="width: 500px;"/> <img src="{{ site.baseurl }}/assets/img/tutorials/dataviz-beautification-synthesis/trends_mass2.png" alt="Img" style="width: 500px;"/></center>
11351139

1136-
### Congrats on taking three different types of figures on beautification journeys and all the best with the rest of your data syntheses!
1140+
The world of coding and packages is pretty dynamic and things change - like how since I originally made the graphs above, the `theme_clean()` function changed and now makes a slightly different type of graph. Perhaps you notice horizontal lines going across the plot. Sometimes they can be useful, other times less so as they can distract people and make the graph look less clean (ironic given the theme name). So for our next step, we will make our own theme.
1141+
1142+
```r
1143+
# Make a new theme
1144+
theme_coding <- function(){ # creating a new theme function
1145+
theme_bw()+ # using a predefined theme as a base
1146+
theme(axis.text.x = element_text(size = 12, vjust = 1, hjust = 1), # customising lots of things
1147+
axis.text.y = element_text(size = 12),
1148+
axis.title = element_text(size = 14),
1149+
panel.grid = element_blank(),
1150+
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), units = , "cm"),
1151+
plot.title = element_text(size = 12, vjust = 1, hjust = 0.5),
1152+
legend.text = element_text(size = 12, face = "italic"),
1153+
legend.title = element_blank(),
1154+
legend.position = c(0.9, 0.9))
1155+
}
1156+
```
1157+
1158+
### A data storytelling tip: Find something to highlight, is there a story amidst all the points?
1159+
1160+
While having lots of data is often impressive, it can also make it hard to actually figure out what the key message of the graph is. In this tutorial we are exploring how bird populations are changing over time. Might be cool to highlight a particular species, like this mallee emu-wren, a small bird that hasn't experienced particularly dramatic population changes. But in a time of global change, telling apart relatively stable populations is also important!
1161+
1162+
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/data-synthesis/wren.png" alt="Img" style="width: 500px;"/></center>
1163+
<center>Illustration by <a href="https://www.malkolmboothroyd.com" target="_blank">Malkolm Boothroyd</a></center>
1164+
1165+
We could make the mallee emu-wren point bigger and a different colour, for which we essentially need a column that says whether or not a given record is for the mallee emu-wren.
1166+
1167+
### A data manipulation tip: Using case_when(), combined with mutate, is a great way to create new variables based on one or more conditions from other variables.
1168+
1169+
```r
1170+
# Create new columns based on a combo of conditions using case_when()
1171+
bird_models_mass <- bird_models_mass %>%
1172+
mutate(wren_or_not = case_when(common.name == "Mallee emu-wren" ~ "Yes",
1173+
common.name != "Mallee emu-wren" ~ "No"))
1174+
```
1175+
1176+
Now we are ready for an even snazzier graph! One thing you might notice is different is that before we added our data frame right at the start in the first line inside the `ggplot()`, whereas now we are adding the data inside each specific element - `geom_point`, `geom_smooth`, etc. This way `ggplot` gets less confused about what elements of the code apply to which parts of the graph - a useful thing to do when making more complex graphs.
1177+
1178+
We can also add our mallee emu-wren illustration to the plot!
1179+
1180+
```r
1181+
# Load packages for adding images
1182+
packs <- c("png","grid")
1183+
lapply(packs, require, character.only = TRUE)
1184+
1185+
# Load beluga icon
1186+
icon <- readPNG("wren.png")
1187+
icon <- rasterGrob(icon, interpolate=TRUE)
1188+
```
1189+
1190+
And onto the figure!
1191+
1192+
```r
1193+
(trends_mass_wren <- ggplot() +
1194+
geom_point(data = bird_models_mass, aes(x = log(mass), y = abs(estimate),
1195+
colour = wren_or_not,
1196+
size = wren_or_not),
1197+
alpha = 0.3) +
1198+
geom_smooth(data = bird_models_mass, aes(x = log(mass), y = abs(estimate)),
1199+
method = "lm", colour = "deepskyblue4", fill = "turquoise4") +
1200+
geom_label_repel(data = subset(bird_models_mass, common.name == "Mallee emu-wren"),
1201+
aes(x = log(mass), y = abs(estimate),
1202+
label = common.name),
1203+
box.padding = 1, size = 5, nudge_x = 1, nudge_y = 0.1,
1204+
# We are specifying the size of the labels and nudging the points so that they
1205+
# don't hide data points, along the x axis we are nudging by one
1206+
min.segment.length = 0, inherit.aes = FALSE) +
1207+
annotation_custom(icon, xmin = 2.3, xmax = 4.2, ymin = 0.16, ymax = 0.22) +
1208+
# Adding the icon
1209+
scale_colour_manual(values = c("turquoise4", "#b7784d")) +
1210+
# Adding custom colours
1211+
scale_size_manual(values= c(3, 10)) +
1212+
# Adding a custom scale for the size of the points
1213+
theme_coding() +
1214+
# Adding our new theme
1215+
guides(size = F, colour = F) +
1216+
# An easy way to hide the legends which are not very useful here
1217+
ggtitle("Mallee emu-wren trends\nin the context of Australian-wide trends") +
1218+
# Adding a title
1219+
labs(x = "\nlog(Body mass)", y = "Absolute population change\n"))
1220+
```
1221+
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/data-synthesis/trends_mass_wren.png" alt="Img" style="width: 500px;"/></center>
1222+
1223+
You can save it using `ggsave()` - you could use either `png` or `pdf` depending on your needs - `png` files are raster files and if you keep zooming, they will become blurry and are not great for publications or printed items. `pdf` files are vectorised so you can keep zooming to your delight and they look better in print but are larger files, not as easy to embed online or in presentations. So think of where your story is going and that can help you decide of the file format.
1224+
1225+
```r
1226+
ggsave(trends_mass_wren, filename = "trends_mass_wren.png",
1227+
height = 5, width = 6)
1228+
```
1229+
1230+
## 3. Put your story in perspective
1231+
1232+
We have highlighted the mallee emu-wren - a great thing to do if we are say a scientist working on this species, or a conservation organisation focusing on its protection, or we just really like this cute little Australian bird. When trying to tell a story with data though, it's always nice to put things in perspective and maps are a very handy way of doing that. We could tell the story of bird monitoring around the world, highlight a region of interest (Australia) and then give the story an anchor - the mallee emu-wren!
1233+
1234+
First, we will create the map - here is how to make an object with the world in it.
1235+
1236+
```r
1237+
world <- map_data("world")
1238+
```
1239+
1240+
Next up, we can extract the coordinates of the different bird populations monitored around the world.
1241+
1242+
```r
1243+
bird_coords <- bird_pops_long %>%
1244+
dplyr::select(3:27) %>%
1245+
distinct()
1246+
```
1247+
1248+
And now we are ready for our map! One way to learn what each line does is to have a go at commenting it out using a `#` and then spotting what changes - or you can check out the comments below each line.
1249+
1250+
```r
1251+
(pop_map <- ggplot(bird_coords, aes(x = decimal.longitude, y = decimal.latitude)) +
1252+
geom_polygon(data = world, aes(x = long, y = lat, group = group), fill = "grey", alpha = 0.4) +
1253+
# Adding the world
1254+
geom_bin2d(bins = 100) +
1255+
# Adding density squares - they will show how many data points there are in each square
1256+
theme_void() +
1257+
# Adding a clean theme
1258+
coord_proj("+proj=eck4") +
1259+
# A custom projection
1260+
ylim(-80, 80) +
1261+
# Setting some limits to the graphs coordinates
1262+
scale_fill_viridis(option = "magma",
1263+
direction = -1,
1264+
end = 0.35, begin = 0.8,
1265+
name = "Number of time series",
1266+
#breaks = c(50, 150, 250),
1267+
guide = guide_legend(keyheight = unit(2.5, units = "mm"),
1268+
keywidth = unit(10, units = "mm"),
1269+
label.position = "bottom",
1270+
title.position = 'top', nrow = 1)) +
1271+
# Adding a nice colour theme plus a custom legend
1272+
ggtitle("Bird populations in the Living Planet Database") +
1273+
annotate("rect", xmin = 110, xmax = 160, ymin = -10,
1274+
ymax = -50, alpha = 0.2, fill = "turquoise4") +
1275+
# Adding a semi-transparent polygon to highlight Australia
1276+
theme(legend.position = c(0.14, 0.07),
1277+
legend.title=element_text(color = "black", size = 10),
1278+
text = element_text(color = "#22211d"),
1279+
plot.title = element_text(size = 12, hjust = 0.5,
1280+
color = "grey20",
1281+
margin = margin(b = 0.2,
1282+
t = 0.4, l = 2,
1283+
unit = "cm"))))
1284+
1285+
ggsave(pop_map, filename = "bird_map.png")
1286+
```
1287+
1288+
Here is our map!
1289+
1290+
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/data-synthesis/bird_map.png" alt="Img" style="width: 700px;"/></center>
1291+
1292+
Finally, lets put our story together by making a panel! The `widths` and `heights` arguments help get the proportions right.
1293+
1294+
```r
1295+
bird_panel <- grid.arrange(pop_map, trends_mass_wren, ncol = 2,
1296+
widths = c(0.6, 0.4),
1297+
heights = c(1, 0.15))
1298+
1299+
ggsave(bird_panel, filename = "bird_map_panel.png",
1300+
height = 5, width = 12)
1301+
```
1302+
1303+
<center> <img src="{{ site.baseurl }}/assets/img/tutorials/data-synthesis/bird_map_panel.png" alt="Img" style="width: 900px;"/></center>
1304+
1305+
### Congrats on taking many different types of figures on beautification journeys and all the best with the rest of your data viz and storytelling!
11371306

11381307
<b>If you'd like more inspiration and tips, check out the materials below!</b>
11391308

0 commit comments

Comments
 (0)