Dimitri Liakhovitski
2013-Oct-30 14:35 UTC
[R] ggplot2 question: keeping the order as in the input data
Hello!
I am using ggplot2 (see the code below) to plot the data in
'myplotdata'.
The first column of 'myplotdata' is called "att.levels" and
contains
strings; the second column is called "WTP" and contains numeric
values.
Notice - I use 'coord.flip()'
The command aes(x=att_levels, y=WTP), if I understand correctly, sorts
things alphabetically based on the column 'att_levels'.
Question 1: How can I reverse the order for x in the plot (also
alphabetically but in the opposite direction)?
Question 2: How can I just have exactly the same order as in the object
'myplotdata'?
Thanks a lot!
ggplot(myplotdata, aes(x=att_levels, y=WTP)) +
geom_bar(stat="identity",fill="dark
orange",colour="black",
alpha = 1,position = "identity") +
geom_text(aes(label=WTP),colour="black",size=4,hjust=1.1,position='dodge')
+
coord_flip() +
xlab("") +
ylab("")
--
Dimitri Liakhovitski
[[alternative HTML version deleted]]
William Dunlap
2013-Oct-30 16:04 UTC
[R] ggplot2 question: keeping the order as in the input data
Try making att_levels (or att.levels, whatever you really call it)
into a factor with the levels in the order you like. E.g.,
aes(x = factor(att_levels, levels=unique(att_levels)), y = WTP)
instead of
aes(x = att_levels, y = WTP)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf
> Of Dimitri Liakhovitski
> Sent: Wednesday, October 30, 2013 7:35 AM
> To: r-help
> Subject: [R] ggplot2 question: keeping the order as in the input data
>
> Hello!
>
> I am using ggplot2 (see the code below) to plot the data in
'myplotdata'.
> The first column of 'myplotdata' is called "att.levels"
and contains
> strings; the second column is called "WTP" and contains numeric
values.
> Notice - I use 'coord.flip()'
>
> The command aes(x=att_levels, y=WTP), if I understand correctly, sorts
> things alphabetically based on the column 'att_levels'.
> Question 1: How can I reverse the order for x in the plot (also
> alphabetically but in the opposite direction)?
> Question 2: How can I just have exactly the same order as in the object
> 'myplotdata'?
>
> Thanks a lot!
>
> ggplot(myplotdata, aes(x=att_levels, y=WTP)) +
> geom_bar(stat="identity",fill="dark
orange",colour="black",
> alpha = 1,position = "identity") +
>
>
geom_text(aes(label=WTP),colour="black",size=4,hjust=1.1,position='dodge')
+
> coord_flip() +
> xlab("") +
> ylab("")
>
>
>
>
> --
> Dimitri Liakhovitski
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.