18.3 Functional programming
An example is to make a geom. For this we can have a look at the “Corporate Reputation” data from #TidyTuesday 2022 week22.
poll <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-05-31/poll.csv')
reputation <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-05-31/reputation.csv')
rep2<-reputation%>%
group_by(company,industry)%>%
summarize(score,rank)%>%
ungroup()%>%
mutate(year=2022)
full <- poll%>%
filter(!is.na(year))%>%
full_join(rep2,by=c("2022_rank"="rank","2022_rq"="score","company","industry","year")) %>%
count(year,company,industry,"rank"=`2022_rank`,"score"=`2022_rq`,sort=T) %>%
arrange(-year)
##################
# mapping = aes(x = fct_reorder(x,-y), y = y, fill = y, color = y, label = y)
rank_plot <- function(data,mapping) {
data %>%
ggplot(mapping)+ # aes(x=fct_reorder(x,-y),y=y)
geom_col(width =0.3, # aes(fill=rank)
show.legend = F)+
geom_text(hjust=0,fontface="bold", # aes(label=rank,color=rank),
show.legend = F)+
scale_y_discrete(expand = c(0, 0, .5, 0))+
coord_flip()+
ggthemes::scale_fill_continuous_tableau(palette = "Green-Gold")+
ggthemes::scale_color_continuous_tableau(palette = "Green-Gold")+
labs(title="",
x="",y="")+
theme(axis.text.x = element_blank(),
axis.text.y = element_text(face="bold"),
axis.ticks.x = element_blank(),
axis.ticks.y = element_line(size=2),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(size=2),
plot.background = element_rect(color="grey95",fill="grey95"),
panel.background = element_rect(color="grey92",fill="grey92"))
}
df<-full%>%
filter(year==2017,
industry=="Retail")
rank_plot(data = df,
mapping = aes(x=fct_reorder(company,-rank),y=rank,
fill = rank, color = rank, label = rank))