How to specifically order ggplot2 x or y axis instead of alphabetical order

11,390 views
Skip to first unread message

Zongtai Qi

unread,
May 12, 2013, 5:17:59 AM5/12/13
to ggp...@googlegroups.com
Greetings,

I'm new to the ggplot2 package and I'm trying to build a line graph.

The code is as below:

data <- read.csv ('my.data.csv',header =T)
library(ggplot2)
p <- ggplot(data,aes(x=stage,y=trans.effi, group = trans.method))
p + geom_point(size=6) +
  theme_bw() + geom_line () +
  scale_x_discrete (limits = c('stage1', 'stage2', 'stage3'))

data is my.data.csv file
my X axis is different stages
my Y axis is the values for the different stages

I just want to be able to specify the order of the labels on the x axis. In this case, I'm trying to specify the order of "Stage". By default, it orders alphabetically. How do I override this/keep the data in the same order as in my original csv file?

I've tried this command

scale_x_discrete(limits=c("stage3","stage2","stage1"))

It however doesn't work very well, and give me missing error messages:


Error in if (zero_range(from) || zero_range(to)) return(rep(mean(to),  :
missing value where TRUE/FALSE needed
Warning messages:
1: Removed 4 rows containing missing values (geom_point).

So the more general question is how to specify the order of ggplot2 x or y axis instead of alphabetical order?

Thanks,

Zongtai

Troy S

unread,
May 12, 2013, 12:43:39 PM5/12/13
to Zongtai Qi, ggplot2
Reorder the factor as GGplot goes by that.
 d=data.frame(x=sample(letters[1:5], 100,replace=T), y=rnorm(100))
 d$x<-factor(d$x, levels=c('c','d','e','a','b'))
 ggplot(d,aes(x,y))+geom_boxplot()



--
--
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.
 
 

Adam T. Greer

unread,
May 12, 2013, 11:14:44 AM5/12/13
to ggp...@googlegroups.com
Before your ggplot() call, try

data <- within(data, stage <- factor(stage, levels = c("stage1", "stage2", "stage3")))

you would do the same to specify the order of a categorical variable on the y axis.

-Adam
--
--
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.
 
 


-- 
Adam T. Greer
Ph.D Candidate, Graduate Research Assistant
Marine Biology and Fisheries
Rosenstiel School of Marine and Atmospheric Science
221 South Grosvenor
Phone: 305-421-4025
Reply all
Reply to author
Forward
0 new messages