Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created October 14, 2020 21:13
Show Gist options
  • Save geotheory/2ad328029666dfebc9acd529474bd54b to your computer and use it in GitHub Desktop.
Save geotheory/2ad328029666dfebc9acd529474bd54b to your computer and use it in GitHub Desktop.
require(ggplot2)
gg_donut = function(x, lab, textsize = 5){
ggplot() +
geom_rect(aes(ymax = c(100, x), ymin = 0, xmax = 4, xmin = 3.2, fill = c('A','B'))) +
geom_text(aes(y = 0, x = 0, label = paste0(round(x,1), '%')), size = textsize) +
coord_polar(theta="y") + xlim(c(0, 4)) +
scale_y_continuous(limits = c(0,100)) +
scale_fill_manual(values = c('grey80','dodgerblue2')) +
labs(title = lab) +
theme_void() +
theme(plot.title = element_text(hjust = 0.5)) +
guides(fill = 'none')
}
#' Example usage
#'
#' gg_donut(45, 'Retweets')
#'
#' if(require(patchwork)){
#' gg_donut(45, 'Retweets') + gg_donut(15, 'Links') + gg_donut(100*2/3, 'Hashtags')
#' }
#'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment