Search code examples
rggplot2axis-labels

Cannot remove mysterious zero from beneath plots


I've got five nice plots, but I've no idea why a 0 is printing beneath each. I've (at least tried) to remove x axis titles. I have no code--well, none I'm cognizant of--asking for the zeros beneath each x axis. I do wish to preserve the "SP22" etc. Here's the code for two of my plots:

output1 <- ggplot(data=SP22_SP23, aes(x=semesters, y=UG_classif, group=1)) +
  geom_line( aes(y=UG_classif), color="mediumaquamarine")+
  geom_point() + 
  scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), (expand = c(0,.1))) +
  scale_y_continuous(n.breaks = 10, limits = c(500, 3500)) +
    labs(title = "Undergraduate Enrollment:",
       subtitle= "Sp'22 through Sp'23\n", x = NULL, y = "Student Count") +
  theme_minimal() +
  removeGridX()

output2 <- ggplot(data=SP22_SP23, aes(x=semesters, y=NDseeking, group=1)) +
  geom_line( aes(y=NDseeking), color="tan3")+
  geom_point() + 
  scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), (expand = c(0,.1))) +
  scale_y_continuous(n.breaks = 10, limits = c(0, 400)) +
    labs(title = "Non-degree Seeking Enrollment:",
       subtitle= "Sp'22 through Sp'23\n", x=NULL, y = "Student Count") +
  theme(axis.title.x=element_blank()) +
  theme_minimal() +
  removeGridX()

And here's a screenshot of what I'm getting:

plots with pesky zeros beneath

As you can see, I've tried both putting x = NULL with the labs() and theme(axis.title.x=element_blank()).

Sincere thanks for any help!


Solution

  • The 0 being printed is due to the (expand = c(0,.1)) call in the scale_x_discrete call; the parenthesis is a short but to printing. The zero in the axis is from the first value in the expand call.

    To see:

    library(ggplot2)
    ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete((expand = c(2,3)))
    

    Avoid this by not surrounding the expand argument by parenthesis.

    e.g.

    ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete(expand = c(2,3))