[R] qplot/ggplot2 Questions

Dennis Murphy djmuser at gmail.com
Tue Jun 21 20:11:12 CEST 2011


Hi:

On Tue, Jun 21, 2011 at 10:03 AM, wwreith <reith_william at bah.com> wrote:
> I took some data from an online poll about which R GUI people used most and I
> am messing around with it to learn how to use qplot. Specifically I am
> making a horizontal bar graph and I have two questions.
>
>
> 1. The categories are ordered in rather strange way at least to me. It is
> not alphabetical or ascending/descending order of votes cast so i had to
> manually state the order I wanted which is fine, but is there an easy way
> put the cateogies in alphabetical order or in my case descending order?
> Maybe something like: order="alphabetical"

Use the levels = argument of factor() to assign the order of the factor levels.
>
>
> 2. Is there an easy way to take a bar graph of count data and turn it into a
> bar graph of percentages. I know I can count how many items are in each
> category then divide by the total and then graph that, but is there
> something quick like geom="percent" or something that would do it
> automatically?

See the formatter = argument in scale_continuous(). Here's an example
from the on-line help page for scale_continuous():

x <- rnorm(10) * 100000
y <- seq(0, 1, length = 10)
p <- qplot(x, y)
p + scale_y_continuous(formatter = "percent")

You want to get your response in the 0-100 range before you start
ggplot(), though. If you already have the counts, it's just easier to
convert them to percentages in the data first before invoking ggplot()
or qplot().

HTH,
Dennis

>
>
> Code:
>
>
> library(ggplot2)
> x<-R.GUI.Poll$Poll
> q<-qplot(x, main="Most Popular R-GUIs", ylab="Votes Received", xlab="R GUI",
> fill=I("blue"), colour=I("black"))+coord_flip()+scale_x_discrete(
> limits=c("R via a data mining tool plugin","Rexcel","JGR (Java Gui for
> R)","RKWard","Revolution Analytics","R Commander","Rattle GUI","ESS (Emacs
> Speaks Statistics)","Tinn-R","RapidMiner R extension","Eclipse with
> StatET","Rstudio","Built-in R console"))
> q<-q+opts(axis.text.x = theme_text(colour = "black", size = 12))
> q<-q+opts(plot.title = theme_text(colour = "black", face="bold", size = 16,
> vjust=2))
> q<-q+opts(axis.text.y = theme_text(colour = "black", hjust=1))
> q<-q+opts(axis.title.y = theme_text(face= "bold", size= 12, angle=90))
> q<-q+opts(axis.title.x = theme_text(face= "bold", size= 12, vjust=-.5))
> q
>
> --
> View this message in context: http://r.789695.n4.nabble.com/qplot-ggplot2-Questions-tp3614722p3614722.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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