Note: Quarto does not support dashboard yet. This document is created using R Markdown (Rmd).
This dashboard uses the Plotly package, with example data from Plotly and Gapminder data to illustrate interactive charts. Note the use of external fonts from Google Fonts, color choices and animation button and slider control.
# install.packages(c("plotly","tidyverse","RColrBrewer"))
library(plotly)
library(tidyverse)
# Reading in example dataset
df <- read.csv("https://plotly.com/~public.health/17.csv", skipNul = TRUE, encoding = "UTF-8")
# Font management
library(showtext)
font_add_google("EB Garamond","ebgaramond")
t <- list(
family = "ebgaramond",
size = 12)
# Create label function for animation button
labels <- function(size, label) {
list(
args = c("xbins.size", size),
label = label,
method = "restyle"
)
}
# Create Figure object
fig <- df %>%
plot_ly(
x = ~date,
autobinx = FALSE,
autobiny = TRUE,
marker = list(color = "steelblue"),
name = "date",
type = "histogram",
xbins = list(
end = "2016-12-31 12:00",
size = "M1",
start = "1983-12-31 12:00"
)
)
# Configure dropdown menu
fig <- fig %>% layout(
paper_bgcolor = "white",
plot_bgcolor = "white",
title = "<b>Shooting Incidents</b><br>use dropdown to change bin size", # HTML to separate line
xaxis = list(
type = 'date'
),
yaxis = list(
title = "Incidents"
),
updatemenus = list(
list(
x = 0.1,
y = 1.15,
active = 1,
showactive = TRUE,
buttons = list(
labels("D1", "Day"),
labels("M1", "Month"),
labels("M6", "Half Year"),
labels("M12", "Year")
)
)
)
)
fig <- fig %>% layout(font = t) # Add font to text
fig
#install.packages("gapminder")
library(gapminder)
library(RColorBrewer)
library(plotly)
# Read in Gapminder data
df <- gapminder
# Font management
t <- list(
family = "ebgaramond",
size = 12)
# Create figure object
fig <- df %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
colors = c("slateblue3", "steelblue", "firebrick", "forestgreen", "turquoise1"),
# manually select colar
alpha=.5, # translucent glyth
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers',
fill = ~''
)
# Add font to text
fig <- fig %>% layout(
xaxis = list(
type = "log"
), font = t
)
fig <- fig %>% layout(legend = list(orientation = "h", # show entries horizontally
xanchor = "center", # use center of legend as anchor
x = 0.5, y = 100)) # put legend in center of x-axis
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig <- fig %>% animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="red"))
)
fig