Add x and y axis to all facet_wrap

Tyler Rinker picture Tyler Rinker · Mar 1, 2014 · Viewed 34.4k times · Source

It is often desirable to minimize ink in a plot. I have a faceted plot (facet_wrap) and would like to remove as much ink as possible yet maintain readability. I have set it up as I'd like except the x and y axis is not present for the facets (subplots) unless on the far left or bottom. With so much ink removed I believe the eye needs these cues and am asking how to put the x and y axis in all plots within a facet_wrap. Below is my code thus far, the current output and the deired output (red lines are the desired add in):

library(ggplot); library(grid)

ggplot(mtcars, aes(mpg, hp)) + 
    geom_point() + 
    facet_wrap(~carb) +
    theme(panel.grid = element_blank(),
        panel.background = element_rect(fill = "white", colour = "black"), 
        panel.border = element_rect(fill = NA, colour = "white"), 
        axis.line = element_line(),
        strip.background = element_blank(),
        panel.margin = unit(2, "lines"))

Current Plot enter image description here

Desired Plot enter image description here

Answer

Thomas picture Thomas · Mar 1, 2014

This should simplify things considerably:

library('ggthemes')
ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') + 
    theme_tufte() + theme(axis.line=element_line()) + 
    scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))

enter image description here