Visual Analytics Flashcards

(170 cards)

1
Q

What are geoms?

A

Visual marks representing data points

Geoms, short for “geometric objects” are the basic visual element used to create plots.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why does ggplot(…) do?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What layer creates a scatterplot?

A

geom_point()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are scatterplots useful for?

A

Displaying the relationship between two continuous variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you colour points by a variable?

A

Within aes()

color = factor(var)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you plot a line graph?

A

geom_line()

Useful for trend lines and time-series

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you plot a box plot?

A

geom_boxplot()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many geoms should you have?

A

Need to have at least one, there is not upper limit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does geom_smooth() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What kinds of variables can you include in geom_point()

A

Map variables to aesthetics

  • colour
  • shape
  • size
  • alpha
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you display the definition of a function eg geom_bar and its default parameter values?

A

args() function

eg args(geom_bar)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What kinds of arguments can be customised in geom_histogram()?

A

+ geom_histogram(stat = “bin”, binwidth = 3)

stat = “bin” is the default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are “scales”?

A

Functions that control how the data is mapped to visual properties
eg position (x and y axis), colour, shape or zie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does scale_color_brewer() do and how is it used?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you import ggplot?

A

library(ggplot2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If using shapes to represent a categorical variable, how do you change the shapes from the default?

A

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’…..))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does adding scale_size(range = c(1, 10)) do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does facet do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you facet a plot?

A

