Visual Analytics Flashcards
What are geoms?
Visual marks representing data points
Geoms, short for “geometric objects” are the basic visual element used to create plots.
Why does ggplot(…) do?
Initialises a ggplot object
- Declare the input data frame for a graphic and specify the set of plot aesthetics (that will be common to all subsequent laters)
What layer creates a scatterplot?
geom_point()
What are scatterplots useful for?
Displaying the relationship between two continuous variables
How do you colour points by a variable?
Within aes()
color = factor(var)
How do you plot a line graph?
geom_line()
Useful for trend lines and time-series
How do you plot a box plot?
geom_boxplot()
How many geoms should you have?
Need to have at least one, there is not upper limit
What does geom_smooth() do?
Adds a smoothed trend line to a plot, helping to visualise patterns in the data.
eg geom_smooth(method = lm_
geom_smooth(method = loess)
Default is loess for < 1000 and GAM for > 1000
What kinds of variables can you include in geom_point()
Map variables to aesthetics
- colour
- shape
- size
- alpha
How do you display the definition of a function eg geom_bar and its default parameter values?
args() function
eg args(geom_bar)
What kinds of arguments can be customised in geom_histogram()?
+ geom_histogram(stat = “bin”, binwidth = 3)
stat = “bin” is the default
What are “scales”?
Functions that control how the data is mapped to visual properties
eg position (x and y axis), colour, shape or zie
What does scale_color_brewer() do and how is it used?
Used to modify the color scheme of the points (based in colour scheme defined in ggplot aesthetics)
+ scale_color_brewer(type = “seq”, palette = 3)
How do you import ggplot?
library(ggplot2)
If using shapes to represent a categorical variable, how do you change the shapes from the default?
scale_shape_manual(values = c(16, 15, 17))
passing in a list of chosen shapes corresponding to number of categories
ANOTHER EXAMPLE
+scale_color_manual(name=’Legend’, values=c(‘D’=”grey”,
‘E’=’red’…..))
What does adding scale_size(range = c(1, 10)) do?
This function scales the size of the points based on the size variable.
range = c(1, 10) means that the smallest value of size will be mapped to a point size of 1, and the largest value will be mapped to a point size of 10.
What does facet do?
It is a way of displaying multiple plots that share the same structure and data, but differ in some aspect of their visual appearance, such as the values of a categorical variable.
How do you facet a plot?
+ facet_wrap(~var, ncol=3)
What is theme in ggplot?
Theme is a set of aesthetic attributes that can be used to customise the appearance of non-data elements.
Includes
- Axis labels
- Legend
- Title
- Background
- Font size and colour
How do you make the shapes into line shapes?
+ scale_shape(solid = FALSE)
How do you change the colour and size of plotted points?
+ geom_point(shape = 21, colour = “black”, fill = “white”, size = 5, stroke = 5)
How do you add a theme?
Add to the document
theme_set(theme_bw())
How do you set limits on the x or y axis?
xlim(0, 1)
ylim(0, 1)
To adjust them:
+coord_cartesian(xlim=c(0,3),ylim = c(0,5000))
Difference is xlim/ylim removes data points outside the range but coord_cartesian keeps them, just crops the range.
How do you add a theme to a graph?
plot + theme_bw()
How do you add labels to your plot?
+ labs()
can set eg
- title
- subtitle
- y
- x
- caption
What does stroke control?
The thickness of the point boundary
How do you change the colour of text on a plot?
+ theme(text = element_text(colour = “blue”))
How do you change the size of text of the title and axis labels?
+ theme(plot.title=element_text(size=25),
axis.title.x=element_text(size=20),
axis.title.y=element_text(size=20),
axis.text.x=element_text(size=15),
axis.text.y=element_text(size=15))
How can you change the x and y tick labels?
scale_x_continuous()
eg + scale_x_continuous(labels=c(“zero”, “one”, ….. ))
eg + scale_y_continuous(breaks= seq(0, 20000, 4000))
How do you rotate the angle of the axis?
+ theme(axis.text.x = element_text(angle = 45))
How do you flip the x and y axis?
+ coord_flip()
How do you facet based on multiple categorical variables?
+ facet_grid(color ~ cut)
It arranges the subplots in a grid format, where:
color defines the rows.
cut defines the columns.
If you don’t need a strict row-column layout, you can use:
+ facet_wrap(~ color + cut)
What does setting scales = “free” mean?
Allows each facet (subplot) to have its own independent axis scales instead of using a shared scale across all facets.
Add it within facet_wrap / facet_grid
- scales=’free_x’ to free only X-axis
- scales=’free_y’ to free only Y-axis.
How can you arrange multiple plots?
library(gridExtra)
grid.arrange(p1,p2,ncol=2)
How can you match the colour of the smooth added to the categorical variable?
+ geom_smooth(aes(color=color))
How do you add a horizontal line to a plot?
geom_hline()
eg geom_hline(yintercept=5000, size=2, linetype=”dotted”, color=”blue”)
How do you add vertical line to a plot?
geom_vline()
eg + geom_vline(xintercept=4, size=2, color=”firebrick”)
How do you draw a line from x to y?
geom_segment()
eg geom_segment(aes(x=4, y=5000, xend=4, yend=10000, size=2), lineend=”round”)
In this example it is a vertical line (x and xend are the same)
How do you plot a bar chart side by side vs stacked?
p1 <- gg + geom_bar(position=”dodge”, aes(fill=factor(vs))) # side-by-side
p2 <- gg + geom_bar(aes(fill=factor(vs))) # stacked
What does aggregate do?
Groups the data and applies a function
eg df <- aggregate(mtcars$mpg, by=list(mtcars$cyl), FUN=mean)
mtcars$mpg is the column being aggregated (Miles Per Gallon).
by = list(mtcars$cyl): This groups the data by the number of cylinders (cyl).
FUN = mean: It calculates the mean mpg for each cylinder group.
If plotting a bar chart where the y data is “explicit” what do we need to do?
stat = “identity”
if the value is already calculated
How do you ensure data is treated as categorical?
Eg in a dataframe, apply as.factor() to the column
eg df$cyl <- as.factor(df$cyl)
How do you convert from Wide Format to Long Format using melt()?
reshape2::melt
eg reshape2::melt(economics[, c(“date”, “psavert”, “uempmed”)], id=”date”)
What is an area plot and how do we create it?
An area plot is a graph that uses color to show how data changes over time.
+ geom_area()
What two ways can you plot multiple variables on an area plot?
Non-overlapping (stacked) or overlapping
How do you create the two versions of the area plot?
Non-overlapping / stacked - use the long format dataset
+ geom_area(aes(y=value, fill=variable))
Overlapping - use the wide format dataset
+ geom_area(aes(y = variable, fill = “cornflowerblue”)
Add a new layer for each variable to plot, may need to adjust alpha values
What must app.R end with?
A call to shinyApp()
How do you include a sliding select scale?
sliderInput(
inputId = “num”,
label = “Choose a number”,
value = 25,
min = 1,
max = 100
)
How do you add a graph?
Use of plotOutput and renderPlot
UI side:
plotOutput(“hist”)
Server side:
output$hist <- renderPlot({
hist(rnorm(input$num))
)}
How can you incorporate a button?
actionButton()
submitButton()
How do you include a single check box?
checkboxInput()
How do you include multiple checkboxes in a group?
checkboxGroupInput()
How do you include a date?
dateInput()
How do include a date range?
dateRangeInput()
How do you include a file?
fileInput()
How do you create a drop down of numbers?
numericInput()
How do you include a Password field?
passwordInput()
How do you include radio buttons?
radioButtons()
How do you include a drop down selection?
selectInput()
How do you include a slider?
sliderInput()
How do you include a text field?
textInput()
How do you create an input?
with an *Input() function
What is the inputId used for?
For internal use
What is the label used for?
Used to display
How do you insert an interactive table?
dataTableOutput()
How do you insert raw HTML?
htmlOutput()
How do you insert an image?
imageOutput()
How do you insert a plot?
plotOutput()
How do you insert a tableOutput()?
tableOutput()
How do you insert a Shiny UI element?
uiOutput()
How do you insert text?
verbatimTextOutput()
How do you display an output?
Using *Output()
Where * is the type of output and the name to give to the output object goes in quotes between ()
eg plotOutput(“hist”)
What do you need to have between arguments?
Commas
What doe *Outpt() do and why is it not sufficient on its own?
*Output() is written in the UI and adds a space in the UI for an R object.
You have to build the object in the server function.
What do we tell the server?
How to assemble inputs into outputs
What are the 3 rules to write the server function?
1 - save objects to display to output$
eg output$hist
2 - build objects to display with render*()
eg renderPlot({})
3 - access input values with input$
How do you create an interactive table (from a data frame, matrix or other table-like structure)?
renderDataTable()
How do you create a code block of printed output?
renderPrint()
How do you create a character string?
renderText()
What does render*() do?
Builds a reactive output to display in the UI
Where* is the type of object to build and inside the parenthesis is the code block that builds the object ({})
What is reactivity?
The application automatically updates its UI in response to changes in data or events, without requiring explicit manual updates.
It occurs whenever you use an input value to render an output object.
Which part of the shiny app is an observer object?
The reactive endpoint
What are inputs and outputs in terms of reactivity?
input - reactive value
output - reactive function
When does the input value change?
Whenever a user changes the input control
How do you call a reactive value?
Reactive values act as the data streams that flow through the app.
It has to be called in a reactive function. ie render*()
Otherwise will get an error message to tell you.
If you think of reactivity in R as a two step process, what are the steps?
1 - reactive values notify the functions that use them when they become invalid
2 - the objects created by reactive functions respond
How do you outputcomputed statistics?
verbatimTextOutput(“stats”) in UI
output$stats <- renderPrint ({
summary(rnorm(input$num))
})
If we have multiple outputs what should we use?
reactive()
eg
Use reactive() to generate the random sample once
data <- reactive({ rnorm(input$num) })
- This ensures consistency, that they are both using the same dataset
- Modularise the code with reactive()
What does reactive() do?
Builds a reactive object (reactive expression) to use (in downstream code), reactive expression
Why are reactive expressions special?
1 - You call a reactive expression like a function eg data()
2 - reactive expressions cache their values (expressions will return its most recent value, unless it has become invalidated)
Why might you want to prevent a reaction with isolate()?
Eg example of updating a title - want to prevent the title field from updating when the user enters different text
What does isolate() do?
Turns an object into a non-reactive object
Use isolate() to treat reactive objects
like normal R objects
Allows you to access a reactive value or expression without triggering a reactive dependency.
It is mainly used inside observe(), observeEvent(), and eventReactive().
How can we trigger code?
With observeEvent()
What does acionButton() do?
Takes action in response to a specific action
UI:
actionButton(inputId = “clicks”,
label = “Click me”)
SERVER:
observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})
We create an observer object to respond to the action button
How do you print to console the number of times a button has been clicked?
observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})
Need to specify precisely which reactive values should notify the observer ie input$clicks
What does observeEvent() do?
Take action in response to a specific event
What does observe() do?
Takes action in response to input changes
Creates an observer object which may not display anything
How do we delay reactions?
with eventReactive()
This is a reactive expression that only responds to a specific event - the event mentioned before {} eg input$go
Why would we want to use eventReactive()?
To delay a reaction eg prevent a graph from updating until we hit a button
How do you implement event reactive?
eg actionButton(inputId = “
go”,
label = “Update”)
SERVER:
data <- eventReactive(input$go, {
rnorm(input$num)
})
output$hist <- renderPlot({
hist(data())
})
How can you manage state?
reactiveValues()
What does reactiveValues() do?
Creates a list of reactive values to manipulate programmatically.
eg
# Create a reactiveValues object with an initial count of 0
values <- reactiveValues(count = 0)
Can increment this count when a button is clicked etc.
THIS IS IN THE SERVER
How do you manipulate reactiveValues()?
Usually with observeEvent()
How can you create distributions?
rnorm or runoff
What do you use render() for?
To make an object to display in the UI
What do you use reactive() for?
To make an object to use in downstream code
What do you use isolate() for?
To make an object to use in downstream code
What do you use eventReactive () for?
To delay a reaction
What do you use observeEvent() for?
To trigger code
What do you use reactiveValues() for?
To make your own reactive values
Discuss reactive expressions vs observers.
Reactive expressions are callable, return a value, are lazy (passive) and are cached.
Reactive expressions are used for calculations whereas observers are used for side effects.
What builds the user interface for the Shiny app?
HTML
ui <- fluidPage() is container-fluid
How do you create a heading in the Shiny App?
tags$h1()
How do you create an anchor element in the Shiny app?
tags$a()
How can you see what html tags are available?
names(tags)
How can you add a link to the code
tags$a(href = “url”, “Text to Appear”)
How do you insert text with HTML?
Character strings do not need a tag, can just type “….”
Can neaten the content by adding a new paragraph
tags$p(“Text”)
How can you put something in italics?
tags$em(“Text”)
How can you put something in bold?
tags$strong(“Text”)
How can you add monospaced text (code)?
tags$code(‘code”)
How do you add a bold word within a sentence?
You can nest the functions inside of others
tags$p(“This is a”,
tags$strong(“Shiny”),
“app.”)
How do you insert a line break?
tags$br()
How can you add a horizontal line?
tags$hr()
for horizontal rule
How can you add an image?
tags$img(src = “www”, width = 10, height = 10)
Use the source argument to point to the image URL
To add an image from a saved location, how do you need to save it?
Create a folder called “www” in the same directory as app.R and place the image in this. Then call src = “filename.png”
What function creates a hyperlink?
a()
How do you change the font of text?
Within the p() element
p(style = “font-family:Impact”,
“Text”)
How can you pass in a character string as raw HTML?
HTML()
eg HTML(“<h1>My Shiny App</h1>”)
How do you position elements within an app?
Using layout functions
This divides the UI into a grid of rows and columns
Within the layout, how many columns are in each row?
12
How do you divide the UI into a grid of rows and columns
fluidRow()
column(width = 2)
What does fluidRow() do?
Adds rows to the grid.
Each new row goes below the previous rows (“fluid”)
What does column do?
Adds columns within a row. Each new column goes to the right of the previous column
column(3)
What do you specify within a column() call?
Specify the width and optionally the offset
eg column(4, offset = 8)
How do you place an element in the grid?
Call it as an argument of a layout function
eg fluidRow(
“In the row”
}
What is a panel?
A single unit with its own properties, which contains multiple elements
What kinds of panels are there?
- tabPanel()
We can combine tabPanel() with one of:
- tabsetPanel()
- navlistPanel()
What is tabPanel()?
tabPanel() creates a stackable layer of elements, called a tab.
Each tab is like a small UI of its own.
tabPanel(“Title” …. [elements of the tab]….)
What does tabsetPanel() do?
Combines tabs into a single panel. You can us the tab key to navigate between tabs.
tabsetPanel(
tabPanel(“Tab 1”, “Contents”),
tabPanel(“Tab 2”, “Contents”),
tabPanel(“Tab 3”, “Contents”)
)
What is the difference between tabsetPanel() and navlistPanel()?
- Tabset appears across top
- Use tabs to navigate between tabs
- Navlist appears on side
- Uses links to navigate between tabs
What does navlistPanel() do?
Combines tabs into a single panel. Use links to navigate between tabs.
Arrange tab panels into a stack with sidebar navigations
navlistPanel(
tabPanel(“Tab 1”, “contents”),
tabPanel(“Tab 2”, “contents”),
tabPanel(“Tab 3”, “contents”)
)
How do you group elements into a grey box?
wellPanel()
Groups elements into a grey “well”
What kinds of prepackaged layouts can we use?
flowLayout()
sidebarLayout()
splitLayout()
How can we utilise sidebarLayout() to divide an app into two sections?
sidebarLayout(
sidebarPanel(),
mainPanel()
)
What is an alternative to fluidPage()?
navbarPage(title = “Title”,
tabPanel(“tab 1”, “contents”)
)
navbarPage() combines tabs into a single page
How can we combine tab links into a dropdown menu for navbarPage()?
navbarMenu()
navbarMenu(title = “More”,
tabPanel(“tab 1”, “contents”)
)
What is a pre-made dashboard template we can use?
These come from the shinydashboard package
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
How can we segment elements within a row?
box()
treat the same as column
Within dashboardSidebar() how can we add a menu?
sidebarMenu(
)
and include menuItem()
How can we add icons?
icon = icon(“dashboard”)
How do you create a fixed (non-fluid) design?
Use fixedPanel() with fixedRow()
If giving a numeric input, what should you include?
Minimum and maximum values
eg min = 1 and max = nrow(dataset)
How can you process a dataset based on desired criteria?
Use a reactive function
eg movies_to_display <- reactive({
req(input$type, input$sample_size) # ensure availablity of value before proceeding
filter(movies, title_type %in% input$type) %>%
sample_n(input$sample_size)
})
How do you include a data table output?
library(DT)
DT::dataTableOutput() in UI
DT::datatable in Server
How do you need to pass variables into the aesthetic?
aes_string()
How do you need to call the processed dataset?
Assuming it was processed with reactive(), need to call it as a function data()
How do you print a statement about your processed data?
Need a dynamic UI element.
UI:
uiOutput(“n”)
SERVER:
output$n <- renderUI({
types <- movies_to_display()$title_type %>%
factor(levels = input$type)
counts <- table(types) p("There are ", counts, input$type, " movies in this dataset.") })
In a sliderInput, how do you ensure that integers are treated as years, ie presented as XXXX rather than X,XXX?
sep = “”
How do you print output from R functions (like summary(), str(), table())
renderPrint() - pass to verbatimTextOutput
How do you add conditioning to the panel displayed, dependent on the panel you are on?
conditionalPanel() - wrap the “conditional” parts in here and add condition = “input.main ==1” at the start
within tabsetPanel in the main panel, add “id = “main””, and add “value = X” to each of the tabPanels(), where value = 1 for the tab you want to have the conditional elements displayed for.
What library do you need to have to ensure you have the iris dataset?
library(dplyr) or library(ggplot2)
How do you cluster the data for use in a plot?
data <- reactive({
req(input$x, input$y)
iris[, c(input$x, input$y)]
})
clusters <- reactive({
req(input$count)
kmeans(data(), input$count)
})
Then in the plot
geom_point(color = factor(clusters()$cluster))
What can you use reactiveValues() and observeEvent() together for?
Eg update a value on a button click
rv <- reactiveValues(
norm = rnorm(500),
unif = runif(500),
chisq = rchisq(500, 2)
)
observeEvent(input$renorm, {
rv$norm <- rnorm(500)
})
How do you have a multi-drop selection within a tab panel?
navbarMenu() - add a title and tabpanel() s for each drop down option
When you have two reactive outputs, how could you restructure the code?
data <- reactive({
rnorm(input$num)
})
output$hist <- renderPlot({
hist(data())
})
output$stats <- renderPrint({
summary(data())
})
Explain what isolate does in this context:
output$hist <- renderPlot({
hist(rnorm(input$num), main = isolate(input$title))
})
The title will only be updated if num changes.
How do you print to console the number of times a button is clicked?
observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})
What is the difference between reactive() and eventReactive()?
Use reactive() if you want the histogram to update automatically when input$num changes.
Use eventReactive() if you want the user to click a button to trigger updates, preventing unnecessary re-computation.