3

This question has been asked before but the answers aren't always clear or are complicated. I'm hoping that newer versions of ggplot2 have lead to easier solutions.

How can you eliminate just the vertical lines of a ggplot without eliminating the axis tick marks or labels? This would really be nice for bar graphs as it would eliminate some unnecessary distraction from the graphic.

Here is some sample code to aid the discussion:

library(ggplot2)
set.seed(10)
CO3 <- data.frame(id=1:nrow(CO2), CO2[, 2:3], 
           outcome=factor(sample(c('none', 'some', 'lots', 'tons'), 
           nrow(CO2), rep=T), levels=c('none', 'some', 'lots', 'tons')))
CO3
x <- ggplot(CO3, aes(x=outcome)) + geom_bar(aes(x=outcome))+ 
     facet_grid(Treatment~Type, margins='Treatment', scales='free') 
x +  theme_bw() + opts(axis.text.x=theme_text(angle= 45, vjust=1, hjust= 1))
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

1 Answers1

5

Try this, redifining guide_grid.

This solution is from the Cookbook for R

# Save the original definition of the guide_grid
guide_grid_orig <- ggplot2:::guide_grid

# Create the replacement function
guide_grid_no_vline <- function(theme, x.minor, x.major, y.minor, y.major) {  
  x.minor <- setdiff(x.minor, x.major)
  y.minor <- setdiff(y.minor, y.major)

  ggname("grill", grobTree(
    theme_render(theme, "panel.background"),
    if(length(y.minor) > 0) theme_render(
      theme, "panel.grid.minor", name = "y",
      x = rep(0:1, length(y.minor)), y = rep(y.minor, each=2), 
      id.lengths = rep(2, length(y.minor))
      ),
    if(length(y.major) > 0) theme_render(
      theme, "panel.grid.major", name = "y",
      x = rep(0:1, length(y.major)), y = rep(y.major, each=2), 
      id.lengths = rep(2, length(y.major))
      )
    ))
}
# Set the environment to be the same as original
environment(guide_grid_no_vline) <- environment(ggplot2:::guide_grid)

# Assign the function inside ggplot2
assignInNamespace("guide_grid", guide_grid_no_vline, ns="ggplot2")

# Draw the plot with the redefined guide_grid
ggplot(CO3, aes(x=outcome)) + 
  geom_bar(aes(x=outcome))+ 
  facet_grid(Treatment~Type, margins='Treatment', scales='free')  +
  theme_bw() + 
  opts(axis.text.x=theme_text(angle= 45, vjust=1, hjust= 1))

# Restore the original guide_grid function so that it will draw all gridlines again
assignInNamespace("guide_grid", guide_grid_orig, ns="ggplot2")
EDi
  • 13,160
  • 2
  • 48
  • 57
  • I've done that but then you get into trouble when you allow the scales to be free. I altered the code above to reflect that. My apologies for not being clearer. – Tyler Rinker Mar 19 '12 at 22:57
  • Found a solution on the cookbook for R – EDi Mar 19 '12 at 23:10
  • works like a charm. I forgot to check the R cookbook site. I love that site. – Tyler Rinker Mar 19 '12 at 23:55
  • I had to extract all functions in `guide_grid_no_vline` from their respective packages, but other than that, this worked fine. Seems like a lot of trouble to remove a few lines, though. – Roman Luštrik May 01 '12 at 21:28
  • 1
    Thank god this solution was made obsolete by a newer version of ggplot2. See http://stackoverflow.com/a/2680870/322912 – Roman Luštrik Nov 21 '12 at 00:06