library(tidyverse)
TidyTuesday 2022 week 5 - Dog breeds
Dog breeds datasets, load the data
<- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_traits.csv')
breed_traits <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/trait_description.csv')
trait_description <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_rank.csv') breed_rank_all
Make a rank dataset to see which breed is favourite in 2020
<-breed_rank_all%>%
rankcount(Breed,`2020 Rank`,Image)%>%
arrange(`2020 Rank`)%>%
slice(1:10)%>%
relocate(`2020 Rank`)%>%
select(-n)
rank
Select favourite breeds, and set the images
<-c("Bulldogs","Poodles","Beagles","Rottweilers","Dachshunds")
dogs
<-rank%>%filter(Breed%in%dogs)%>%select(Breed,Image)
images
<-breed_traits%>%
my_df select(-"Coat Type",-"Coat Length")%>%
filter(Breed%in%dogs)%>%
mutate(id=row_number())%>%
relocate(id)%>%
pivot_longer(cols=3:16,names_to="factor",values_to="value")%>%
inner_join(images,by="Breed")
library(extrafont)
library(showtext)
::showtext_auto()
showtext::showtext_opts(dpi=320)
showtextlibrary(sysfonts)
#font_families_google()
font_add_google(name="Barlow Condensed",family="dogs")
= "dogs" family
To make the plot, use:
- ggforce::geom_ellipse to make the paws
- ggimage::geom_image to add the images
<-
dog_prints_plot
ggplot(my_df,aes(id,value,group=Breed))+
# this is the largest part of the paw
::geom_ellipse(aes(x0=id,y0=value-0.2,a=0.2,b=0.25,
ggforceangle=50,color=Breed),fill="#393d36",size=0.5,alpha=0.7)+
# the following four ellipses are small extrems of the paw
::geom_ellipse(aes(x0=id+0.22,y0=value+0.2,a=0.1,b=0.12,
ggforceangle=50,color=Breed),fill="#393d36",size=0.5,alpha=0.7)+
::geom_ellipse(aes(x0=id-0.2,y0=value+0.15,a=0.1,b=0.12,
ggforceangle=50,color=Breed),fill="#393d36",size=0.5,alpha=0.7)+
::geom_ellipse(aes(x0=id+0.01,y0=value+0.25,a=0.1,b=0.1,
ggforceangle=50,color=Breed),fill="#393d36",size=0.5,alpha=0.7)+
::geom_ellipse(aes(x0=id-0.32,y0=value-0.15,a=0.1,b=0.1,
ggforceangle=50,color=Breed),fill="#393d36",size=0.5,alpha=0.7)+
# this is the grey part of the paw
::geom_ellipse(aes(x0=id,y0=value-0.3,a=0.08,b=0.08,
ggforceangle=50),color="grey65",size=0.02,fill="grey65",alpha=0.1) +
# The segment is to make the arrow up
geom_segment(x=0.43,xend=0.43,y=0,yend=6, size=3,color="grey65",
arrow = arrow(length = unit(c(0, 0, 0.4, 0.4), 'cm')))+
# description of the factors
geom_text(aes(label=factor),check_overlap = T, vjust=5.5, hjust=0.5, size=2.5, color="midnightblue",family=family)+
# The breed's names
geom_text(aes(x=id,y=6.5,label=Breed),color="black",size=5.5,family=family,face="bold")+
# Images of the breeds
::geom_image(aes(x=id,y=-1.4,image=Image),size=0.2)+
ggimage
# expand the canvas to make the paws standing out
scale_y_continuous(expand = c(0,1))+
ylim(-1.5,7)+
coord_cartesian()+
labs(title="Top 5 selected Breeds: Was Goofy a dog?",
caption="Datasource: American Kennel Club | #TidyTuesday 2022-w5 | Viz: Federica Gazzelloni")+
::theme_fivethirtyeight()+
ggthemestheme(text = element_text(family = family,size=14),
plot.title = element_text(size=18,hjust=0.1),
plot.caption = element_text(face = "bold",vjust=0.5),
panel.grid.major.x = element_line(size=38),
panel.grid.major.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_line(size=4),
plot.background = element_rect(color="white",fill="white"),
panel.background = element_rect(color="white",fill="white"),
legend.position = "none")
Add extra images and save it
library(cowplot)
<- ggdraw()+
final draw_plot(dog_prints_plot)+
draw_image("print.png",x=0.3,y=0.43,scale=0.16)+
draw_image("print.png",x=0.4,y=0.43,scale=0.16)
ggsave("dog_prints_plot.png",final)