1. Introduction

In this short post you will learn how to use the RColorBrewer package to choose customized colors for your ggplot visualizations.

2. Data

Data for this analysis comes from the World Bank.

3. Load the data and libraries

Data contains three variables. The first is country, the second refers to the year, and the third contains the percentage of GDP that a certain country spent in military.

library(dplyr)
library(ggplot2)
library(RColorBrewer)

load("data.RData")
df <- military_long

4. Box Plot to have a general idea of the data

df %>% 
  ggplot(aes(x = country, y = expense))+
  geom_boxplot()

Note that the military expenditure in Japan is almost a constant. Moreover, Japan is the country that spends the least in military, while the US spends the most.

5. Military expenditure throughout time

ggplot(data = df, aes(x = year, y = expense, group = country, color = country))+
  geom_line(size = 1)+
  scale_x_discrete(breaks = seq(1960, 1990, by=4))+
  xlab('Year')+
  ylab('Military expenditure (% of GDP)')+
  ggtitle('Military Expenses of the Main Capitalist Economies',
          subtitle = '1960 - 1991')

6. Using RColorBrewer to choose a different color pallete

Let us first see which are the color palettes offered by RColorBrewer

par(mar=c(3,4,2,2))
display.brewer.all()

Now you will use the Set1 color palette, passing it to scale_color_brewer. Note that we also made several changes to the ggplot theme. To learn more about it, check out this tutorial

ggplot(data = military_long, aes(x = year, y = expense, group = country, color = country))+
  geom_line(size = 1)+
  scale_x_discrete(breaks = seq(1960, 1990, by=4))+
  scale_color_brewer(palette = 'Set1')+
  xlab('Year')+
  ylab('Military expenditure (% of GDP)')+
  ggtitle('Military Expenses of the Main Capitalist Economies',
          subtitle = '1960 - 1991')+
  theme_bw()+
  guides(color = guide_legend(title=''))+
  theme(text=element_text(color = 'white'),
            # Changes panel, plot and legend background to dark gray:
            panel.background = element_rect(fill = '#2E3031'),
            plot.background = element_rect(fill = '#2E3031'),
            legend.background = element_rect(fill='#2E3031'),
            legend.key = element_rect(fill = '#2E3031'),
            # Changes legend texts color to white:
            legend.text =  element_text(colour = 'white'),
            legend.title = element_text(colour = 'white'),
            # Changes color of plot border to white:
            panel.border = element_rect(color = 'white'),
            # Eliminates grids:
            panel.grid.minor = element_blank(),
            panel.grid.major = element_blank(),
            # Changes color of axis texts to white
            axis.text.x = element_text(colour = 'white'),
            axis.text.y = element_text(colour = 'white'),
            axis.title.x = element_text(colour= 'white'),
            axis.title.y = element_text(colour= 'white'),
            # Changes axis ticks color to white
            axis.ticks.y = element_line(color = 'white'),
            axis.ticks.x = element_line(color = 'white'),
            legend.position = "bottom")