change order of bar plot categories

149 views
Skip to first unread message

Wade Wall

unread,
Oct 14, 2009, 3:40:06 PM10/14/09
to ggplot2
Hi all,

I am trying to change the order of bar plot categories, and not sure
how to do. I am graphing temperature means (low, medium, and high)
and want them in increasing temperature order, but qplot puts them in
alphabetical order. Any way to manually instruct qplot regarding the
order.

Here is some example code. The temperatures are arranged in the order
I would like them (first column).

temp<-c(rep("Low",2),rep("Medium",2),rep("High",2))
light<-rep(c("Dark","light"),3)
avg<-dat.avg2[,3]
se<-dat.avg2[,4]
dat.avg.temp<-data.frame(cbind(avg,se))
dat.avg.temp<-data.frame(cbind(temp,light,dat.avg.temp))
dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp,
geom="bar", position="dodge")

Any help would be appreciated,

Wade

Tobias Verbeke

unread,
Oct 14, 2009, 4:02:21 PM10/14/09
to Wade Wall, ggplot2
Hi Wade,

> I am trying to change the order of bar plot categories, and not sure
> how to do. I am graphing temperature means (low, medium, and high)
> and want them in increasing temperature order, but qplot puts them in
> alphabetical order. Any way to manually instruct qplot regarding the
> order.
>
> Here is some example code. The temperatures are arranged in the order
> I would like them (first column).
>
> temp<-c(rep("Low",2),rep("Medium",2),rep("High",2))


temp<-factor(c(rep("Low",2),rep("Medium",2),rep("High",2)),
levels = c("Low", "Medium", "High"))

or (to create an ordinal variable):

temp<-factor(c(rep("Low",2),rep("Medium",2),rep("High",2)),
levels = c("Low", "Medium", "High"), ordered = TRUE)


> light<-rep(c("Dark","light"),3)
> avg<-dat.avg2[,3]
> se<-dat.avg2[,4]
> dat.avg.temp<-data.frame(cbind(avg,se))
> dat.avg.temp<-data.frame(cbind(temp,light,dat.avg.temp))
> dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp,
> geom="bar", position="dodge")

HTH,
Tobias

Felipe Carrillo

unread,
Oct 14, 2009, 4:19:31 PM10/14/09
to ggplot2, Wade Wall, r-h...@stat.math.ethz.ch
Is this what you want?

temp<-c(rep("Low",2),rep("Medium",2),rep("High",2))
light<-rep(c("Dark","light"),3)
avg<-dat.avg2[,3] #
se<-dat.avg2[,4]
dat.avg.temp<-data.frame(cbind(avg,se))
dat.avg.temp<-data.frame(cbind(temp,light,dat.avg.temp))
dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp,
geom="bar", position="dodge") + scale_fill_discrete("Temp",labels=c('Low','Medium','High'))
dat.plot

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA


--- On Wed, 10/14/09, Wade Wall <wade...@gmail.com> wrote:

Wade Wall

unread,
Oct 15, 2009, 7:26:24 AM10/15/09
to Felipe Carrillo, ggplot2, r-h...@stat.math.ethz.ch
Hi Tobias and Felipe,

Both of your methods changed to the labels (L,M,H), but not the
corresponding values. The methods graph L as H, M as L, and M and H.

I have had display problems using ggplot2 before, but it seemed as if
it was because I was using Windows XP. I ran the current code using
Ubuntu 9.04 and got the same display issue.

Here is the code again, because I forgot to hard code the numbers.

temp<-factor(c(rep("Low",2),rep("Medium",2),rep("High",2)))
levels = c("Low", "Medium", "High"))
light<-rep(c("Dark","light"),3)
avg<-c(0.5200000,0.7800000, 0.3000000, 0.6000000, 0.0600000, 0.6078431)
se<-c(0.07065409, 0.05858327, 0.06480741, 0.06928203, 0.03358571, 0.06904634)
dat.avg.temp<-data.frame(cbind(avg,se))
dat.avg.temp<-data.frame(cbind(temp,light,dat.avg.temp))
dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp,
geom="bar", position="dodge")+
scale_fill_discrete("Temp",labels=c('Low','Medium','High'))
dat.plot

Thanks again for any help.

Wade

smu

unread,
Oct 15, 2009, 8:21:24 AM10/15/09
to ggplot2
hey,

you can define the level, when you define your factor variable:

lev = c("Low", "Medium", "High")
temp<-factor(c(rep("Low",2),rep("Medium",2),rep("High",2)),
levels=lev)
light<-rep(c("Dark","light"),3)
avg<-c(0.5200000,0.7800000, 0.3000000, 0.6000000, 0.0600000, 0.6078431)
se<-c(0.07065409, 0.05858327, 0.06480741, 0.06928203, 0.03358571, 0.06904634)
dat.avg.temp<-data.frame(cbind(avg,se))
dat.avg.temp<-data.frame(cbind(temp,light,dat.avg.temp))

dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp, geom="bar", position="dodge")

regards,
stefan

Xavier

unread,
Oct 15, 2009, 8:26:16 AM10/15/09
to ggplot2
Are you sure the first solution doesn't work? because it looks like it
works for me, and the code included in your last post is not correct
(in the creation of "temp", levels is not included as argument of
function "factor", so it all comes back to the problem in your first
code!)

The code below works:

temp<-factor(c(rep("Low",2),rep("Medium",2),rep("High",2)), levels = c
("Low", "Medium", "High") )
light<-rep(c("Dark","light"),3)
avg<-c(0.5200000,0.7800000, 0.3000000, 0.6000000, 0.0600000,
0.6078431)
dat.avg.temp<-data.frame(temp, light, avg)
dat.plot<-qplot(light,avg, fill=factor(temp),data=dat.avg.temp,
geom="bar", position="dodge")
dat.plot
Reply all
Reply to author
Forward
0 new messages