the$grammar$of$graphics$ - about...
TRANSCRIPT
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
The Grammar of Graphics
A. Michelle Lawing Ecosystem Science and Management Texas A&M University College Sta,on, TX 77843 [email protected]
michellelawing.info/Rcode
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
ANNOUNCEMENT
• Ecological Integra,on Symposium – 26 March – 9:15-‐10:00am – Talks Session 1
• Room 404 – Public Health & Disease • Room 401 – Hybrid Zones & Selec,on • Room 402 – Honey Bees
– 11:00am-‐12:00pm – Talks Session 2 • Room 404 – Changing Landscapes • Room 401 – From Communi,es to Popula,ons • Room 402 – Entomology
– 1:00-‐2:15pm – Talks Session 3 • Room 404 – Ecosystem Ecology • Room 401 – Phylogene,cs & Evolu,on • Room 402 – Habitat & Spa,al Ecology
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
ANNOUNCEMENT
• Ecological Integra,on Symposium – 27 March – 9:10-‐10:10am – Dr. Paul Gibbs -‐ One health, the socio-‐ecology of disease, and epidemic preven,on
– 10:10-‐11:10am – Dr. Maureen Donnelly -‐ Amphibian conserva,on in the New World Tropics: A tragedy in three acts
– 11:30am-‐12:30pm – Dr. Parviez Hosseini -‐ Understanding the role of land-‐use change and ecological community structure in pathogen emergence
– 2:00-‐3:00pm – Dr. Tyrone Hayes -‐ From Silent Spring to Silent Night: A tale of toads and men
– 3:00-‐4:00pm – Dr. Rosemary Grant -‐ Lessons from a natural ecosystem
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Schedule for today
• My presenta,on
• Survival Analysis Presenta,on
• Survival Analysis Demonstra,on
• BREAK
• Survival Analysis Tutorial
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Visualizing your data
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Visualizing your data
• Most important tool in data analysis
• To understand your data
• To share your results
• DEMONSTRATE
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
ggvis
• Hadley Wickham (Rice Uni) • Grammar of graphics – A way to think about ANY graph – A collec,on of parts (think of legos) graph = data + coordinate system + marks + proper,es – Extends ggplot2 – Uses R to build html graphs
• Use analy,c power of R to prepare data • Visual power of java script to display your results • Interac,ve data display • Output in html, so can use in shiny, R markdown, web page…
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
get ggvis
• Today we will cover the breadth and depth of ggvis graphing language
• Learn grammar of graphics • Learn to customize your ggvis plots > install.packages("ggvis") > library(ggvis)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
change iris names for simplicity
> install.packages("ggvis") > library(ggvis) > names(iris) [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
> names(iris) <-‐ c("SL", "SW", "PL", "PW", "Species")
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
grammar of graphics
• Every ggvis graphic starts with a call to ggvis() • 1st argument is the dataset
• Other arguments describe how to map variables and visualize proper,es
> p <-‐ ggvis(iris, x = ~SL, y = ~SW)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
add a layer of points to your scaffold
> p <-‐ ggvis(iris, x = ~SL, y = ~SW)
> layer_points(p) • Either create a temporary variable, then modify it or use lots of parentheses
> layer_points(ggvis(iris, x = ~SL, y = ~SW))
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
the plot opens in viewer (Rstudio) or browser (R)
> layer_points(ggvis(iris, x = ~SL, y = ~SW))
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
piping in the data
> layer_points(ggvis(iris, x = ~SL, y = ~SW))
• pronounced pipe %>% • allows you to rewrite the above func,on call as…
> iris %>% ggvis(~SL, ~SW) %>% layer_points()
• no,ce we dropped the argument name x and y
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
the ,lde > iris %>% ggvis(~SL, ~SW) %>% layer_points()
• why use a ,lde?
• the ,lde specifies that you are using a variable WITHIN the given dataset.
• this is necessary for ggvis to run and MUST be used this way
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
some op,ons for your points > iris %>% ggvis(~SL, ~SW) %>% layer_points()
• you can add more variables to the plot – fill – stroke – size – shape
> iris %>% ggvis(~SL, ~SW, size = ~PL) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
size gradient > iris %>% ggvis(~SL, ~SW, size = ~PL) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
no,ce this symbol for export > iris %>% ggvis(~SL, ~SW, size = ~PL) %>% layer_points()
Render: SVG | Canvas
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
try fill
> iris %>% ggvis(~SL, ~SW, fill = ~Species) %>% layer_points()
> iris %>% ggvis(~SL, ~SW, size = ~PL) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
try stroke
> iris %>% ggvis(~SL, ~SW, stroke = ~Species) %>% layer_points()
> iris %>% ggvis(~SL, ~SW, fill = ~Species) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
try shape
> iris %>% ggvis(~SL, ~SW, shape = ~Species) %>% layer_points()
> iris %>% ggvis(~SL, ~SW, stroke = ~Species) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
constant color
> iris %>% ggvis(~SL, ~SW, fill := “red”) %>% layer_points()
> iris %>% ggvis(~SL, ~SW, shape = ~Species) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
a special way to specify a constant > iris %>% ggvis(~SL, ~SW, fill := “red”) %>% layer_points()
• note that := must be used to specify a constant
> iris %>% ggvis(~SL, ~SW, size := 1000, opacity := 0.4) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
interac,vity
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
basic interac,ve controls
> iris %>% ggvis(~SL, ~SW, size := 1000, opacity := 0.4) %>% layer_points()
• replace constant values with a func,on • input_slider()
> iris %>% ggvis(~SL, ~SW, size := input_slider(100, 1000)) %>% layer_points()
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
viewing the graphic
> iris %>% ggvis(~SL, ~SW, size := input_slider(100, 1000)) %>% layer_points()
• from the R, graph opens in web browser • resize the window to see slider • press esc to exit interac,vity in Rstudio
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
input_
> iris %>% ggvis(~SL, ~SW, size := input_slider(100, 1000)) %>% layer_points() • Other basic interac,ve controls
– input_checkbox(): a check-‐box – input_checkboxgroup(): a group of check boxes – input_numeric(): a spin box – input_radiobuzons(): pick one from a set op,ons – input_select(): create a drop-‐down text box – input_text(): arbitrary text input
• Note that all interac,ve controls start with input_ so you can use tab comple+on to discover all the op,ons
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
two interac,ve controls > iris %>% ggvis(~SL, ~SW, size := input_slider(100, 1000)) %>% layer_points() > iris %>% ggvis(~SL) %>%
layer_densi,es(adjust = input_slider(.1, 2, label = "Bandwidth"), kernel = input_select(c("Gaussian" = "gaussian”, "Rectangular" = "rectangular”, "Triangular" = "triangular”), label = "Kernel"))
• note switching between = and := can be confusing • read the help pages to know which to use when
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
arguments
• specific arguments differ depending on input_ • all input_ func,ons share three arguments – label: an informa,ve text label displayed next to the control
– id: a unique iden,fier for the control – map: a func,on that enables you to remap the output of the input to something appropriate for your graphic
• map is extremely useful (e.g., you can make different orders of magnitude visible with a slider)
• input_slider(-‐5, 5, map = func,on(x) 10 ^ x)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
mul,ple outputs
> #first method with pronounced pipe > iris %>% ggvis(~SL, ~SW, size := input_slider(10, 1000)) %>%
layer_points(fill := "red") %>% layer_points(stroke := "black", fill := NA)
> #second method with assignment > slider <-‐ input_slider(10, 1000) > iris %>% ggvis(~SL, ~SW) %>% layer_points(fill := "red", size :=
slider) %>% layer_points(stroke := "black", fill := NA, size := slider)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
limita,ons
• interac,ve inputs can only modify your data, not the underlying plot specifica,ons
• can’t add or remove layers • can’t switch between datasets
• but you can embed in shiny to gain full control • shiny is an extension of R to create interac,ve web apps
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing