Discussion:
How to specifically order ggplot2 x or y axis instead of alphabetical order
Zongtai Qi
2013-05-12 09:17:59 UTC
Permalink
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
--
--
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 ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Troy S
2013-05-12 16:43:39 UTC
Permalink
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()
Post by Zongtai Qi
Greetings,
I'm new to the ggplot2 package and I'm trying to build a line graph.
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"))
missing value where TRUE/FALSE needed
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
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
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
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Adam T. Greer
2013-05-12 15:14:44 UTC
Permalink
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
Post by Zongtai Qi
Greetings,
I'm new to the ggplot2 package and I'm trying to build a line graph.
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"))
missing value where TRUE/FALSE needed
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
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
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
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
--
--
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 ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...