Skip to content

Instantly share code, notes, and snippets.

@cecilesauder
Last active October 13, 2018 00:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cecilesauder/77da0255d44421273343631f7c7ae15d to your computer and use it in GitHub Desktop.
Save cecilesauder/77da0255d44421273343631f7c7ae15d to your computer and use it in GitHub Desktop.
ggplot for people who made more than 40 #dplyr travis builds
#devtools::install_github("r-lib/gh")
library(gh)
#devtools::install_github("romainfrancois/dplyr5000")
library(dplyr5000)
library(tidyverse)
library(grid) 
library(magick)
#> Linking to ImageMagick 6.9.7.4
#> Enabled features: fontconfig, freetype, fftw, lcms, pango, x11
#> Disabled features: cairo, ghostscript, rsvg, webp

## ##########
## INDEPENDENT CODE TO BE SOURCED:
## source : https://jcarroll.com.au/2016/06/03/images-as-x-axis-labels-updated/
## ##########
# user-level interface to the element grob
my_axis = function(img, angle = 90) {
  structure(
    list(img=img, angle=angle),
    class = c("element_custom", "element_blank", "element_text", "element") # inheritance test workaround
  )
}
# returns a gTree with two children: the text label, and a rasterGrob below
element_grob.element_custom <- function(element, x, ...)  {
  stopifnot(length(x) == length(element$img))
  tag <- names(element$img)
  # add vertical padding to leave space
  g1 <- textGrob(paste0(tag, "\n\n\n\n\n"), x=x, rot = element$angle, vjust=0.6)
  g2 <- mapply(rasterGrob, x=x, image=element$img[tag], 
               MoreArgs=list(vjust=0.7, interpolate=FALSE,
                             height=unit(3,"lines")),
               SIMPLIFY=FALSE)
  
  gTree(children=do.call(gList, c(g2, list(g1))), cl="custom_axis")
}
# gTrees don't know their size and ggplot would squash it, so give it room
grobHeight.custom_axis = heightDetails.custom_axis = function(x, ...)
  unit(6, "lines")
## ##########
## END
## ##########

#tibble with results to plot
tib <- dplyr5000 %>% 
  group_by(user) %>% 
  tally(sort = TRUE) %>%
  filter(!is.na(user)) %>%
  filter(n> 40)


get_github_avatar <- function(x){
  profil <- gh("/users/:username", username = x)
  image_read(profil[["avatar_url"]])
}

pix <- map(tib$user, get_github_avatar)

names(pix) <- tib$user

tib %>%
  ggplot(aes(x= reorder(user,-n), y = n, fill = 1)) +
  geom_bar(stat = "identity") +
  guides(fill=FALSE) + 
  labs(
    title = "#dplyr travis builds",
    subtitle = "people who made more than 40 builds",
    x="", y="builds"
  ) +
  theme(axis.text.x  = my_axis(pix, angle = 0), 
        axis.text.y  = element_text(size=14),
        axis.title.x = element_blank())

Created on 2018-10-12 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment