class: center, middle, inverse, title-slide # Faceting ### Derek Ogle, May 2020 --- class: inverse, center, middle # Today's Goal <font size="7">Highlight differences in subgroups by efficiently placing subgroups into separate panels.</font> --- # Background - Historical temperature data from Ashland's airport. - From NOAA's [Climate Data Online app](https://www.ncdc.noaa.gov/cdo-web/) and [saved here](https://raw.githubusercontent.com/droglenc/NCData/master/Ashland_Temps_2001_20.csv). -- - Loaded data into the `ash` object. - Made all variables lower case (`tolower` in `rename_all()`). - Made a proper date (with `as.Date`) - Extracted year and month (with `year()` and `month()`). - Created a "season" variable. - Restricted variables (with `select()`) and to only 2010-2019 (with `filter()`). ```r ## #!# Set to your own working directory and have just your filename below. ash <- read.csv("https://raw.githubusercontent.com/droglenc/NCData/master/Ashland_Temps_2001_20.csv", stringsAsFactors=FALSE) %>% rename_all(tolower) %>% mutate(date=as.Date(date,format="%m/%d/%Y"),year=lubridate::year(date),mon=lubridate::month(date,label=TRUE), season=case_when( mon %in% c("Jan","Feb","Mar") ~ "Winter",mon %in% c("Apr","May","Jun") ~ "Spring", mon %in% c("Jul","Aug","Sep") ~ "Summer",mon %in% c("Oct","Nov","Dec") ~ "Fall"), season=factor(season,levels=c("Winter","Spring","Summer","Fall"))) %>% select(-(station:elevation)) %>% filter(date>=as.Date("1/1/2010",format="%m/%d/%Y"),date<=as.Date("12/31/2019",format="%m/%d/%Y")) ``` --- - Examine the resulting data.frame. ```r head(ash) ``` ``` date prcp tavg tmax tmin year mon season 1 2010-01-01 0 NA 12 -10 2010 Jan Winter 2 2010-01-02 0 NA 4 -16 2010 Jan Winter 3 2010-01-03 0 NA 10 1 2010 Jan Winter 4 2010-01-04 0 NA 17 0 2010 Jan Winter 5 2010-01-05 0 NA 21 -1 2010 Jan Winter 6 2010-01-06 0 NA 22 2 2010 Jan Winter ``` -- - Create a named color palette. ```r clrs <- c("Winter"="dodgerblue2","Spring"="springgreen3","Summer"="darkgoldenrod3","Fall"="sienna3") ``` --- class: inverse, center, middle ### Exploratory Plot 1 --- class: split-50 count: false .column[.content[ ```r tp <- ggplot(data=ash,mapping=aes(y=tmax,x=date)) + geom_point(mapping=aes(color=season),size=0.3,alpha=0.5) + geom_smooth(size=0.5,color="black") + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") tp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_scat1_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r tp <- ggplot(data=ash,mapping=aes(y=tmax,x=date)) + geom_point(mapping=aes(color=season),size=0.3,alpha=0.5) + geom_smooth(size=0.5,color="black") + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + * scale_x_date( * name="Date",expand=expansion(mult=0.02), ) + theme_bw() + * theme(panel.grid.minor.y=element_blank(),legend.position="none") tp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_scat1_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r tp <- ggplot(data=ash,mapping=aes(y=tmax,x=date)) + geom_point(mapping=aes(color=season),size=0.3,alpha=0.5) + geom_smooth(size=0.5,color="black") + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + scale_x_date( name="Date",expand=expansion(mult=0.02), * date_breaks="1 year",date_labels="%Y", ) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") tp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_scat1_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r tp <- ggplot(data=ash,mapping=aes(y=tmax,x=date)) + geom_point(mapping=aes(color=season),size=0.3,alpha=0.5) + geom_smooth(size=0.5,color="black") + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + scale_x_date( name="Date",expand=expansion(mult=0.02), date_breaks="1 year",date_labels="%Y", * date_minor_breaks="3 months" ) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") tp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_scat1_non_seq_4_output-1.png" width="100%" /> ]] --- class: inverse, center, middle ### Exploratory Plot 2 --- class: split-50 count: false .column[.content[ ```r vp <- ggplot(data=ash,mapping=aes(y=tmax,x=as.factor(year), color=season,fill=season)) + geom_violin(alpha=0.5,color="gray30",size=0.25, ) + stat_summary(fun=mean,geom="point",size=0.7, ) + scale_fill_manual(values=clrs) + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + scale_x_discrete(name="Year",expand=expansion(mult=0.05)) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") vp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_viol1_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r vp <- ggplot(data=ash,mapping=aes(y=tmax,x=as.factor(year), color=season,fill=season)) + geom_violin(alpha=0.5,color="gray30",size=0.25, * position=position_dodge(width=0.8) ) + stat_summary(fun=mean,geom="point",size=0.7, * position=position_dodge(width=0.8) ) + scale_fill_manual(values=clrs) + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)", breaks=seq(-20,100,10)) + scale_x_discrete(name="Year",expand=expansion(mult=0.05)) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") vp ``` ]] .column[.content[ <img src="Lecture_Facets_files/figure-html/ash_viol1_non_seq_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle ### Faceting to Highlight Subgroups --- # Faceting - Creates a separate panel for each subgroup. -- - Use `facet_wrap()` if subgroups defined by one variable. -- - Use `facet_grid()` if subgroups defined by two (or more) variables. -- - Faceting variable must be wrapped in `vars()` in both `facet_XXX()`s. --- ```r tp + facet_wrap(vars(year)) ``` -- <img src="Lecture_Facets_files/figure-html/unnamed-chunk-6-1.png" width="80%" /> --- # Modifications to `facet_wrap()` - Use `nrow=` and `ncol=` to control number of rows and columns for the panels. - Can supply just one and the other will be calculated. -- - Use `scales=` to identify whether axis scales are `fixed` (default) or "free". -- - `scales="free"`: Both x- and y-axes will vary by panel. - `scales="free_x`: Only the x-axes will vary by panel. - `scales="free_y`: Only the y-axes will vary by panel --- ```r tp + facet_wrap(vars(year),scales="free_x",nrow=2) ``` <img src="Lecture_Facets_files/figure-html/unnamed-chunk-8-1.png" width="100%" height="60%" /> --- class: inverse, center, middle ### May need to think ahead to what faceting will look like when making other design choices (i.e., you can't always automatically add facet_XXX() and have the graph look the way you want it to) --- ```r ggplot(data=ash,mapping=aes(y=tmax,x=date)) + geom_point(mapping=aes(color=season),size=0.3,alpha=0.5) + geom_smooth(size=0.5,color="black") + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)",breaks=seq(-20,100,20)) + scale_x_date(name=element_blank(),expand=expansion(mult=0.02), date_breaks="3 months",date_labels="%b",date_minor_breaks="1 months") + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") + facet_wrap(vars(year),scales="free_x",nrow=2) ``` <img src="Lecture_Facets_files/figure-html/ash_scat3-1.png" width="100%" height="60%" /> --- ```r ggplot(data=ash,mapping=aes(y=tmax,x=season,color=season,fill=season)) + geom_violin(alpha=0.5,color="gray30",size=0.25) + stat_summary(fun=mean,geom="point",size=0.7) + scale_fill_manual(values=clrs) + scale_color_manual(values=clrs) + scale_y_continuous(name="Maximum Temperature (F)",breaks=seq(-20,100,20)) + scale_x_discrete(name=element_blank(),expand=expansion(mult=0.05)) + theme_bw() + theme(panel.grid.minor.y=element_blank(),legend.position="none") + facet_wrap(vars(year),scale="free_x",nrow=2) ``` <img src="Lecture_Facets_files/figure-html/ash_viol2a-1.png" width="100%" height="60%" /> --- class: inverse, center, middle # Using facet_grid() with two grouping variables --- # Background - Examine the relationship between the total volume of *organic* avocados sold and the average price of an avocado, by years and region (Great Lakes or West). -- - Loaded the data into the `avoc` object. ```r ## #!# Set to your own working directory and have just your filename below. avoc <- read.csv("https://raw.githubusercontent.com/droglenc/NCData/master/Avocados.csv", stringsAsFactors=FALSE,quote="") %>% filter(region %in% c("GreatLakes","West"),type=="organic") %>% mutate(region=factor(region),year=factor(year)) %>% select(year,region,type,Total.Volume,AveragePrice) head(avoc) ``` ``` year region type Total.Volume AveragePrice 1 2015 GreatLakes organic 66775.85 1.49 2 2015 GreatLakes organic 62669.16 1.55 3 2015 GreatLakes organic 65341.66 1.53 4 2015 GreatLakes organic 71502.74 1.60 5 2015 GreatLakes organic 59169.17 1.61 6 2015 GreatLakes organic 67773.07 1.57 ``` --- ```r ggplot(data=avoc,mapping=aes(y=Total.Volume,x=AveragePrice)) + geom_point(size=0.7,alpha=0.5) + geom_smooth(color="red",size=0.5,se=FALSE) + facet_grid(rows=vars(region),cols=vars(year)) + scale_x_continuous(name="Average Price ($)",labels=scales::dollar) + scale_y_continuous(name="Thousands of Bags Sold",limits=c(0,NA),expand=expansion(mult=c(0,0.02)), labels=scales::unit_format(unit="",scale=1/1000)) + theme_bw() + theme(panel.grid.minor=element_blank()) ``` <img src="Lecture_Facets_files/figure-html/avoc1-1.png" width="100%" height="60%" /> --- class: inverse, center, middle # Demo of a Useful Add-on Package --- ```r ggplot(data=avoc,mapping=aes(y=Total.Volume,x=AveragePrice,color=year:region)) + geom_point(size=0.7,alpha=0.5) + scale_color_manual(values=rep("black",8)) + gghighlight() + facet_grid(rows=vars(region),cols=vars(year)) + scale_x_continuous(name="Average Price ($)",labels=scales::dollar) + scale_y_continuous(name="Thousands of Bags Sold",limits=c(0,NA),expand=expansion(mult=c(0,0.02)), labels=scales::unit_format(unit="",scale=1e-3)) + theme_bw() + theme(panel.grid.minor=element_blank(),legend.position="none") ``` <img src="Lecture_Facets_files/figure-html/avoc2-1.png" width="100%" height="60%" /> --- class: inverse, center, middle # Next Time <font size="7">We will examine how to control all aspects of a plot.</font>