In a previous R Tutorial, United States Shark Attack Data Analysis with R, we completed data analysis of confirmed unprovoked United States shark attacks from 1837 until July 26, 2018. The shark attack data was analyzed and visualized based on total occurrences in each state based in the U.S. and will graphically be displayed. In this tutorial, we will learn how to add a background image to ggplot() graphs using the png and grid packages.
Install and Load Packages
Below are the packages and libraries that we will need to load to complete this tutorial.
Input:
1 2 3 4 5 6 | install.packages("ggplot2") install.packages("png") install.packages("grid") library(ggplot2) library(png) library(grid) |
Download and Load the United States Shark Attack Dataset
This data is already packaged and is available for download from the University of Florida, Florida Museum. Or you can easily download the United States shark attack dataset from the R-ALGO dataset page or directly from here United States Shark Attacks – united_states_shark_attacks.csv
Input:
1 | us_attacks <- read.csv("united_states_shark_attacks.csv", stringsAsFactors = FALSE) |
View the United States Shark Attack Dataset
head() function
Input:
1 | head(us_attacks) |
Output:
1 2 3 4 5 6 7 | State Total 1 Florida 812 2 Hawaii 159 3 California 122 4 South Carolina 102 5 North Carolina 64 6 Texas 43 |
ggplot() with geom_bar() Plot for United States Shark Attacks
Below is a basic ggplot() with geom_bar() and some fun colors to plot out all of the states with shark attacks (obviously only coastal states).
Input:
1 2 3 4 5 6 7 | ggplot(us_attacks, aes(x = State, y = Total, fill = State)) + geom_bar(stat = "identity") + xlab("States") + ylab("Total Shark Attacks") + ggtitle("United States Shark Attacks") + theme(axis.text.x = element_text(angle = 35, hjust = 1)) + theme(plot.title = element_text(hjust = 0.5)) |
Output:
factor() Function to Plot High to Low Shark Attacks
I would prefer to plot the states with the most attacks to the lowest attacks. We can do this by using the factor() function as shown below.
Input:
1 2 3 4 5 6 7 8 9 | us_attacks$State <- factor(us_attacks$State, us_attacks$State) ggplot(us_attacks, aes(x = State, y = Total, fill = State)) + geom_bar(stat = "identity") + xlab("States") + ylab("Total Shark Attacks") + ggtitle("United States Shark Attacks") + theme(axis.text.x = element_text(angle = 35, hjust = 1)) + theme(plot.title = element_text(hjust = 0.5)) |
Output:
Inserting Background Image in ggplot()
Now, this is where the fun begins. I really enjoy being able to add some cool background images (png) in ggplot() to make it not so dull. Usually, if I have some colorful bars, as shown above, I will use a black or white image. In this example, I will be using black bar charts with a shark image (png) to make it presentable.
First, you need to find an image. I was able to find a free shark background using Google as shown below:
Create a variable by running the below command. (Make sure to have png package installed and loaded).
Input:
1 | shark_background <- png::readPNG("shark_background.png") |
Creating the Shark Attack Plot
Now we will draw a bar chart with a background image (from above). Also, we will need to combine the annotation_custom-function of the ggplot2 package and the rasterGrob-function of the grid package.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 | ggplot(us_attacks, aes(State, Total, fill = Total)) + ggtitle("United States Shark Attacks") + scale_fill_continuous(guide = FALSE) + annotation_custom(rasterGrob(shark_background, width = unit(1,"npc"), height = unit(1,"npc")), -Inf, Inf, -Inf, Inf) + geom_bar(stat="identity", fill = "#000000", position = "dodge", width = .75, colour = 'black') + scale_y_continuous('Total Attacks', limits = c(0, max(us_attacks$Total) + max(us_attacks$Total))) + scale_x_discrete('States') + geom_text(aes(label = round(Total), ymax = 0), size = 5, fontface = 2, colour = 'white', hjust = 0.5, vjust = -1) |
Output:
As you can see from the above, there are a few code changes we need to make to make the states readable and to center to plot title.
- Code change:
- Center title: theme(plot.title = element_text(hjust = 0.5))
- Angle the X axis State names: theme(axis.text.x = element_text(angle=45, hjust = 1))
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ggplot(us_attacks, aes(x=reorder(State, -Total), y=Total)) + ggtitle("United States Shark Attacks") + scale_fill_continuous(guide = FALSE) + annotation_custom(rasterGrob(shark_background, width = unit(1,"npc"), height = unit(1,"npc")), -Inf, Inf, -Inf, Inf) + geom_bar(stat="identity", fill = "#000000", position = "dodge", width = .75, colour = 'black') + scale_y_continuous('Total Attacks', limits = c(0, max(us_attacks$Total) + max(us_attacks$Total))) + scale_x_discrete('States') + geom_text(aes(label = round(Total), ymax = 0), size = 4, fontface = 2, colour = 'white', hjust = 0.5, vjust = -1) + theme(axis.text.x = element_text(angle=45, hjust = 1)) + theme(plot.title = element_text(hjust = 0.5)) |
Output:
Hope you enjoyed this tutorial and have fun creating plots with creativity!