I have to put things somewhere so I don’t forget them
minipost
ggplot2
dont-forget
Author
Joshua Kunst Fuentes
Published
July 23, 2023
Modified
March 28, 2024
The idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.
A lot of other functionalities like new geoms, new facets!
Code
library(ggforce)p1+geom_mark_hull(aes(filter =mpg>30, label ="Interesting"), description ="Lightweight vehicles have high performance.", color ="gray70", fill ="gray90", concavity =5,# control width text label.minwidth =unit(100, "mm"),# how much distance before show legend label.buffer =unit(2.5, "mm"), label.colour ="gray30", label.family ="ibm")+# use circle for pointsgeom_mark_circle(aes(filter =wt>3.75, label =NULL, description =name), color ="gray70", fill ="transparent", label.fontsize =10, label.family ="ibm")
And other geom_mark_* like hull, circle, ellpse, rect.
Code
p2+geom_mark_ellipse(aes(fill =species, label =species), alpha =0.1, color ="transparent", # a nice touch sometimes! (imho) label.colour ="gray30", label.family ="ibm", label.fontsize =8,# label.fontface = "plain",# this is just for blogpost expand =unit(-5, "mm"), radius =unit(5, "mm"))+geom_mark_circle(aes( filter =coalesce(bill_length_mm, 0)==max(bill_length_mm, na.rm =TRUE), label =NULL, description ="A rare penguin!"), color ="gray70", fill ="transparent", label.fontsize =8, label.family ="ibm")+theme(legend.position ="none")+labs(x =NULL, y =NULL)
bnd<-penguins|>summarise(min(bill_length_mm, na.rm =TRUE)-1,max(bill_length_mm, na.rm =TRUE)+1,min(bill_depth_mm, na.rm =TRUE)-1,max(bill_depth_mm, na.rm =TRUE)+1)|>as.list()|>unlist()|>as.vector()p2+geom_voronoi_tile(aes(fill =cluster, group =-1), data =dcenters, alpha =0.2, bound =bnd)+geom_voronoi_segment(aes(group =-1), data =dcenters, color ="gray90", bound =bnd)+xlim(bnd[1], bnd[2])+ylim(bnd[3], bnd[4])+scale_fill_viridis_d(direction =-1, option ="C")+# its better put point over all layersgeom_point(aes(color =species), size =2, shape =21, color ="gray90")+theme(legend.position ="right")
# I know the correct alternative is Mpgmiles_per_gallon<-label_comma(suffix =" mi/gal")wt_lbl<-label_comma(scale =1000, suffix =" lbs")p1+scale_y_continuous( labels =miles_per_gallon, name ="fuel consumption")+scale_x_continuous( labels =wt_lbl, name ="weigth")
I use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.
Code
# remotes::install_github("grantmcdermott/parttree")library(parttree)# 2 independent variablespenguinct2<-ctree(species~bill_length_mm+bill_depth_mm, data =penguins, control =ctree_control(maxdepth =3))ggplot(penguins, aes(x =bill_length_mm, y =bill_depth_mm))+geom_parttree( data =penguinct2, aes(fill =species), alpha =0.2, color ="gray60",)+geom_point(aes(col =species))+theme_minimal()
{ggparty} + {parttree}
Code
dpred_node<-penguins|>select(species, bill_length_mm, bill_depth_mm)|>mutate( id =predict(penguinct2, type ="node", newdata =penguins), species_pred =predict(penguinct2, newdata =penguins))|>group_by(id)|>summarise( species =unique(species_pred), bill_length_mm =mean(bill_length_mm), bill_depth_mm =mean(bill_depth_mm))dpred_node