rggplot2labelaxis

Remove axis.titles in gg.gap () plots


Someone knows how to delete axis.title.y from a gg.gap() plot? I'm trying using theme () but it doesn't work...

For example using a gg.gap() package example: I did a ggplot without any y and x axis titles ("p" plot), but when I used "p" in gg.gap to create a gap in y axis, the axis titles appears again...

data(mtcars)
library(ggplot2)

p<-ggplot(data = mtcars, aes(x = gear, fill = gear)) +geom_bar() +ggtitle("Number of Cars by Gear") +xlab("Gears") + 
          theme( axis.title.y = element_blank(),
             axis.ticks.y = element_blank(),
             axis.text.y = element_blank(),
             axis.ticks.x = element_blank(),
             axis.text.x = element_blank())

pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))

And neither theme() works...

pgg + theme( axis.title.y = element_blank(),
             axis.ticks.y = element_blank(),
             axis.text.y = element_blank(),
             axis.ticks.x = element_blank(),
             axis.text.x = element_blank())

Thank you in advance!


Solution

  • This one is interesting. As @teunbrand mentioned, you should be able to fix this via scale_y_continuous(name=""), or my preference: name=NULL. It doesn't work though in any permutation!

    What seems to work is setting the axis title to NULL or "" using lab(y=...) or ylab(). I thought this worked wherever you added it, but seems that you need to add to the plot before applying the gg.gap() function:

    # this doesn't work
    p <- p + scale_y_continuous(NULL)
    pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
    pgg
    
    # this doesn't work either
    pgg + scale_y_continuous(NULL)
    
    # this apparently won't work
    pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
    pgg + ylab(NULL)
    
    
    # this works
    p <- p + ylab(NULL)
    pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
    pgg
    

    enter image description here