+ facet_wrap(~var, ncol=3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is theme in ggplot?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you make the shapes into line shapes?

A

+ scale_shape(solid = FALSE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you change the colour and size of plotted points?

A

+ geom_point(shape = 21, colour = “black”, fill = “white”, size = 5, stroke = 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you add a theme?

A

Add to the document

theme_set(theme_bw())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you set limits on the x or y axis?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you add a theme to a graph?
plot + theme_bw()
26
How do you add labels to your plot?
+ labs() can set eg - title - subtitle - y - x - caption
27
What does stroke control?
The thickness of the point boundary
28
How do you change the colour of text on a plot?
+ theme(text = element_text(colour = "blue"))
29
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))
30
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))
31
How do you rotate the angle of the axis?
+ theme(axis.text.x = element_text(angle = 45))
32
How do you flip the x and y axis?
+ coord_flip()
33
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)
34
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.
35
How can you arrange multiple plots?
library(gridExtra) grid.arrange(p1,p2,ncol=2)
36
How can you match the colour of the smooth added to the categorical variable?
+ geom_smooth(aes(color=color))
37
How do you add a horizontal line to a plot?
geom_hline() eg geom_hline(yintercept=5000, size=2, linetype="dotted", color="blue")
38
How do you add vertical line to a plot?
geom_vline() eg + geom_vline(xintercept=4, size=2, color="firebrick")
39
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)
40
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
41
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.
42
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
43
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)
44
How do you convert from Wide Format to Long Format using melt()?
reshape2::melt eg reshape2::melt(economics[, c("date", "psavert", "uempmed")], id="date")
45
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()
46
What two ways can you plot multiple variables on an area plot?
Non-overlapping (stacked) or overlapping
47
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
48
What must app.R end with?
A call to shinyApp()
49
How do you include a sliding select scale?
sliderInput( inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100 )
50
How do you add a graph?
Use of plotOutput and renderPlot UI side: plotOutput("hist") Server side: output$hist <- renderPlot({ hist(rnorm(input$num)) )}
51
How can you incorporate a button?
actionButton() submitButton()
52
How do you include a single check box?
checkboxInput()
53
How do you include multiple checkboxes in a group?
checkboxGroupInput()
54
How do you include a date?
dateInput()
55
How do include a date range?
dateRangeInput()
56
How do you include a file?
fileInput()
57
How do you create a drop down of numbers?
numericInput()
58
How do you include a Password field?
passwordInput()
59
How do you include radio buttons?
radioButtons()
60
How do you include a drop down selection?
selectInput()
61
How do you include a slider?
sliderInput()
62
How do you include a text field?
textInput()
63
How do you create an input?
with an *Input() function
64
What is the inputId used for?
For internal use
65
What is the label used for?
Used to display
66
How do you insert an interactive table?
dataTableOutput()
67
How do you insert raw HTML?
htmlOutput()
68
How do you insert an image?
imageOutput()
69
How do you insert a plot?
plotOutput()
70
How do you insert a tableOutput()?
tableOutput()
71
How do you insert a Shiny UI element?
uiOutput()
72
How do you insert text?
verbatimTextOutput()
73
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")
74
What do you need to have between arguments?
Commas
75
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.
76
What do we tell the server?
How to assemble inputs into outputs
77
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$
78
How do you create an interactive table (from a data frame, matrix or other table-like structure)?
renderDataTable()
79
How do you create a code block of printed output?
renderPrint()
80
How do you create a character string?
renderText()
81
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 ({})
82
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.
83
Which part of the shiny app is an observer object?
The reactive endpoint
84
What are inputs and outputs in terms of reactivity?
input - reactive value output - reactive function
85
When does the input value change?
Whenever a user changes the input control
86
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.
87
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
88
How do you outputcomputed statistics?
verbatimTextOutput("stats") in UI output$stats <- renderPrint ({ summary(rnorm(input$num)) })
89
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()
90
What does reactive() do?
Builds a reactive object (reactive expression) to use (in downstream code), reactive expression
91
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)
92
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
93
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().
94
How can we trigger code?
With observeEvent()
95
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
96
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
97
What does observeEvent() do?
Take action in response to a specific event
98
What does observe() do?
Takes action in response to input changes Creates an observer object which may not display anything
99
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
100
Why would we want to use eventReactive()?
To delay a reaction eg prevent a graph from updating until we hit a button
101
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()) })
102
How can you manage state?
reactiveValues()
103
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
104
How do you manipulate reactiveValues()?
Usually with observeEvent()
105
How can you create distributions?
rnorm or runoff
106
What do you use render() for?
To make an object to display in the UI
107
What do you use reactive() for?
To make an object to use in downstream code
108
What do you use isolate() for?
To make an object to use in downstream code
109
What do you use eventReactive () for?
To delay a reaction
110
What do you use observeEvent() for?
To trigger code
111
What do you use reactiveValues() for?
To make your own reactive values
112
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.
113
What builds the user interface for the Shiny app?
HTML ui <- fluidPage() is container-fluid
114
How do you create a heading in the Shiny App?
tags$h1()
115
How do you create an anchor element in the Shiny app?
tags$a()
116
How can you see what html tags are available?
names(tags)
117
How can you add a link to the code
tags$a(href = "url", "Text to Appear")
118
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")
119
How can you put something in italics?
tags$em("Text")
120
How can you put something in bold?
tags$strong("Text")
121
How can you add monospaced text (code)?
tags$code('code")
122
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.")
123
How do you insert a line break?
tags$br()
124
How can you add a horizontal line?
tags$hr() for horizontal rule
125
How can you add an image?
tags$img(src = "www", width = 10, height = 10) Use the source argument to point to the image URL
126
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"
127
What function creates a hyperlink?
a()
128
How do you change the font of text?
Within the p() element p(style = "font-family:Impact", "Text")
129
How can you pass in a character string as raw HTML?
HTML() eg HTML("

My Shiny App

")
130
How do you position elements within an app?
Using layout functions This divides the UI into a grid of rows and columns
131
Within the layout, how many columns are in each row?
12
132
How do you divide the UI into a grid of rows and columns
fluidRow() column(width = 2)
133
What does fluidRow() do?
Adds rows to the grid. Each new row goes below the previous rows ("fluid")
134
What does column do?
Adds columns within a row. Each new column goes to the right of the previous column column(3)
135
What do you specify within a column() call?
Specify the width and optionally the offset eg column(4, offset = 8)
136
How do you place an element in the grid?
Call it as an argument of a layout function eg fluidRow( "In the row" }
137
What is a panel?
A single unit with its own properties, which contains multiple elements
138
What kinds of panels are there?
- tabPanel() We can combine tabPanel() with one of: - tabsetPanel() - navlistPanel()
139
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]....)
140
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") )
141
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
142
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") )
143
How do you group elements into a grey box?
wellPanel() Groups elements into a grey "well"
144
What kinds of prepackaged layouts can we use?
flowLayout() sidebarLayout() splitLayout()
145
How can we utilise sidebarLayout() to divide an app into two sections?
sidebarLayout( sidebarPanel(), mainPanel() )
146
What is an alternative to fluidPage()?
navbarPage(title = "Title", tabPanel("tab 1", "contents") ) navbarPage() combines tabs into a single page
147
How can we combine tab links into a dropdown menu for navbarPage()?
navbarMenu() navbarMenu(title = "More", tabPanel("tab 1", "contents") )
148
What is a pre-made dashboard template we can use?
These come from the shinydashboard package library(shinydashboard) ui <- dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody() )
149
How can we segment elements within a row?
box() treat the same as column
150
Within dashboardSidebar() how can we add a menu?
sidebarMenu( ) and include menuItem()
151
How can we add icons?
icon = icon("dashboard")
152
How do you create a fixed (non-fluid) design?
Use fixedPanel() with fixedRow()
153
If giving a numeric input, what should you include?
Minimum and maximum values eg min = 1 and max = nrow(dataset)
154
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) })
155
How do you include a data table output?
library(DT) DT::dataTableOutput() in UI DT::datatable in Server
156
How do you need to pass variables into the aesthetic?
aes_string()
157
How do you need to call the processed dataset?
Assuming it was processed with reactive(), need to call it as a function data()
158
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.") })
159
In a sliderInput, how do you ensure that integers are treated as years, ie presented as XXXX rather than X,XXX?
sep = ""
160
How do you print output from R functions (like summary(), str(), table())
renderPrint() - pass to verbatimTextOutput
161
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.
162
What library do you need to have to ensure you have the iris dataset?
library(dplyr) or library(ggplot2)
163
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))
164
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) })
165
How do you have a multi-drop selection within a tab panel?
navbarMenu() - add a title and tabpanel() s for each drop down option
166
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()) })
167
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.
168
How do you print to console the number of times a button is clicked?
observeEvent(input$clicks, { print(as.numeric(input$clicks)) })
169
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.
170