[R] ggplot2 geom_bar arrangement

Thomas Mailund thomas.mailund at gmail.com
Wed Jun 28 05:52:45 CEST 2017


The order the bars are plotted in is determined by the levels in a factor, and your labels are treated as a factor. You can make sure you keep the order of your labels by simply doing this:

Lab <- factor(Lab, levels = Lab)

before constructing the data frame.

Cheers

On 27 Jun 2017, 20.43 +0200, Brian Smith <bsmith030465 at gmail.com>, wrote:
> Hi,
>
> I was trying to draw a geom_bar plot. However, by default, the bars are
> arranged according to the label, which I don't want. I want the bars to
> appear exactly as they appear in the data frame. For example in the code:
>
> Lab=c(letters[4:6],letters[1:3])
> valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
> df <- data.frame(Lab,valuex)
> px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
> geom_bar(stat = "identity")
> px
>
>
> The default arranges the bars in order 'a' through 'f', but I want them
> arranged as per df.
>
> How can I do this?
>
> thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list