generate a barplot that retains the record order of the input data frame?

16 views
Skip to first unread message

Gordon Robertson

unread,
Jan 30, 2013, 8:50:51 AM1/30/13
to ggp...@googlegroups.com
I'm inexperienced with ggplot, so apologize if I've missed something simple. I'm using ggplot2_0.9.3. 

I'd like to generate a bar plot from an input data frame in which rows are sorted by descending value of a column. I'd like the bar plot to retain this record order. Big bars at the top, small at the bottom. Each bar has a label (a GO term), and a label is put beside its bar. 

When I generate the plot, the bars are reordered so that the labels are in order. In the example below, the plot's bars will be reordered to put bar 3 is at the top and 1 at the bottom. I'd like the bars to be in the order they are in, in df.o. 

> df <- data.frame( labs=c(2,1,3), vals=c(1.1,3.3,2.2) )
> df
  labs vals
1    2  1.1
2    1  3.3
3    3  2.2
> df.o <- df[order(-df$vals),]
> df.o
  labs vals
2    1  3.3
3    3  2.2
1    2  1.1
> pt <- ggplot( df.o, aes(x=labs, y=vals) )
> pt + geom_bar(stat="identity", fill="white", width=0.8, colour="darkgreen") + coord_flip()
# plotted as:
#3  2.2
#2  1.1
#1  3.3 

Thanks for your help.

G
--
Gordon Robertson
Michael Smith Genome Sciences Centre
BC Cancer Agency
Vancouver BC Canada
www.bcgsc.ca


Ista Zahn

unread,
Jan 30, 2013, 9:13:16 AM1/30/13
to Gordon Robertson, ggp...@googlegroups.com
Hi Gordon,

Please see http://stackoverflow.com/questions/tagged/ggplot2?sort=faq&pagesize=30.
The question at the top of the list answers your question.

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages