class: center, middle, inverse, title-slide # Graphing Univariate Densities ### Derek Ogle, May 2020 --- # Background - Morphological measurements of Florida Black Bear (*Ursus americanus floridanus*). - Published in [Bartareau, T.M. 2017. Estimating the live body weight of American Black Bears in Florida. Journal of Fish and Wildlife Management 8:234-239](https://www.fwspubs.org/doi/10.3996/012016-JFWM-003). - Data provided as a supplement. .center[ <img src="https://derekogle.com/NCGraphing/modules/zimgs/blackbear.jpg" width="500px" /> <font size="1">Credit: <a href="https://news.illinois.edu/view/6367/803074">University of Illinois</a></font> ] --- # Background - Downloaded 'fwma-08-01-02_table s1.xls' Excel file from [Journal of Fish and Wildlife Management](https://www.fwspubs.org/doi/10.3996/012016-JFWM-003) and saved as [BlackBearsFL.csv]("https://raw.githubusercontent.com/droglenc/NCData/master/BlackBearsFL.csv"). -- - Loaded data into `flbb` data.frame. - Renamed some variables to remove spaces and capitalization. - Changed sex labels and made `sex` a factor variable. ```r #!# Set to your own working directory and have just your filename below. flbb <- read.csv("https://raw.githubusercontent.com/droglenc/NCData/master/BlackBearsFL.csv") %>% rename(sex=SEX, age=AGE..yr., length=BODY.LENGTH..cm., weight=BODY.WEIGHT..kg., chest=CHEST.GIRTH..cm.) %>% mutate(sex=plyr::mapvalues(sex, from=c("FEMALE","MALE"), to=c("Female","Male")), sex=factor(sex)) ``` --- # Background ```r head(flbb) ``` ``` DATE sex age weight chest length 1 4/17/2003 Female 0.10 2.47 29.8 39.0 2 3/22/2002 Female 0.17 5.30 29.0 46.5 3 4/8/2003 Female 0.17 5.64 30.2 49.0 4 4/1/2003 Female 0.17 5.85 31.4 50.4 5 7/19/2012 Female 0.42 6.80 39.0 74.0 6 7/19/2012 Female 0.42 13.15 56.2 90.5 ``` - Variables are: - **DATE**: Date of capture. - **sex**: Sex of bear (`Female` or `Male`). - **age**: Estimated age of the bear (years). - **weight**: Body weight of the bear (kg). - **chest**: Chest girth of the bear (cm). - **length**: Body length of the bear (cm). -- - Our focus here will be on `weight` and `sex`. --- # Histogram .pull-left[ - Displays the frequency of observations in "bins" of the quantitative *X* variable. - i.e., the distribution of *X*. ] .pull-right[ <img src="Lecture_BlackBears_files/figure-html/hist_demo-1.png" width="100%" /> ] --- # Histogram .pull-left[ - Constructed with `geom_histogram()`. - `bins=`: Number of bins to use. - `binwidth=`: Width of the bins. - `breaks=`: Bin boundary values. - `boundary=`: Left-most boundary of first bin. - `center=`: Center of first bin. - `closed=`: Are bins closed on `"right"` (*default*) or `"left"`. ] .pull-right[ <img src="Lecture_BlackBears_files/figure-html/unnamed-chunk-4-1.png" width="100%" /> ] --- class: inverse, center, middle # A Mostly Default Histogram --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, mapping=aes(x=weight)) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist1_auto_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + * geom_histogram() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist1_auto_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram() + * scale_y_continuous(name="Frequency of Bears") p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist1_auto_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram() + scale_y_continuous(name="Frequency of Bears") + * scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25)) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist1_auto_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram() + scale_y_continuous(name="Frequency of Bears") + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25)) + * theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist1_auto_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Histogram with Typical Modifications --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( * color="gray30", ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", * fill="gray30", ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", fill="gray30", * alpha=0.25, ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", fill="gray30", alpha=0.25, * binwidth=10, ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_5_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", fill="gray30", alpha=0.25, binwidth=10, * boundary=0,closed="left" ) + scale_y_continuous( name="Frequency of Bears", ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_6_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", fill="gray30", alpha=0.25, binwidth=10, boundary=0,closed="left" ) + scale_y_continuous( name="Frequency of Bears", * expand=expansion(mult=c(0,0.05)) ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_7_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram( color="gray30", fill="gray30", alpha=0.25, binwidth=10, boundary=0,closed="left" ) + scale_y_continuous( name="Frequency of Bears", expand=expansion(mult=c(0,0.05)) ) + scale_x_continuous( name="Weight (kg)",breaks=seq(0,250,25), * expand=expansion(mult=c(0,0)) ) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist2_non_seq_8_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Histogram with Typical Modifications ### Finish by Removing Vertical Grids --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, mapping=aes(x=weight)) + * geom_histogram(color="gray30",fill="gray30",alpha=0.25, * binwidth=10,boundary=0,closed="left") + * scale_y_continuous(name="Frequency of Bears", * expand=expansion(mult=c(0,0.05))) + * scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), * expand=expansion(mult=c(0,0))) + * theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist3_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_histogram(color="gray30",fill="gray30",alpha=0.25, binwidth=10,boundary=0,closed="left") + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + * theme(panel.grid.major.x=element_blank(), * panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/hist3_user_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Stacked Histogram (Split by Groups) --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb,mapping=aes( x=weight, )) + geom_histogram(binwidth=10,boundary=0,closed="left", ) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit1_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb,mapping=aes( x=weight, * color=sex, )) + geom_histogram(binwidth=10,boundary=0,closed="left", ) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit1_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb,mapping=aes( x=weight, color=sex, * fill=sex )) + geom_histogram(binwidth=10,boundary=0,closed="left", ) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit1_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb,mapping=aes( x=weight, color=sex, fill=sex )) + geom_histogram(binwidth=10,boundary=0,closed="left", * alpha=0.25 ) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit1_non_seq_4_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Stacked Histogram (Split by Groups) ### Modify the Default Colors --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, * mapping=aes(x=weight,color=sex,fill=sex)) + * geom_histogram(binwidth=10,boundary=0,closed="left", * alpha=0.25) + * scale_y_continuous(name="Frequency of Bears", * expand=expansion(mult=c(0,0.05))) + * scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), * expand=expansion(mult=c(0,0))) + * theme_bw() + * theme(panel.grid.major.x=element_blank(), * panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit2_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight,color=sex,fill=sex)) + geom_histogram(binwidth=10,boundary=0,closed="left", alpha=0.25) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) + * scale_color_manual(values=c("red","blue")) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit2_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight,color=sex,fill=sex)) + geom_histogram(binwidth=10,boundary=0,closed="left", alpha=0.25) + scale_y_continuous(name="Frequency of Bears", expand=expansion(mult=c(0,0.05))) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) + scale_color_manual(values=c("red","blue")) + * scale_fill_manual(values=c("red","blue")) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/histsplit2_user_3_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Density Plot --- .pull-left[ # Density Plot - Displays the density of observations of the quantitative *X* variable. - i.e., relative distribution of *X*. - A smoothed representation of a histogram. - Constructed with `geom_density()`. - Use `adjust=` to modify "smoothing effect." - Smaller values = less smoothing. ] .pull-right[ <img src="Lecture_BlackBears_files/figure-html/density_demo-1.png" width="100%" /> ] --- class: inverse, center, middle # Density Plot ### With Same Modifications as Histogram --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density( ) + scale_x_continuous( name="Weight (kg)", breaks=seq(0,250,25),expand=expansion(mult=c(0,0)), ) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density1_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density( * color="gray30", ) + scale_x_continuous( name="Weight (kg)", breaks=seq(0,250,25),expand=expansion(mult=c(0,0)), ) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density1_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density( color="gray30", * fill="gray30", ) + scale_x_continuous( name="Weight (kg)", breaks=seq(0,250,25),expand=expansion(mult=c(0,0)), ) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density1_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density( color="gray30", fill="gray30", * alpha=0.25 ) + scale_x_continuous( name="Weight (kg)", breaks=seq(0,250,25),expand=expansion(mult=c(0,0)), ) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density1_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density( color="gray30", fill="gray30", alpha=0.25 ) + scale_x_continuous( name="Weight (kg)", breaks=seq(0,250,25),expand=expansion(mult=c(0,0)), * limits=c(0,250) ) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density1_non_seq_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Density Plot ### Adjusting "Smoothness" --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density(color="gray30",fill="gray30",alpha=0.25, ) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0)), limits=c(0,250)) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density2_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=weight)) + geom_density(color="gray30",fill="gray30",alpha=0.25, * adjust=0.25 ) + scale_x_continuous(name="Weight (kg)",breaks=seq(0,250,25), expand=expansion(mult=c(0,0)), limits=c(0,250)) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/density2_non_seq_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Density Plot by Groups --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=weight, )) + geom_density(alpha=0.25) + scale_x_continuous(name="Weight (kg)", breaks=seq(0,250,25),limits=c(0,250), expand=expansion(mult=c(0,0))) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/densitysplit1_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=weight, * color=sex, )) + geom_density(alpha=0.25) + scale_x_continuous(name="Weight (kg)", breaks=seq(0,250,25),limits=c(0,250), expand=expansion(mult=c(0,0))) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/densitysplit1_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=weight, color=sex, * fill=sex )) + geom_density(alpha=0.25) + scale_x_continuous(name="Weight (kg)", breaks=seq(0,250,25),limits=c(0,250), expand=expansion(mult=c(0,0))) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/densitysplit1_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=weight, color=sex, fill=sex )) + geom_density(alpha=0.25) + scale_x_continuous(name="Weight (kg)", breaks=seq(0,250,25),limits=c(0,250), expand=expansion(mult=c(0,0))) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + * scale_color_manual(values=c("red","blue")) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/densitysplit1_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=weight, color=sex, fill=sex )) + geom_density(alpha=0.25) + scale_x_continuous(name="Weight (kg)", breaks=seq(0,250,25),limits=c(0,250), expand=expansion(mult=c(0,0))) + scale_y_continuous(name="Relative Density of Bears", expand=expansion(mult=c(0,0.05))) + scale_color_manual(values=c("red","blue")) + * scale_fill_manual(values=c("red","blue")) + theme_bw() + theme(panel.grid.major.x=element_blank(), panel.grid.minor.x=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/densitysplit1_non_seq_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Boxplot --- .pull-left[ # Boxplot - Displays summary statistics that describe the distribution of the quantitative *X* variable. - "Box" is from 1st quartile (Q1) to 3rd quartile (Q3). - Line in box is median. - "Whiskers" extend to min/max or last point not considered an "outlier." - "Outliers" are more than 1.5*IQR from Q1 or Q3. - Usually for comparing groups. - Constructed with `geom_boxplot()`. ] .pull-right[ <img src="Lecture_BlackBears_files/figure-html/boxplot_demo-1.png" width="100%" /> ] --- class: inverse, center, middle # Boxplot with Typical Modifications --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot1_auto_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + * geom_boxplot(fill="gray30",alpha=0.25) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot1_auto_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_boxplot(fill="gray30",alpha=0.25) + * scale_x_discrete(name="Sex") p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot1_auto_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_boxplot(fill="gray30",alpha=0.25) + scale_x_discrete(name="Sex") + * scale_y_continuous(name="Weight (kg)", * limits=c(0,250),breaks=seq(0,250,25), * expand=expansion(mult=c(0,0))) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot1_auto_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_boxplot(fill="gray30",alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + * theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot1_auto_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Boxplot with Colors by Group --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=sex,y=weight, )) + geom_boxplot(alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot2_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=sex,y=weight, * color=sex,fill=sex )) + geom_boxplot(alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot2_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=sex,y=weight, color=sex,fill=sex )) + geom_boxplot(alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + * scale_color_manual(values=c("red","blue")) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot2_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=sex,y=weight, color=sex,fill=sex )) + geom_boxplot(alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + scale_color_manual(values=c("red","blue")) + * scale_fill_manual(values=c("red","blue")) + theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot2_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes( x=sex,y=weight, color=sex,fill=sex )) + geom_boxplot(alpha=0.25) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + scale_color_manual(values=c("red","blue")) + scale_fill_manual(values=c("red","blue")) + theme_bw() + * theme(legend.position="none") p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/boxplot2_non_seq_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Violin Plot --- .pull-left[ # Violin Plot - Displays density of the quantitative *X* variable. - Usually for comparing groups. - Constructed with `geom_violin()`. - Very similar to use of `geom_boxplot()`. - Suggest using `trim=FALSE`. ] .pull-right[ <img src="Lecture_BlackBears_files/figure-html/violin_demo-1.png" width="100%" /> ] --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin1_auto_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + * geom_violin(fill="gray30",alpha=0.25,trim=FALSE) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin1_auto_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_violin(fill="gray30",alpha=0.25,trim=FALSE) + * scale_x_discrete(name="Sex") p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin1_auto_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_violin(fill="gray30",alpha=0.25,trim=FALSE) + scale_x_discrete(name="Sex") + * scale_y_continuous(name="Weight (kg)", * limits=c(0,250),breaks=seq(0,250,25), * expand=expansion(mult=c(0,0))) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin1_auto_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_violin(fill="gray30",alpha=0.25,trim=FALSE) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + * theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin1_auto_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Violin Plot ### With Individual Points --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + * geom_violin(fill="gray30",alpha=0.25,trim=FALSE) + * scale_x_discrete(name="Sex") + * scale_y_continuous(name="Weight (kg)", * limits=c(0,250),breaks=seq(0,250,25), * expand=expansion(mult=c(0,0))) + * theme_bw() p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin2_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=flbb, mapping=aes(x=sex,y=weight)) + geom_violin(fill="gray30",alpha=0.25,trim=FALSE) + scale_x_discrete(name="Sex") + scale_y_continuous(name="Weight (kg)", limits=c(0,250),breaks=seq(0,250,25), expand=expansion(mult=c(0,0))) + theme_bw() + * geom_jitter(width=0.05,height=0,alpha=0.25) p ``` ]] .column[.content[ <img src="Lecture_BlackBears_files/figure-html/violin2_user_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Next Time <font size="7">We will examine graphs for distributions of categorical data.</font>