- Create a wide variety of interactive graphics using the
plotlyandggiraphpackages - Learn the basics of
shiny - Create some animations (not really interactive but sometimes useful)
plotly and ggiraph packagesshinyplotlyggplotlylibrary(plotly)
library(palmerpenguins)
p <- ggplot(data = penguins,
aes(x = bill_length_mm, y = flipper_length_mm)) +
geom_point()
ggplotly(p)
ggplotly plotggplotlyggplotly with more or less any ggplot that you have createdtooltip which is the highlighted information when you hover your mouse over the plotplotlyplotly also has its own syntax for creating individual plotsplot_ly(data = penguins, x = ~bill_length_mm,
y = ~bill_depth_mm, color = ~sex,
type = 'scatter', mode = 'markers')
plot_ly(data = penguins, x = ~species, y = ~bill_depth_mm, type = 'box')
plot_ly(data = penguins, x = ~bill_length_mm,
y = ~bill_depth_mm, z = ~flipper_length_mm,
type = 'scatter3d', mode = 'markers')
plotlyggplot2; though has never been quite as popularggiraphggiraphgeom_X with geom_X_interactivetooltip, clicking behaviour using onclick, and the data behaviour using data_idggiraph in uselibrary(ggiraph)
p <- iris %>% mutate(Row.name = 1:n()) %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, colour = Species,
tooltip = Row.name, data_id = Species)) +
geom_point_interactive()
girafe(ggobj = p, width = 10)
shinyui.R contains the details of what’s actually going to appear on the webpage. The second, server.R, contains all the details of the R code that needs to be runshiny walkthroughFile > New File > Shiny Web AppRun AppThe animation package contains functions which will turn static R plots into animations: gifs, html, swf, etc The general framework is:
(It’s a bit like using the pdf and similar commands in base R plotting)
animation examplelibrary(animation)
oopt = ani.options(interval = 0.2, nmax = 100)
for (i in 1:ani.options("nmax")) {
plot(density(rnorm(10*i)),
main = 10*i)
ani.pause()
}
gganimateHas special function called transition_state which works a little bit like facet_wrap but overlays the facets
library(gganimate)
library(gifski)
p <- ggplot(data = penguins, aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point() +
# Add an animation of the penguins' species over time
transition_states(species, transition_length = 2,
state_length = 1) +
# Add a label for the current species
labs(title = "Penguin species: {closest_state}")
animate(p, renderer = gifski_renderer())
gganimatelibrary(gapminder)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp,
label = country,
size = pop,
colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
#geom_text() +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
# Here comes the gganimate specific bits
transition_time(year) +
ease_aes('linear')
animate(p, renderer = gifski_renderer())
animate functionplotly is the way to start if you just want to create a simple animation from a ggplot. If you want more control use ggiraphshiny for full dashboard control of plots and any other R feature. A whole other course required to understand this fullygganimate a great package for creating animations (though also see plotly which can add a play button)