Order of bars in geom_bar() when using coord_flip()

2,469 views
Skip to first unread message

Merlyn

unread,
Apr 8, 2011, 12:47:14 PM4/8/11
to ggplot2
I have a data set that I would like to display some information as bar
charts using the coord_flip() option. Here is some example code.

set.seed(42)
n <- 1000
issue.name <- sample(c("Distance", "Time", "Weather", "Other"), n,
TRUE)
issue.answer <- sample(c("Yes", "No", "Not Sure", "No Response"), n ,
TRUE)
example.data <- data.frame(issue.name, issue.answer)

ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +
geom_bar(position = "dodge") + coord_flip()

What I would like to change is the order of either the bars or the
legend so that the color of the top bar is the same as the top color
in the legend - I find it is a little difficult to read when the top
color in the plot is the bottom color in the legend.

Does anyone have a suggestion on how to flip the order of the bars or
a better way to display the data?

Thanks,

Merlyn

unread,
Apr 8, 2011, 3:11:12 PM4/8/11
to ggplot2
Found the solution to my own question. Consider the code below.

set.seed(42)
n <- 1000
issue.name <- sample(c("Distance", "Time", "Weather", "Other"), n,
TRUE)
issue.answer <- sample(c("Yes", "No", "Not Sure", "No Response"), n ,
TRUE)
issue.answer <- factor(issue.answer,
labels = c("Yes", "No", "Not Sure", "No Response"), ordered = TRUE)

example.data <- data.frame(issue.name, issue.answer)

ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +
geom_bar(position = "dodge") + coord_flip() +
scale_fill_hue(
breaks = rev(levels(issue.answer)),
labels = rev(levels(issue.answer)))

Brian Diggs

unread,
Apr 8, 2011, 3:17:04 PM4/8/11
to ggplot2
On 4/8/2011 9:47 AM, Merlyn wrote:
> I have a data set that I would like to display some information as bar
> charts using the coord_flip() option. Here is some example code.
>
> set.seed(42)
> n<- 1000
> issue.name<- sample(c("Distance", "Time", "Weather", "Other"), n,
> TRUE)
> issue.answer<- sample(c("Yes", "No", "Not Sure", "No Response"), n ,
> TRUE)
> example.data<- data.frame(issue.name, issue.answer)
>
> ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +
> geom_bar(position = "dodge") + coord_flip()
>
> What I would like to change is the order of either the bars or the
> legend so that the color of the top bar is the same as the top color
> in the legend - I find it is a little difficult to read when the top
> color in the plot is the bottom color in the legend.

You can control the order that the items in the legend are displayed by
using the breaks argument of scale_fill_discrete:

ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +

geom_bar(position = "dodge") + coord_flip() +
scale_fill_discrete(breaks=c("Yes","Not Sure","No Response","No"))

> Does anyone have a suggestion on how to flip the order of the bars or
> a better way to display the data?

A hackish way to flip the order of the bars (relative to the order in
the legend) is to dodge them negatively:

ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +

geom_bar(position = position_dodge(width=-0.9)) + coord_flip()

The 0.9 is empirically determined; I tried different values starting at
-1 and adjusted until it looked right. I don't know if negative dodging
is supposed to work, but I've used it for this purpose before.

> Thanks,

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

Brian Diggs

unread,
Apr 8, 2011, 2:21:57 PM4/8/11
to ggp...@googlegroups.com
On 4/8/2011 9:47 AM, Merlyn wrote:
> I have a data set that I would like to display some information as bar
> charts using the coord_flip() option. Here is some example code.
>
> set.seed(42)
> n<- 1000
> issue.name<- sample(c("Distance", "Time", "Weather", "Other"), n,
> TRUE)
> issue.answer<- sample(c("Yes", "No", "Not Sure", "No Response"), n ,
> TRUE)
> example.data<- data.frame(issue.name, issue.answer)
>
> ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +
> geom_bar(position = "dodge") + coord_flip()
>
> What I would like to change is the order of either the bars or the
> legend so that the color of the top bar is the same as the top color
> in the legend - I find it is a little difficult to read when the top
> color in the plot is the bottom color in the legend.

You can control the order of the entries in the legend using the breaks
argument to scale_fill_discrete:

ggplot(example.data, aes(x = issue.name, fill = issue.answer)) +

geom_bar(position = "dodge") + coord_flip() +
scale_fill_discrete(breaks=c("Yes","Not Sure","No Response","No"))


> Does anyone have a suggestion on how to flip the order of the bars or
> a better way to display the data?
>
> Thanks,
>

Reply all
Reply to author
Forward
0 new messages