Chapter 9 Manipulating plots

9.1 Themes

As you could see in the plots above, we have used a default theme in ggplot2. Table 9.1 shows a series of themes to be found in ggplot2and the package ggthemes. These are just a selection of some of the themes.

Table 9.1: Selected themes for ggplot2
Function Package Description
theme_bw() ggplot2 Black elements on white background
theme_minimal() ggplot2 Minimalistic
theme_classic() ggplot2 Theme without grid lines
theme_base() ggthemes Copy of the base theme in R
theme_economist() ggthemes The Economist theme
theme_fivethirtyeight() ggthemes FiveThirtyEight theme
theme_tufte() ggthemes Tufte (1983) theme

Figure 9.1 shows the look of the different themes. The order is: Standard, theme_bw(), theme_minimal(), theme_classic(), theme_base(), theme_economist(), theme_fivethirtyeight(), theme_tufte().

Eight themes

Figure 9.1: Eight themes

You can find a lot more resources online related to ggplot2. In addition to the links above, do consult ggthemr and ggplot2 extensions.

Below, we will be using theme_minimal() as the theme when we work with out plots.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(position = "jitter") + 
  geom_smooth(se=FALSE) +
  theme_minimal()

9.2 Colours

If we want to change the colours of the points in our plot, we can add the colour="" option to our geometric objects. In the example below we change the colour of our points from black to red and the colour of the line to black.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(colour="red") + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal()

If we want to give points a value based on the value of a specific variable, we need to specificy this within aes(). When we add colour=abortlaw3 to our aes(), we will see different colours for states with different restrictions on abortion.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3)) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal()

If we want to change these colours, we can use scale_colour_manual().

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3)) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) 

The colours are very bright. If we want to make them less so we can add alpha to geom_point() to add transparency to the points. Below we use an alpha of 0.7 (if we want more transparency we can use a lower alpha level).

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) 

9.3 Labels

Make sure that your figure have labels that helps the reader understand what is going on. To do this, you can add labs() to your figure. Here we will add a title, subtitle and caption.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions"
  ) 

Last, we can see that the legend title is abortlaw3. We can change this by adding colour to labs() as well.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions"
  ) 

9.4 Axes

Related to labels are the axes. Always label the axes so they have meaningful names. The variable name is not a meaningful name. We add x and y to the labs() addition in our plot.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(se=FALSE, colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions",
    y = "Obama vote share in 2012",
    x = "Number of abortions per 1,000 women aged 15-44 in 2008"
  ) 

9.5 Confidence intervals

We can have confidence intervals in our figure by not having se (standard errors) set to FALSE.

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions",
    y = "Obama vote share in 2012",
    x = "Number of abortions per 1,000 women aged 15-44 in 2008"
  ) 

9.6 Making multiple plots in one

If we would prefer to have the plots for different observations, we can specify that with facet_grid().

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions",
    y = "Obama vote share in 2012",
    x = "Number of abortions per 1,000 women aged 15-44 in 2008"
  ) +
  facet_grid(~ abortlaw3)

However, now we have redundant information as we have a legend with no vital information not already visible in the figure. Below, we change the title of the figure, remove the legend and update the group names.

# Recode variable to have more informative labels
states$abortlaw3_names <- recode(states$abortlaw3, 
                                 "0-5 restr" = "Less than 6 restrictions",
                                 "6-8 restr" = "Between 6 and 8 restrictions",
                                 "9-10 restr" = "More than 8 restrictions"
                                 )

ggplot(states, aes(x=abort_rate08, y=obama2012)) +
  geom_point(aes(colour=abortlaw3), alpha=0.7) + 
  geom_smooth(colour="black") +
  theme_minimal() +
  scale_colour_manual(values = c("red", "blue", "black")) +
  labs(
    title = "Abortion restrictions, abortions and the Obama vote",
    subtitle = "The relation between number of abortions and vote share for Obama",
    caption = "Data from the poliscidata R package",
    colour = "Abortion restrictions",
    y = "Obama vote share in 2012",
    x = "Number of abortions per 1,000 women aged 15-44 in 2008"
  ) +
  facet_grid(~ abortlaw3_names) +
  # Remove the legend
  theme(legend.position="none")

9.7 Saving plots

When you have a plot you would like to save, you can use ggsave(). Do keep in mind that it will only save the last plot you have created.

ggsave("fig1-abortion.png")

The figure will be saved in your working directory. The file type .png can be replaced to whatever format you would prefer your figure to be in. If you have saved your figure in an object, you can save it by specifying this before the file name.

ggsave(fig1, "fig1-abortion.png")

Often you will see that you are not totally satisfied with the size of your figure. To change this, you can use width and height.

ggsave(fig1, "fig1-abortion.png", width = 4, height = 4)