Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DHBins: hexmaps for NZ

DHBins: hexmaps for NZ

The DHBins package lets you show statistics for New Zealand Regional Councils and District Health Boards on a hexagonal map, making comparisons easy by size or colour. For categorical data, you can split each hex into six triangles. This was my first ever set of ggplot geoms!

Thomas Lumley

February 17, 2020
Tweet

Other Decks in Programming

Transcript

  1. Regional councils (and triangles) data(region_ethnicity) tri_eth<-tri_alloc(as.matrix(region_ethnicity[,-1]), classes=c("Asian","Euro","Maori","MELAA","Pacific"), names=region_ethnicity$Area) tri_data<-data.frame(Region=rep(rownames(tri_eth),6), ethnicity=as.vector(tri_eth),

    tri_id=rep(1:6,each=nrow(tri_eth))) ggplot(tri_data)+ geom_regiontri(aes(map_id=Region,class_id=tri_id, fill=ethnicity))+ geom_label_region(colour="Black", short=TRUE,cex=4) 10 / 18
  2. Real ggplot geoms (they will layer and facet) ggplot(immune)+ geom_dhb(aes(map_id=dhb_fixname(DHB)),fill="white",

    colour="grey",coord=FALSE)+ geom_dhb(aes(fill=vax_pct,map_id=dhb_fixname(DHB), radius=sqrt(Ntotal)),alpha=0.5)+ scale_fill_viridis_d(drop=FALSE) data(immune_long) ggplot(immune_long) + geom_dhb(aes(map_id=dhb_fixname(DHB),fill=pct_vax), alpha=0.5,colour="lightgrey") + scale_fill_viridis_d(drop=FALSE)+ geom_label_dhb(short=TRUE,colour="black",cex=3)+ facet_wrap(~ethnicity) 13 / 18
  3. How to write a geom geom_dhb, geom_region based on geom_map

    also needs coord_fixed, expand_limits, axis themes returns a list of ggplot objects needs new GeomDHBMap based on GeomMap so hex size can depend on data, can't just call geom_map geom_label_dhb, geom_label_region just calls geom_text 16 / 18
  4. How to triangle Sainte-Laguë algorithm, as in MMP elections number

    of triangles is round(6*n/N) where n is count in that class and N is chosen to get exactly 6 hexes allocated 17 / 18