Bar chart without vertical grid

686 views
Skip to first unread message

thomas

unread,
Nov 26, 2010, 11:36:13 AM11/26/10
to ggplot2
Hi,

I have a bar char with 8 categories/bars on the x-axis. The screenshot
shows a quick example I drew up. ( http://yfrog.com/2gcharte0j )
What I would like to do is remove the vertical grid lines (circled
red) but keep both the ticks and the labels on the x axis as well as
keeping the grid lines, ticks and labels on the y-axis.

i found a somewhat solution on stackoverflow but it doesn't work
(repositioning the text seems difficult) and I really hope that there
is a quicker way of doing it. My labels on the x-axis actually have 3
characters not 1 -- if that makes a difference.

Thanks for your help,
Thomas

James Howison

unread,
Nov 26, 2010, 12:10:50 PM11/26/10
to ggplot2
Pic was good, but a minimal reproducible example would help. A pointer to your attempted solution on Stack Overflow would also help.

I'm assuming that your issue is that + opts(panel.grid.minor = theme_blank()) also gets rid of the horizontal minor lines? Could you convert those to majors and be happy (ie. breaks=seq(0,200,by=25))? If you object to labeling each then you could try supplying labels=c("0","","50","","100","","150")?

Actually this reminds me, sometimes I see a minor_breaks parameter, but I've never been able to get that to work.

https://github.com/hadley/ggplot2/wiki/Graph-Panel-Attributes

--J

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

thomas

unread,
Nov 27, 2010, 12:51:28 PM11/27/10
to ggplot2
Hi James,

stack overflow solution:
http://stackoverflow.com/questions/2678141/how-can-i-suppress-the-vertical-gridlines-in-a-ggplot2-plot
though putting the labels back like suggested in the comments doesn't
seem to work for me.

I am not sure I follow your approach. Yes, only major horizontal grid
lines would be ok but switching to minor and/or major breaks wouldn't
change the x-axis and therefore the vertical grid lines since it is
discrete (at least it didn't for me). How would that get rid of the
vertical grid lines?

example:
theme_update(panel.background = theme_blank(),
panel.grid.major = theme_line(colour = "#EAEAEA", size =
0.5),
legend.key = theme_blank(),
panel.grid.minor = theme_blank())
data <- data.frame(Name = c("A","B","C","D","E","F","G","H"), y =
c(100,200,300,400,200,300,400,350))
p <- qplot(data$Name,data$y,geom="bar") + opts(legend.position =
"none") + opts(plot.margin = unit(c(0,0,-0.2,0),"cm"))

Thank you very much.

Best,
Thomas

On Nov 26, 6:10 pm, James Howison <ja...@freelancepropaganda.com>
wrote:

James Howison

unread,
Nov 27, 2010, 5:26:32 PM11/27/10
to ggplot2
Sorry, I did misunderstand. I thought you wanted to remove only the minor vertical gridlines, but you want to remove all of them (kind of a keynote style bar chart). Ideal here would be a panel.grid.major.y and panel.grid.minor.y param to opts but AFAIK those don't exist.

I messed around trying to do it with geom_hline etc, but this works better:

data <- data.frame(Name = c("A","B","C","D","E","F","G","H"), y = c(100,200,300,400,200,300,400,350))

ggplot(data,aes(x=Name,y=y)) + geom_bar()

grid.remove("panel.grid.minor.x.polyline",grep=T)
grid.remove("panel.grid.major.x.polyline",grep=T)
grid.remove("panel.grid.minor.y.polyline",grep=T)

Note that ggsave seems to discard these edits, so I had to wrap this in png() and dev.off().

Anyone know why ggsave() ignores grid level edits?

I used the instructions here:

https://github.com/hadley/ggplot2/wiki/Editing-raw-grid-objects-from-a-ggplot

Thx,
James

thomas

unread,
Nov 28, 2010, 8:26:30 AM11/28/10
to ggplot2
I just tried your wiki entry and updated the last lines.
"panel.grid.m...." doesn't work, i have to give the full path.

And while it seems to work when I check the pdf/print output, when I
include the PDF via \includegraphics in latex I actually see both
horizontal and vertical lines?!?
Anyone ever had the same problem and knows why that happens or how to
solve that?

Best,
Thomas

On Nov 27, 11:26 pm, James Howison <ja...@freelancepropaganda.com>
wrote:
> Sorry, I did misunderstand.  I thought you wanted to remove only the minor vertical gridlines, but you want to remove all of them (kind of a keynote style bar chart).  Ideal here would be a panel.grid.major.y and panel.grid.minor.y param to opts but AFAIK those don't exist.
>
> I messed around trying to do it with geom_hline etc, but this works better:
>
> data  <- data.frame(Name = c("A","B","C","D","E","F","G","H"), y = c(100,200,300,400,200,300,400,350))
> ggplot(data,aes(x=Name,y=y)) + geom_bar()
>
> grid.remove("panel.grid.minor.x.polyline",grep=T)
> grid.remove("panel.grid.major.x.polyline",grep=T)
> grid.remove("panel.grid.minor.y.polyline",grep=T)
>
> Note that ggsave seems to discard these edits, so I had to wrap this in png() and dev.off().
>
> Anyone know why ggsave() ignores grid level edits?
>
> I used the instructions here:
>
> https://github.com/hadley/ggplot2/wiki/Editing-raw-grid-objects-from-...
>
> Thx,
> James
>
> On Nov 27, 2010, at 12:51, thomas wrote:
>
>
>
>
>
>
>
> > Hi James,
>
> > stack overflow solution:
> >http://stackoverflow.com/questions/2678141/how-can-i-suppress-the-ver...

Kohske Takahashi

unread,
Nov 28, 2010, 8:35:46 AM11/28/10
to thomas, ggplot2
hi, try this:

guide_grid <- function(theme, x.minor, x.major, y.minor, y.major) {
ggname("grill", grobTree(
theme_render(theme, "panel.background"),

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))
),
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))
)
))
}
assignInNamespace("guide_grid", guide_grid, pos="package:ggplot2")

ggplot(mtcars, aes(factor(cyl))) + geom_bar()

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

thomas

unread,
Nov 28, 2010, 9:45:36 AM11/28/10
to ggplot2
hi,

that actually works very well. Thank you very much.

Best,
Thomas

James Howison

unread,
Nov 28, 2010, 10:11:45 AM11/28/10
to ggplot2
Thanks, that's very cool. Could you help a fellow out and walk me through what's going on here? Is guide_grid a ggplot2 function which you are redefining?

*digs a little*

Ah, with a fresh R session guide_grid shows the original function, that draws all four (major.x, minor.x, major.y, minor.y), so you cut out the theme_render commands that did the name="x" ones. Nice one.

So this works for swapping back and forth:

#new session
library(ggplot2)
all_guide_grid <- guide_grid
# define new

assignInNamespace("guide_grid", guide_grid, pos="package:ggplot2")

# do stuff
# return to all four
assignInNamespace("guide_grid", all_guide_grid, pos="package:ggplot2")

I suppose then one could write a function like:

theme_choose_grid <- function(x.minor = T, x.major = T, y.minor = T, y.major = T)

which chose which gridlines to display? I tried that but my r-fu is very shonky, failed as early as defining a function in a function ...

--J

Reply all
Reply to author
Forward
0 new messages