Data visualisation

Being able to look at the data is a key step in data exploration, during the analysis and in the communication of results. R has a range of powerful tools to create graphs quickly, and then to develop them into a publication-ready format where helpful.

For an introduction to the Grammar of Graphics and ggplot2, the package that we will be using for visualisation, watch this video:

r video_code("sk7TT5qM5Hw")

Click here for the code used in the video

ggplot2

We use the ggplot2-package because it offers a consistent way to create anything from simple exploratory plots to complex data visualisation. Each graph command needs certain parts:

Multiple geometries can be layered on top of each other, for example to add trend lines to scatterplots. In that case, aes() functions can be included into the geom_x()-functions to make some of the mappings specific to certain geometries. This is done in the example below to color the points by continent without applying that aestethic to the line - if it was included in the main aes()-function, the plot would contain a separate coloured line for each continent.

Note: Line breaks are completely up to you and can be used to make the code readable as long as the command does not appear complete too early - to keep it simple, there should always be an open bracket, a comma or a + before a line break within the command to create a ggplot-chart

pacman::p_load(dslabs, dplyr, ggplot2)
gapminder2010 <- gapminder %>% filter(year==2010)

ggplot(gapminder2010, aes(x=infant_mortality, y=fertility)) +
  geom_point(aes(col=continent)) + geom_smooth() +
    ggtitle("Association of infant mortality and fertility", 
            subtitle="2010 data from gapminder.org")

esquisse: using ggplot2 with your mouse

The esquisse package provides an RStudio add-in that lets you create ggplot2-charts interactively, without having to know the code in advance. If that sounds You can check out the Getting Started guide{target="_blank"} to see what that would look like. To try it out, run install.packages("esquisse"), load the data you want to use, and type esquisse::esquisser() into your Console.

Try not to rely on esquisse instead of ggplot2 - the aim in this module is to learn how to write code that is reproducible, not how to click boxes. However, esquisse shows you the code it generates (check Export & Code at the bottom right), so that it can be very helpful when you are getting started, or when you are not quite sure how to do something.

Opinionated visualisation

It is easy to find examples of misleading charts that should never have been published. However, even when all the elements of a chart are legitimately presented, the same data can still be used to suggest radically different conclusions. See the following example and follow the source link to read more about "opinionated" data visualisations. Apart from being critical when seeing charts, the take-away message from this is to be conscious of the power of small design choices - that is a power you should wield consciously when creating charts.

knitr::include_graphics("./images/iraq-bloody-toll.png")

Further resources {#further-resources-visualisation}



LukasWallrich/GoldCoreQuants documentation built on Nov. 27, 2021, 1:58 a.m.