The plotly
package allows to build interactive charts
directly from R
. Here is a application to
stacked area chart, using both
the plot_ly()
and ggplotly()
functions.
ggplotly()
function
This post follows the previous basic stacked area chart built with ggplot2.
The idea is to turn the chart interactive:
This is done thanks to the ggplotly()
function of the
plotly
package that turn any ggplot2
chart
object interactive.
# Libraries
library(ggplot2)
library(dplyr)
library(babynames)
library(viridis)
library(hrbrthemes)
library(plotly)
# Load dataset from github
data <- babynames %>%
filter(name %in% c("Ashley", "Amanda", "Jessica", "Patricia", "Linda", "Deborah", "Dorothy", "Betty", "Helen")) %>%
filter(sex=="F")
# Plot
p <- data %>%
ggplot( aes(x=year, y=n, fill=name, text=name)) +
geom_area( ) +
scale_fill_viridis(discrete = TRUE) +
theme(legend.position="none") +
ggtitle("Popularity of American names in the previous 30 years") +
theme_ipsum() +
theme(legend.position="none")
# Turn it interactive
p <- ggplotly(p, tooltip="text")
p
# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/ggplotlyStackedareachart.html"))