ggplotly function
The ggplotly function of the plotly package converts charts made with ggplot2 into interactive charts, so you can zoom, hover over the elements to see their values or hide groups clicking on the legend. This is the easiest way to create interactive charts if you already work with ggplot2.
Create a chart with ggplot2, assign it to an object and pass it to ggplotly. In the following example we are using the mpg data set of ggplot2 to create a scatter plot colored by the class of the cars.
# install.packages("ggplot2")
# install.packages("plotly")
library(ggplot2)
library(plotly)
p <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point()
ggplotly(p)
Note that most geoms and customizations of ggplot2 are supported, such as themes, facets or colors, but some extensions of ggplot2 and some elements, such as subtitles and captions, are not converted.
By default, the tooltip displays all the variables mapped inside aes. The tooltip argument allows choosing which ones to display, passing the names of the aesthetics, such as "x", "y" or "color". Set tooltip = "none" to disable it.
p <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point()
ggplotly(p, tooltip = c("x", "y"))
If you want to display a custom text pass it to the text aesthetic and then set tooltip = "text". ggplot2 will warn you that text is an unknown aesthetic, but you can ignore it, as it will be used by ggplotly. Use <br> to add line breaks.
p <- ggplot(mpg, aes(x = displ, y = hwy, color = class,
text = paste("Model:", model,
"<br>Engine displacement:", displ,
"<br>Highway miles per gallon:", hwy))) +
geom_point()
ggplotly(p, tooltip = "text")
The output of ggplotly is a plotly object, so you can keep customizing it with the functions of the package, such as layout. This is useful for features that are not converted from ggplot2, such as a horizontal legend under the chart.
p <- ggplot(mpg, aes(x = displ, y = hwy, color = drv)) +
geom_point() +
theme_minimal()
ggplotly(p) |>
layout(legend = list(orientation = "h", x = 0, y = -0.2))
By default, the axis ticks are fixed to the ones of the ggplot2 chart, so when zooming in you can end up with few labels. Set dynamicTicks = TRUE to recalculate them when zooming. This is especially useful for dates in time series.
p <- ggplot(economics, aes(x = date, y = unemploy)) +
geom_line(color = "#264653")
ggplotly(p, dynamicTicks = TRUE)
The toolbar on the top right corner of the chart can be removed with config(displayModeBar = FALSE). The config function also allows removing the plotly logo with displaylogo = FALSE or some of the buttons.
p <- ggplot(mpg, aes(x = class, y = hwy, fill = class)) +
geom_boxplot() +
theme(legend.position = "none",
axis.text.x = element_text(angle = 45))
ggplotly(p) |>
config(displayModeBar = FALSE)
If you need more control over the interactivity you can create the charts directly with the plot_ly function, as explained in the guide about interactive plots with plotly. You can also convert ggplot2 maps into interactive maps.
Learn statistics from the basics to advanced techniques, clearly explained
Go to site
See also