Duplication of a discrete axis in ggplot2

The development version of ggplot2 (2.1.0.9001) provides good abbreviation for creating a secondary axis, which is a duplication of the primary axis if the original axis is continuous:

devtools::install_github("hadley/ggplot2")
library(ggplot2)

ggplot(mpg, aes(displ, cyl)) + 
  geom_point() + 
  scale_y_continuous(
    sec.axis = dup_axis()
  )

How can I duplicate a discrete axis?

ggplot(mpg, aes(displ, factor(cyl))) + 
  geom_point() +
  ...?
+2
source share
1 answer

The cowplot library has this object:

library(cowplot)
gpv <- ggplot(mpg, aes(displ, factor(cyl))) + 
   geom_point()
ggdraw( switch_axis_position( gpv, axis="y", keep="y"))

Do not forget that when sending to a file you need printmesh graphics:

png()
  print(ggdraw(switch_axis_position(gpv, axis="y", keep="y")) )
dev.off()
#quartz 
#     2 

enter image description here

+1
source

All Articles