Equal bar width while using facet_grid

1,386 views
Skip to first unread message

Jokel Meyer

unread,
Jan 10, 2012, 4:56:57 PM1/10/12
to ggplot2
Dear ggplot2 forum!

I would like to plot barplots (continuos variable on y axis,
categorical on x axis) while facetting over another categorical
variable. I am using scale="free_x" to drop missing factor levels.
However (see dummy example below) this leads to equal width of the
facet plots but unequal width of the plotted bars. I would like the
opposite: equal width of the bars and unequal width of the single
facet plots. I think otherwise different bar width is misleading.

Thanks for your help!

data <- diamonds[1:10,]
data$cut <- as.factor(as.character(data$cut))
data$color <- as.factor(as.character(data$color))
d <- ggplot(data, aes(cut,carat))
d + geom_bar(stat="identity")+ facet_grid(.~ color,scale="free_x")

Brian Diggs

unread,
Jan 10, 2012, 5:04:40 PM1/10/12
to ggplot2

Set the space to be free too.

d + geom_bar(stat="identity") +
facet_grid(.~ color, scale="free_x", space="free")


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Jokel Meyer

unread,
Jan 11, 2012, 1:50:57 AM1/11/12
to ggp...@googlegroups.com
Thank you very much for the quick answer!
 
I was wondering why this works fine until you used coord_flip()?
 
data <- diamonds[1:10,]
data$cut <- as.factor(as.character(data$cut))
data$color <- as.factor(as.character(data$color))
d <- ggplot(data, aes(cut,carat))
d + geom_bar(stat="identity")+ facet_grid(color~.,scale="free_x", space="free")
 
How could you plot the above example with even bar widths and skipping unused factor levels?
Many thanks!

Brian Diggs

unread,
Jan 12, 2012, 1:49:21 PM1/12/12
to ggplot2
On 1/10/2012 10:50 PM, Jokel Meyer wrote:
> Thank you very much for the quick answer!
>
> I was wondering why this works fine until you used coord_flip()?

coord_flip() is evil. Wait, that is too harsh. coord_flip() is more
likely to cause problems than it is to solve them. I have a personal
(although practical) vendetta against it.

This is a good example; there is no way to do what you want to do.
space="free" does nothing in the presence of coord_flip(). The
"correct" solution is a geom which draws horizontal bars. See
http://groups.google.com/group/ggplot2/msg/24400aadbbb9bc8e for some
code which can do that. Actually that entire thread
(http://groups.google.com/group/ggplot2/browse_thread/thread/583fcfdced24b483/)
addresses this problem.

> data<- diamonds[1:10,]
> data$cut<- as.factor(as.character(data$cut))
> data$color<- as.factor(as.character(data$color))
> d<- ggplot(data, aes(cut,carat))
> d + geom_bar(stat="identity")+ facet_grid(color~.,scale="free_x",
> space="free")
>
> How could you plot the above example with even bar widths and skipping
> unused factor levels?
> Many thanks!

With the above mentioned custom geom, you could then have

ggplot(data, aes(carat, cut)) +
geom_hbar() +
facet_grid(color ~ ., scale="free_y", space="free")

A caveat: this will surely break with the new ggplot2 0.9.0 that is due
out Any Day Now.

Reply all
Reply to author
Forward
0 new messages