Make an interactive plot
interactive-plots-3.Rmd
Make an interactive column chart of travel survey data
This example uses data from PSRC’s household travel survey, on how respondents reached public transit:
library(ggplot2)
library(psrcplot)
library(dplyr)
transit_access <- transit_access_example_data %>% select(-c("survey","sample_size"))
head(transit_access)
## mode_acc_walk count count_moe share share_moe year
## 1 Other Access Mode 76864.68 19849.31 0.1428148 0.0345003 2017/2019
## 2 Walked or jogged 461347.49 49535.06 0.8571852 0.0345003 2017/2019
## 3 Other Access Mode 95813.72 26434.51 0.2464731 0.0604748 2021
## 4 Walked or jogged 292925.31 36395.11 0.7535269 0.0604748 2021
To create a interactive column plot, call the function
interactive_column_chart()
, specifying
t
as the underlying table. Note that for
column charts the x variable should be
discrete/categorical and the y variable should be
numeric (as quantities are represented on the y axis).
transit_access_chart <- interactive_column_chart(
t=transit_access,
x='mode_acc_walk', y='share', fill='year',
moe='share_moe', est='percent',
color="psrc_light", title='Transit Access Mode')
transit_access_chart
To make interactive plots, psrcplot converts ggplot2 objects into plotly objects. The result can be altered via plotly commands. In some cases, due to plotly limitations, modifications may require you alter the underlying ggplot2 object first, as detailed here.
Exporting an interactive html chart
You can save the interactive bar chart programmatically using the
htmlwidgets::saveWidget()
command. Once you have an html version of the file it can be embedded in
any webpage.
htmlwidgets::saveWidget(transit_access_chart, file=('transit_access.html'))