HES 505 Fall 2022: Session 27
Matt Williamson
By the end of today you should be able to:
Build simple interactive graphics with ggplot and plotly
Describe the basic structure of shiny apps
Begin incorporating interactive elements into stories
plotlyg <- txhousing %>%
# group by city
group_by(city) %>%
# initiate a plotly object with date on x and median on y
plotly::plot_ly(x = ~date, y = ~median) %>%
# add a line plot for all texan cities
plotly::add_lines(name = "Texan Cities", hoverinfo = "none",
type = "scatter", mode = "lines",
line = list(color = 'rgba(192,192,192,0.4)')) %>%
# plot separate lines for Dallas and Houston
plotly::add_lines(name = ~city,
data = filter(txhousing,
city %in% c("Dallas", "Houston")),
hoverinfo = ~city,
line = list(color = c("red", "blue")),
color = ~city)plotlySyntax is similar to ggplot
hoverinfo describes which elements you’d like to make interactive
Other plot elements available (see ?plot_ly)
mapview and tmapEasy extension of your existing
Class Demo
leaflet
mapview
mapsapi
tmap and gganimateshinyAn Rstudio product designed to allow reactive data visualizations via an R server
Allows users to generate new results (not just plot your old stuff)
shiny appFlexdashboards allow the user to visualized data you’ve created
Integrating shiny apps allows them to run new analysis!
Reactivity is the link between user input and server output
server <- function(input, output, session) {
observe({
# even though the slider is not involved in a calculation, if
# you change the slider it will run all this code and update the text box
# changes to the mytext box also will trigger the code to run
input$myslider
txt <- paste(input$mytext, sample(1:10000, 1))
updateTextInput(session, inputId = "myresults", value = txt)
})
}
ui <- basicPage(
h3("The results text box gets updated if you change the other text box OR the slider."),
sliderInput("myslider", "A slider:", min=0, max=1000, value=500),
textInput("mytext", "Input goes here", value = "Initial value"),
textInput("myresults", "Results will be printed here")
)flexdashboardsDashboard: a visual display of interrelated data
flexdashboards are an Rmarkdown-powered way to build dashboards
Integrates with plotly, leaflet and other htmlwidgets
Examples ]
flexdashboardsAudience engagement drives design
“Scrolling” layouts allow linear story telling (like the ArcGIS Storymap)
“Tabset” layouts allow users to construct their own stories
Lots of readymade templates, themes, and add-ins
Display your research