In this milestone, you’ll recreate a faceted plot of the data in precip
. Run the setup chunk above before continuing with this milestone to load the precip
data and apply transformations from a previous milestone.
Run the code below to see a plot.
knitr::include_graphics("images/solution_09.png")
Your task this week is to recreate this plot of relative humidity across several research sites. Hint: To format the y-axis tick mark labels as percentages, take a look at the documentation for scale_y_continuous()
.
Write your code in the chunk below:
precip %>%
ggplot(data=.,aes(x=date,y=rh2m))+
geom_point(col="blue")+
geom_hline(yintercept = 0.5,linetype="dashed")+
facet_wrap(~site_id)+
scale_y_continuous(labels=scales::percent)+
scale_x_date(limits = c(ymd("2020-01-01"),ymd("2020-12-31")))+
labs(y=NULL,x=NULL,title="Relative humidity at 2 meters by site",
subtitle="Reference line at 50% humidity",
caption="Source: NASA POWER")
For your extension, you might find ways to further polish your recreation plot. For example, can you prevent the x-axis labels from overlapping? Can you add an annotation to just one facet of the plot?
Write your extension code in the following chunk:
rh_tbl<-precip %>%
#extra!
mutate(yr=year(date),
date_fake=ymd(glue::glue("2020-{month(date)}-{day(date)}")))
#Text
rh_txt<-rh_tbl %>%
dplyr::filter(rh2m>0.8)
g<-ggplot(data=rh_tbl,aes(x=date,y=rh2m))+
geom_point(col="blue",alpha=0.6)+
geom_hline(yintercept = 0.5,linetype="dashed")+
facet_wrap(~site_id)+
scale_y_continuous(labels=scales::percent)+
scale_x_date(limits = c(ymd("2020-01-01"),ymd("2020-12-31")),
date_labels="%b\n%Y")+
labs(y=NULL,x=NULL,title="Relative humidity at 2 meters by site",
subtitle="Reference line at 50% humidity",
caption="Source: NASA POWER")+
theme_light()
g+
geom_label_repel(data=rh_txt,
aes(x=date,y=rh2m,
label=round(rh2m,2) %>%scales::percent()),
size=2.5)
g_ply<-ggplot(data=rh_tbl,aes(x=date_fake,y=rh2m,frame=yr))+
geom_point(col="blue",alpha=0.6)+
geom_hline(yintercept = 0.5,linetype="dashed")+
facet_wrap(~site_id)+
scale_y_continuous(labels=scales::percent)+
scale_x_date(limits = c(ymd("2020-01-01"),ymd("2020-12-31")),
date_labels="%b")+
labs(y=NULL,x=NULL,title="Relative humidity at 2 meters by site",
subtitle="Reference line at 50% humidity",
caption="Source: NASA POWER")+
theme_light()
ggplotly(g_ply,tooltip = c("rh2m")) %>%
animation_opts(frame = 1000, transition = 200, redraw = FALSE)