library(tidyverse)
<- tidytuesdayR::tt_load(2023, week = 15) tuesdata
To cite Federica’s work, please use: Gazzelloni F., 2023 Data Visualization US Egg Production down/upwards
<- tuesdata$`egg-production`
eggs <- tuesdata$`cage-free-percentages` cages
%>%
eggsinner_join(cages,by="observed_month")%>%
distinct() %>%
mutate(ymon=zoo::as.yearmon(observed_month),
.after=observed_month)
<- eggs %>%
df inner_join(cages, by = "observed_month") %>%
mutate(ymon = zoo::as.yearmon(observed_month),
.after = observed_month) %>%
select(-source.x, -source.y, -observed_month) %>%
group_by(ymon, prod_process) %>%
mutate(pct_hens = round(n_hens / sum(n_hens) * 100, 2),
pct_eggs = round(n_eggs / sum(n_eggs) * 100, 2)) %>%
ungroup() %>%
arrange(ymon)
%>%head df
%>%
df ggplot(aes(ymon,n_hens))+
geom_line(aes(group=prod_type))+
facet_wrap(~prod_process,scales = "free")
%>%
df filter(!prod_process=="all")%>%
ggplot()+
geom_line(aes(ymon,n_hens,group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
facet_wrap(~prod_process,scales = "free_y")+
scale_y_log10()
%>%
df filter(!prod_process=="all")%>%
ggplot(aes(x=ymon))+
geom_line(aes(y=n_hens/sum(n_hens)*100,
group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
geom_line(aes(y=n_eggs/sum(n_eggs)*100,
group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
facet_wrap(~prod_process,scales = "free_y")+
scale_y_continuous(labels = scales::label_percent(decimal.mark = ",",suffix = " %"))
%>%
df filter(!prod_process=="all")%>%
ggplot()+
geom_line(aes(ymon,percent_eggs,group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
geom_line(aes(ymon,percent_hens,group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))
Fill missing values
you can fill in the data with two approaches:
- Up - While filling the missing values, you have to specify the direction of filling of values. If you choose Up, then the filling process will be bottom-up.
- Down - In this method, you have to set the direction of filling to down.
source: https://www.digitalocean.com/community/tutorials/fill-missing-values-in-tidyr-r
Use .direction = "downup"
to fill missing values in both directions
%>%
df filter(!prod_process=="all")%>%
::fill(percent_eggs, .direction = 'downup') %>% # View()
tidyrmutate(percent_eggs=ifelse(is.na(percent_eggs),25,percent_eggs))%>% #View
ggplot()+
geom_line(aes(ymon,percent_eggs,group=prod_type),
color="red",
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
geom_line(aes(ymon,percent_hens,group=prod_type),
lineend = "butt",
arrow = arrow(length = unit(0.05,"inches"),
type="closed"))+
::theme_fivethirtyeight() ggthemes
Aug 2016 - Feb 2021
%>%
df filter(!prod_process=="all")%>% # count(ymon)
::fill(percent_eggs, .direction = 'downup') %>% # View()
tidyrmutate(percent_eggs=ifelse(is.na(percent_eggs),25,percent_eggs))%>% #View
pivot_longer(cols = c("percent_eggs","percent_hens"))%>%
mutate(name=ifelse(name=="percent_eggs","eggs (%)","hens (%)")) %>%
ggplot()+
geom_line(aes(ymon,value,group=name,color=name),
linewidth= 1,
arrow = arrow(length = unit(0.1,"inches"),
angle = 30,
type="closed"))+
labs(title="US Egg Production Aug 2016 - Feb 2021",
subtitle="Hens & Eggs down/upwards",
color="",
caption="DataSource: #TidyTuesday Week 15 US Egg Production\n #30DayChartChallenge Day 21 down/upwards | DataViz: Federica Gazzelloni")+
::scale_color_fivethirtyeight()+
ggthemes::theme_fivethirtyeight() ggthemes
ggsave("w15_UEPD.png",width = 7,height = 5,bg="white")