In this overview I’m going to walk you through how to make your charts more interactive, with the ability to display more contextual information. The packages we’ll be utilizing are ggplot2 and plotly. We’ll specifically be using the ggplotly() function within plotly.
So you can easily follow along, we’ll be utilizing the iris data set within R. At the end I will show you how we can translate this information and create a more real-world example.
First let’s load the packages and take a quick look at the data set we’ll be utilizing. I chose this data set since it’s easy to work with and has easy to understand groupings.
# Load Packages
library(ggplot2)
library(tidyverse)
library(plotly)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
For these examples we won’t bother with adjusting our axis or title labels so that the code is easier to interpret and there’s less “filler”.
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3)
sp <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3)
ggplotly(sp)
You’ll notice in the above code chunk we had to assign our ggplot to a variable. This makes it significantly easier to wrap ggplotly() around and improves the readability of your code. Just this step alone, made it easier to distinguish the value of each point in our graph; all we have to do is hover over the point.
What if we want to rename the text that displays when hovering over a data point? In the next 3 sections we’ll cover:
In the previous graph you may have noticed the variable names default to the data frame column names. This may not always be preferred. The order of these variables may also not makes sense from a readability perspective.
For this example we’ll move Species to the top, rename Sepal.Width to Width, and rename Sepal.Length to Length. Let’s take a look at the code chunk and corresponding graph first!
sp <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3, aes(text = paste0("Species: ", Species,
"<br>",
"Width: ", Sepal.Width,
"<br>",
"Length: ", Sepal.Length)))
ggplotly(sp, tooltip = "text")
Adjusting the text required 2 things:
All that is required to adjust the text is to format the text you would like displayed. We can use paste0() to help us create a string. You’ll notice the <br> tags in between each line. This makes each variable we’d like to display appear on a new line. You can even add extra spacing if you desire!
The text renders as html, so you can easily customize how you would like your text displayed. For example if we wanted to bold the labels we could do the following:
sp <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3, aes(text = paste0("<b>Species: </b>", Species,
"<br>",
"<b>Width: </b>", Sepal.Width,
"<br>",
"<b>Length: </b>", Sepal.Length)))
ggplotly(sp, tooltip = "text")
Other column values that don’t appear on the graph can easily be added to your text field. This can often add some extra contextual information. As you can see below, I’ve added each column value to the text so you can see the full species’ information while hovering over the data point.
sp <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point(size = 3, aes(text = paste0("Species: ", Species,
"<br>",
"Sepal Width: ", Sepal.Width,
"<br>",
"Petal Width: ", Petal.Width,
"<br>",
"Sepal Length: ", Sepal.Length,
"<br>",
"Petal Length: ", Petal.Length)))
ggplotly(sp, tooltip = "text")
When you have coloured groups, the hover text background will typically default to the respective data point’s colour. In some cases it will default to a dark grey. If you would like this background to be a single colour, here’s how you do that! This step needs to be done when calling ggplotly().
ggplotly(sp, tooltip = "text") %>%
layout(hoverlabel=list(bgcolor = "#ffffff"))
This graph will be using some data from the txhousing data set within the ggplot2 package. We’ll be looking at monthly house sales for the city of Abilene in the year 2000.
# Assign filtered data frame to df
df <- txhousing %>%
filter(city == "Abilene" & year == 2000)
# Format month numbers as the month name
df$month <- month(df$month, abbr = FALSE, label = TRUE)
# Create barplot
bp <- df %>%
ggplot(aes(x = month, y = sales)) +
geom_col(stat = "identity",
fill = "#1f5673",
aes(text = paste0(
"<b>", month, " Summary", "</b>",
"<br>",
"<b>Sales: </b>", sales,
"<br>",
"<b>Median Sale Price: </b>$", median,
"<br>",
"<b>Percentage of Listings Sold: </b>", round(sales/listings*100, 2), "%"
))) +
labs(title = "Monthly House Sales for Abilene in the Year 2000",
x = "Month",
y = "Total House Sales") +
geom_text(aes(label = sales,
y = sales + 3,
text = paste0(sales, " Monthly Sales")))
# Wrap with ggplotly()
ggplotly(bp, tooltip = "text")%>%
layout(hoverlabel=list(bgcolor = "#ffffff"))
That’s it! Animating your ggplot2 graphs with plotly isn’t overly complex and doesn’t require you to learn an entire package. There are some nuances as you’ll noticed with how I positioned the geom_text() in the house sales graph. I typically wrap the majority of my ggplot2 graphs in ggplotly() since improve the user experience. Not only can they glance at the graph to get basic information, but simply hovering over a data point their interested in will provide them with additional information, without navigating away from the graph,.