Changing ggplot colors with scale_color_brewer
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Introduction
‘War is over, if you want it, war is over, now’
John Lennon
Have you ever seen a webpage where text and visualizations do not have the right contrast or where colors do not match? I have had this experience and I have to admit that I myself have difficulty choosing adequate colors for my plots. There are so many technical details to pay attention to! Fortunately R offers you several libraries made by professional designers that offer excellent color palettes for you. In this lesson, you will learn about one of these libraries, the RColorBrewer. We will use data on military expenses of the main capitalist countries during the Cold War.
Data source
Data used in this lesson is available on the World Bank website.
Coding the past: improve your plot’s colors with scale_color_brewer
1. Importing data into R
Download the data file here and load the libraries we will need, according to the code below. To read the data, use the R function read_csv()
. Additionally, we are only interested in the five first rows and in columns 3 and 5 to 36. They are selected with [1:5, c(3, 5:36)]
.
content_copy Copy
2. How to use pivot_longer?
If you take a look at the dataframe you just loaded, you will see that it has one column for each year. To use ggplot your data has to be tidy. According to Hadley Wickham, in a tidy dataframe:
- Each variable must have its own column;
- Each observation must have its own row;
- Each value must have its own cell;
To make our data tidy, we will transform all the year columns in one variable called “year” and we will also transfer the values contained in these columns to a single new variable called “expense”. Note the syntax of the pivot_longer
function. The first argument is the dataframe we want to transform, the second are the columns we would like to treat. Finally, names_to
indicates the name of the new column that will receive the years and values_to
indicates the name of the new column that will receive the values of the year columns.
The mutate
function makes two adjustments in the new long dataset. First, it eliminates the second part of the year names, e.g., [YR1960]
. Second, it rounds the expenses values to two decimal places.
Finally, we change the names of the columns (variables) in our dataset.
content_copy Copy
3. Using scale_color_brewer to improve your plots’ colors
To see all the colors palettes the RColorBrewer offers, use the following code:
content_copy Copy
We will be using palette Set1
in our line plot. To set it, add the layer scale_color_brewer(palette = 'Set1')
. Note that we also set the x-axis to have labels every 4 years with scale_x_discrete(breaks = seq(1960, 1990, by=4))
. Color and group aesthetics were mapped to countries so that each country has a different color.
content_copy Copy
4. Adding a theme to the plot
To customize our plot, we will use the ggplot theme developed in the lesson ‘How to show historical weather data with ggplot2 customized plots’. Small adjustments were made to adapt the theme to this plot. For instance, the legend position was set to be at the bottom of the plot and its title was deleted.
content_copy Copy
Feel free to test other color palettes and check the one you like the most!
Conclusions
- You can transform your dataframe from wide to long format using
pivot_longer
; - RColorBrewer offers color palettes to make your plots more effective and beautiful.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.