class: center, middle, inverse, title-slide # Labels and Annotations ### Derek Ogle, May 2020 --- class: inverse, center, middle # Today's Goal <font size="7">Describe how to modify labels (geoms and scales) and add annotations.</font> --- class: inverse, center, middle # Labels --- # Titles, Subtitles, and Captions - Added with `labs()`. -- - `title=` adds a main title at the top (stats above y-axis). - `subtitle=` adds a subtitle below the main title. - `caption=` adds a "caption" to the bottom-right. - `tag=` adds tag label to the very top-left. -- .center[ <img src="Lecture_Annotations_files/figure-html/labs-1.png" width="40%" /> ] --- class: inverse, center, middle # Example --- # Background - Isle Royale Moose and Wolves. - Note an outbreak of parvovirus occurred in 1980. -- ```r ## #!# Set to your own working directory and have just your filename below. irmw <- read.csv("https://raw.githubusercontent.com/droglenc/NCData/master/WolvesMoose_IsleRoyale_June2019.csv", na.strings=c("NA","N/A")) %>% select(year,wolves,moose) %>% mutate(parvo=ifelse(year>1980,"Post-parvo","Pre-parvo"), parvo=factor(parvo,levels=c("Pre-parvo","Post-parvo"))) head(irmw) ``` ``` year wolves moose parvo 1 1959 20 538 Pre-parvo 2 1960 22 564 Pre-parvo 3 1961 22 572 Pre-parvo 4 1962 23 579 Pre-parvo 5 1963 20 596 Pre-parvo 6 1964 26 620 Pre-parvo ``` --- class: split-50 count: false .column[.content[ ```r *ggplot(data=irmw,mapping=aes(x=year,y=wolves)) + * geom_line(color="gray70") + * geom_point(aes(fill=parvo),size=1.25,pch=21) + * scale_fill_manual(values=c("Pre-parvo"="white","Post-parvo"="black")) + * scale_y_continuous(limits=c(0,NA),expand=expansion(mult=c(0,0.04))) + * scale_x_continuous(expand=expansion(mult=0.04)) + * theme_bw() + * theme(panel.grid.minor=element_blank()) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw1_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r ggplot(data=irmw,mapping=aes(x=year,y=wolves)) + geom_line(color="gray70") + geom_point(aes(fill=parvo),size=1.25,pch=21) + scale_fill_manual(values=c("Pre-parvo"="white","Post-parvo"="black")) + scale_y_continuous(limits=c(0,NA),expand=expansion(mult=c(0,0.04))) + scale_x_continuous(expand=expansion(mult=0.04)) + theme_bw() + theme(panel.grid.minor=element_blank()) + * labs(title="Number of Wolves on Isle Royale, 1959-2019", * subtitle="Estimated from aerial counts", * caption="Source: https://isleroyalewolf.org") ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw1_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r ggplot(data=irmw,mapping=aes(x=year,y=wolves)) + geom_line(color="gray70") + geom_point(aes(fill=parvo),size=1.25,pch=21) + scale_fill_manual(values=c("Pre-parvo"="white","Post-parvo"="black")) + scale_y_continuous(limits=c(0,NA),expand=expansion(mult=c(0,0.04))) + scale_x_continuous(expand=expansion(mult=0.04)) + theme_bw() + theme(panel.grid.minor=element_blank()) + labs(title="Number of Wolves on Isle Royale, 1959-2019", subtitle="Estimated from aerial counts", caption="Source: https://isleroyalewolf.org") + * labs(x=element_blank(), * y=element_blank(), * fill="Relative to Parvo") ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw1_user_3_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Annotations --- # Annotation Layers - Add `geom_` ... but aesthetics not mapped from variables in a data.frame. -- - Aesthetics passed in as single values or small vectors. - Useful for adding small annotations (such as text labels or segments). -- - Use `annotate()`. -- - `geom=` (first argument) is the `geom_` to use. -- - Remaining arguments are position aesthetics (e.g., `x=`, `y=`, `xmin=`, `xmax=`, `ymin=`, `ymax=`, `xend=`, `yend=`) ... -- - ... and other aesthetics (e.g., `color=`, `fill=`, `size=`). --- class: inverse, center, middle # Example --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw,mapping=aes(x=year,y=wolves)) + geom_line(color="gray70") + geom_point(aes(fill=parvo),size=1.25,pch=21) + scale_fill_manual(values=c("Pre-parvo"="white","Post-parvo"="black")) + scale_y_continuous(limits=c(0,NA),expand=expansion(mult=c(0,0.04))) + scale_x_continuous(expand=expansion(mult=0.04)) + theme_bw() + theme(panel.grid.minor=element_blank(),legend.position="none") + labs(title="Number of Wolves on Isle Royale, 1959-2019", subtitle="Estimated from aerial counts", caption="Source: https://isleroyalewolf.org", x=element_blank(),y=element_blank()) p ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2_1_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + * annotate(geom="label",x=1990,y=48,hjust="left", * label="Outbreak of parvovirus in 1980") p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + annotate(geom="label",x=1990,y=48,hjust="left", label="Outbreak of parvovirus in 1980") + * annotate( * geom="segment",x=1990,y=48,xend=1980.5,yend=50, ) p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + annotate(geom="label",x=1990,y=48,hjust="left", label="Outbreak of parvovirus in 1980") + annotate( geom="segment",x=1990,y=48,xend=1980.5,yend=50, * size=0.25, ) p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + annotate(geom="label",x=1990,y=48,hjust="left", label="Outbreak of parvovirus in 1980") + annotate( geom="segment",x=1990,y=48,xend=1980.5,yend=50, size=0.25, * arrow=arrow( * length=unit(2,"mm"), ) ) p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_5_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + annotate(geom="label",x=1990,y=48,hjust="left", label="Outbreak of parvovirus in 1980") + annotate( geom="segment",x=1990,y=48,xend=1980.5,yend=50, size=0.25, arrow=arrow( length=unit(2,"mm"), * angle=15, ) ) p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_6_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 <- p + annotate(geom="label",x=1990,y=48,hjust="left", label="Outbreak of parvovirus in 1980") + annotate( geom="segment",x=1990,y=48,xend=1980.5,yend=50, size=0.25, arrow=arrow( length=unit(2,"mm"), angle=15, * type="closed" ) ) p1 ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2a_non_seq_7_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r *p1 + * annotate(geom="label",x=2014,y=37,hjust="right", * label="Wolves introduced from mainland") ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2b_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 + annotate(geom="label",x=2014,y=37,hjust="right", label="Wolves introduced from mainland") + * annotate(geom="curve",x=2014,y=37,xend=2019,yend=15.5,size=0.25, * arrow=arrow(length=unit(2,"mm"),angle=15,type="closed"), * curvature=-0.25) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2b_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 + annotate(geom="label",x=2014,y=37,hjust="right", label="Wolves introduced from mainland") + annotate(geom="curve",x=2014,y=37,xend=2019,yend=15.5,size=0.25, arrow=arrow(length=unit(2,"mm"),angle=15,type="closed"), curvature=-0.25) + * annotate(geom="segment",x=2008,xend=2013.5,y=18.5,yend=1, * color="steelblue") ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2b_user_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p1 + annotate(geom="label",x=2014,y=37,hjust="right", label="Wolves introduced from mainland") + annotate(geom="curve",x=2014,y=37,xend=2019,yend=15.5,size=0.25, arrow=arrow(length=unit(2,"mm"),angle=15,type="closed"), curvature=-0.25) + annotate(geom="segment",x=2008,xend=2013.5,y=18.5,yend=1, color="steelblue") + * annotate(geom="text",x=2003,y=8,color="steelblue", * label="Concerned about\ninbreeding suppression") ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irw2b_user_4_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Labeling Individual Points --- # Labeling Individual Points - Provide specific details about the data. -- - e.g., the actual mean. - e.g., letters that identify significant differences. - e.g., the sample size. -- - Highlight individual points. -- - Can use `geom_text()`, `geom_label()`, or `annotate()` --- class: inverse, center, middle # Example ... showing means --- class: split-50 count: false .column[.content[ ```r *sum1 *ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + * geom_bar(stat="identity",fill="gray30",alpha=0.75) + * scale_x_discrete(name="Great Lake") + * scale_y_continuous(name="Mean Maximum Ice Coverage (%)", * limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + * theme_bw() + * theme(panel.grid.major.x=element_blank()) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r sum1 ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + geom_bar(stat="identity",fill="gray30",alpha=0.75) + scale_x_discrete(name="Great Lake") + scale_y_continuous(name="Mean Maximum Ice Coverage (%)", limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank()) + * geom_text(aes(label=round(mn,1)),vjust="bottom",nudge_y=1) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar_user_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Example ... showing significance letters --- class: split-50 count: false .column[.content[ ```r *sum1 *ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + * geom_bar(stat="identity",fill="gray30",alpha=0.75) + * geom_errorbar(mapping=aes(ymin=lci,ymax=uci),width=0.1) + * scale_x_discrete(name="Great Lake") + * scale_y_continuous(name="Mean Maximum Ice Coverage (%)", * limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + * theme_bw() + * theme(panel.grid.major.x=element_blank()) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar2_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r sum1 ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + geom_bar(stat="identity",fill="gray30",alpha=0.75) + geom_errorbar(mapping=aes(ymin=lci,ymax=uci),width=0.1) + scale_x_discrete(name="Great Lake") + scale_y_continuous(name="Mean Maximum Ice Coverage (%)", limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank()) + * geom_text(mapping=aes(y=uci,label=c("b","a","b","c","a")), * vjust="bottom",nudge_y=1.5) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar2_user_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Example ... showing sample size --- class: split-50 count: false .column[.content[ ```r *sum1 *ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + * geom_bar(stat="identity",fill="gray30",alpha=0.75) + * geom_errorbar(mapping=aes(ymin=lci,ymax=uci),width=0.1) + * scale_x_discrete(name="Great Lake") + * scale_y_continuous(name="Mean Maximum Ice Coverage (%)", * limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + * theme_bw() + * theme(panel.grid.major.x=element_blank()) + * geom_text(mapping=aes(y=uci,label=c("b","a","b","c","a")), * vjust="bottom",nudge_y=1.5) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar3_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r sum1 ggplot(data=sum1,mapping=aes(x=lake,y=mn)) + geom_bar(stat="identity",fill="gray30",alpha=0.75) + geom_errorbar(mapping=aes(ymin=lci,ymax=uci),width=0.1) + scale_x_discrete(name="Great Lake") + scale_y_continuous(name="Mean Maximum Ice Coverage (%)", limits=c(0,NA),expand=expansion(mult=c(0,0.05))) + theme_bw() + theme(panel.grid.major.x=element_blank()) + geom_text(mapping=aes(y=uci,label=c("b","a","b","c","a")), vjust="bottom",nudge_y=1.5) + * geom_text(mapping=aes(y=0,label=paste("n =",n)), * vjust="bottom",nudge_y=1.5) ``` ]] .column[.content[ ``` # A tibble: 5 x 6 lake n mn s lci uci <fct> <int> <dbl> <dbl> <dbl> <dbl> 1 Superior 47 62.3 30.3 53.4 71.2 2 Michigan 47 40.5 21.7 34.1 46.8 3 Huron 47 65.3 23.3 58.5 72.1 4 Erie 47 82.6 26.0 75.0 90.3 5 Ontario 47 30.2 19.7 24.4 36.0 ``` <img src="Lecture_Annotations_files/figure-html/gli_mnsbar3_user_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Example ... highlighting individual points --- # Background - Examine dynamics of Moose and Wolf abundance from 1970 to 1990. - Make variable with label for only every fifth year. - Make variables with "next year's" wolf and moose abundances. -- ```r irmw2 <- irmw %>% filter(year>=1970,year<=1990) %>% mutate(year5=ifelse(year %in% seq(1970,1990,5),year,""),moose.next=c(moose[-1],NA),wolves.next=c(wolves[-1],NA)) FSA::headtail(irmw2, n=6) ``` ``` year wolves moose parvo year5 moose.next wolves.next 1 1970 18 1045 Pre-parvo 1970 1183 20 2 1971 20 1183 Pre-parvo 1243 23 3 1972 23 1243 Pre-parvo 1215 24 4 1973 24 1215 Pre-parvo 1203 31 5 1974 31 1203 Pre-parvo 1139 41 6 1975 41 1139 Pre-parvo 1975 1070 44 16 1985 22 976 Post-parvo 1985 1014 20 17 1986 20 1014 Post-parvo 1046 16 18 1987 16 1046 Post-parvo 1116 12 19 1988 12 1116 Post-parvo 1260 12 20 1989 12 1260 Post-parvo 1315 15 21 1990 15 1315 Post-parvo 1990 NA NA ``` --- class: split-50 count: false .column[.content[ ```r *head(irmw2) *ggplot(data=irmw2,mapping=aes(x=moose,y=wolves)) ``` ]] .column[.content[ ``` year wolves moose parvo year5 moose.next wolves.next 1 1970 18 1045 Pre-parvo 1970 1183 20 2 1971 20 1183 Pre-parvo 1243 23 3 1972 23 1243 Pre-parvo 1215 24 4 1973 24 1215 Pre-parvo 1203 31 5 1974 31 1203 Pre-parvo 1139 41 6 1975 41 1139 Pre-parvo 1975 1070 44 ``` <img src="Lecture_Annotations_files/figure-html/irmw1_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r head(irmw2) ggplot(data=irmw2,mapping=aes(x=moose,y=wolves)) + * geom_segment(mapping=aes(xend=moose.next,yend=wolves.next,color=year), * size=0.4, * arrow=arrow(length=unit(2,"mm"),angle=15,type="closed")) ``` ]] .column[.content[ ``` year wolves moose parvo year5 moose.next wolves.next 1 1970 18 1045 Pre-parvo 1970 1183 20 2 1971 20 1183 Pre-parvo 1243 23 3 1972 23 1243 Pre-parvo 1215 24 4 1973 24 1215 Pre-parvo 1203 31 5 1974 31 1203 Pre-parvo 1139 41 6 1975 41 1139 Pre-parvo 1975 1070 44 ``` <img src="Lecture_Annotations_files/figure-html/irmw1_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=irmw2,mapping=aes(x=moose,y=wolves)) + * geom_segment(mapping=aes(xend=moose.next,yend=wolves.next,color=year), * size=0.4, * arrow=arrow(length=unit(2,"mm"),angle=15,type="closed")) p ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw1a_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw2,mapping=aes(x=moose,y=wolves)) + geom_segment(mapping=aes(xend=moose.next,yend=wolves.next,color=year), size=0.4, arrow=arrow(length=unit(2,"mm"),angle=15,type="closed")) + * geom_point(size=1.2) + * labs(title="Isle Royale Wolf and Moose Abundance, 1970-1990", * subtitle="Parvovirus outbreak in 1980", * x="Moose Abundance",y="Wolf Abundance", * caption="Source: https://isleroyalewolf.org") + * theme_bw() + theme(panel.grid=element_blank(),legend.position="none", * aspect.ratio=1) p ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw1a_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw2,mapping=aes(x=moose,y=wolves)) + geom_segment(mapping=aes(xend=moose.next,yend=wolves.next,color=year), size=0.4, arrow=arrow(length=unit(2,"mm"),angle=15,type="closed")) + geom_point(size=1.2) + labs(title="Isle Royale Wolf and Moose Abundance, 1970-1990", subtitle="Parvovirus outbreak in 1980", x="Moose Abundance",y="Wolf Abundance", caption="Source: https://isleroyalewolf.org") + theme_bw() + theme(panel.grid=element_blank(),legend.position="none", aspect.ratio=1) + * geom_point(data=filter(irmw,year==1980),color="red",size=1.2) p ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw1a_user_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r *p ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw2_auto_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + * geom_text(mapping=aes(label=year)) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw2_auto_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + geom_text( mapping=aes(label=year5), ) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw3_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + geom_text( mapping=aes(label=year5), * vjust="bottom", ) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw3_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + geom_text( mapping=aes(label=year5), vjust="bottom", * nudge_y=1 ) ``` ]] .column[.content[ <img src="Lecture_Annotations_files/figure-html/irmw3_non_seq_3_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Next Time <font size="7">We will show how to efficiently create separate panels for plots with subgroups.</font>