Interactive Graphs With Plotly And Ggiraph Packages
Jan 26, 2017Imagine that you have a plot like this and want to make it interactive (e.g. show exact values on hover or print the model name).
suppressPackageStartupMessages(library(ggplot2))
p <- ggplot(mpg, aes(displ, hwy, color=class)) +
geom_point() +
labs(title="Smaller car = better MPG",
subtitle="Fuel economy data from 1999 and 2008 for 38 popular models of car",
caption = "adapted from http://fueleconomy.gov",
x="engine displacement (l)",
y="highway (miles per gallon)")
print(p)
Plotly
Getting plotly
of this graph is just one-liner. From now all ggplots can be easily made interactive.
suppressPackageStartupMessages(library(plotly))
ggplotly(p)
And if you need some other information to be printed (like model
), add it to aesthetics:
p2 <- ggplot(mpg, aes(x=displ, y=hwy, color=class, model=model)) +
geom_point() +
labs(title="Smaller car = better MPG",
subtitle="Fuel economy data from 1999 and 2008 for 38 popular models of car",
caption = "adapted from http://fueleconomy.gov",
x="engine displacement (l)",
y="highway (miles per gallon)")
ggplotly(p2)
If you want to show only model
label:
p2 <- ggplot(mpg, aes(x=displ, y=hwy, color=class, text=model)) +
geom_point() +
labs(title="Smaller car = better MPG",
subtitle="Fuel economy data from 1999 and 2008 for 38 popular models of car",
caption = "adapted from http://fueleconomy.gov",
x="engine displacement (l)",
y="highway (miles per gallon)")
# the only tooltip is now `text`
ggplotly(p2, tooltip = "text")
Plotly is able to create graphs with tens of thousands data points.
Ggiraph
Another way to create analogical plots is ggiraph
package.
suppressPackageStartupMessages(library(ggiraph))
p3 <- ggplot(mpg, aes(x=displ, y=hwy, color=class, tooltip=model)) +
geom_point_interactive(size=I(2)) +
labs(title="Smaller car = better MPG",
subtitle="Fuel economy data from 1999 and 2008 for 38 popular models of car",
caption = "adapted from http://fueleconomy.gov",
x="engine displacement (l)",
y="highway (miles per gallon)")
ggiraph(code = print(p3), selection_type = "single")
It not only looks great but it also works with R/Shiny (e.g. clicking on a dot can be used as input), see the vignette.