[R] Legend / bar order - ggplot2

Dennis Murphy djmuser at gmail.com
Mon Aug 29 19:41:26 CEST 2011


Hi:

The bars *are* ordered in the same way, but when you use coord_flip(),
the left category goes on top and the right category goes on the
bottom. Is this what you want?

ggplot(df, aes(x = name, y = value, fill = type)) +
  geom_bar(position = position_dodge()) +
  coord_flip() +
  scale_fill_manual(breaks = rev(levels(df$type)),
                               values = c('orange', 'blue'))

HTH,
Dennis

On Mon, Aug 29, 2011 at 6:18 AM, Yang Lu <Yang.Lu at williams.edu> wrote:
> Hi all,
>
> I am trying to do a barplot in ggplot2 and want to make sure that the legend order is consistent with the bar order, that is the legend order is orig and match; and the bars are ordered in the same way. It seems to me that I can only control one of them. Any idea?
>
> library(ggplot2)
> df <- data.frame(value = rnorm(20),
>                 name = factor(rep(letters[1:10], 2), levels = letters[1:10]),
>                 type = factor(c(rep("orig", 10),
>                   rep("match", 10)), levels = c("orig", "match")))
>
> ggplot(df, aes(x = name, y = value, fill = type)) +
>  geom_bar(position = position_dodge()) +
>  coord_flip()
>
> Thank you very much,
>
> YL
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list