Skip to content

Instantly share code, notes, and snippets.

@bhaskarvk
Last active February 17, 2017 20:29
Show Gist options
  • Save bhaskarvk/229cca8dfd067cfbcb3074c85b950abb to your computer and use it in GitHub Desktop.
Save bhaskarvk/229cca8dfd067cfbcb3074c85b950abb to your computer and use it in GitHub Desktop.
Need to move Y axis label above Y axis text.
OrgSize n
Large 32
Very Large 36
Very Small 43
Small 60
Medium 67
suppressMessages(library(ggplot2))
suppressMessages(library(ggalt))
# devtools::install_github('hrbrmstr/hrbrthemes')
library(hrbrthemes)
scale_x_orgsize <- scale_x_discrete(labels=c(
'Very Small\n(<250 employees)',
'Small\n(251-2,000 employees)',
'Medium\n(2,001-10,000 employees)',
'Large\n(10,001-50,000 employees)',
'Very Large\n(>50,000 employees)'))
orgsize <- readr::read_csv('https://gist.github.com/bhaskarvk/229cca8dfd067cfbcb3074c85b950abb/raw/a6dc75e2c9680ee1148edba71b22f39d23cb2f9e/orgs_by_size.csv')
ggplot(
orgsize, aes(x = OrgSize, y = n)) +
geom_lollipop(point.colour = '#000000', point.size = 1) +
coord_flip() +
labs(list(
x = 'Organization Size', y = 'Number of Orgs.', title = NULL)) +
scale_x_orgsize +
theme_ipsum_rc(grid='X', axis_title_face = 'bold') +
theme(axis.title.y = element_text(angle = 0, vjust = 1))
@bhive01
Copy link

bhive01 commented Feb 17, 2017

Took a while to get everything installed. [Edit, beat to the punch. I didn't get there anyway.]

Firstly, save the plot to something:

p.out <- ggplot(
  orgsize, aes(x = OrgSize, y = n)) +
  geom_lollipop(point.colour = '#000000', point.size = 1) +
  coord_flip() +
  labs(list(
    x = 'Organization Size', y = 'Number of Orgs.', title = NULL)) +
  scale_x_orgsize +  
  theme_ipsum_rc(grid='X', axis_title_face = 'bold') +
  theme(axis.title.y = element_text(angle = 0, vjust = 1))

#save gtable to something too
gtable <- ggplot_gtable(ggplot_build(p.out))

gtable

gtable[[3]] # is axis labels
gtable[[13]] # is the axis title

Link to gtable explorer by @cpsievert
http://104.131.111.111:3838/ggtree/

gtable editing by @zevross
http://zevross.com/blog/2014/11/20/under-the-hood-of-ggplot2-graphics-in-r/

@thomasp85 suggests moving the axis title up one row.
https://twitter.com/thomasp85/status/832647591703633922

@bhaskarvk
Copy link
Author

Thanks @bhive01 and @thomasp85.

@bhive01
Copy link

bhive01 commented Feb 17, 2017

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