changing order of x values in geom_pointrange

543 views
Skip to first unread message

miwes...@gmail.com

unread,
May 11, 2010, 10:11:21 PM5/11/10
to ggplot2
Hello:

I have a point range plot of monthly precipitation for a river basin.
What is the easiest way to change the order of months such that they
are not alphabetical? Using ddply sorts them alphabetically. Either
I want to resort the dataframe or change the order which ggplot plots
the data. Thank you.

Here is my code:

ganges_subbasin_a2pre_sort <- ddply(ganges_subbasin_a2pre, .(Month,
CATCHMENT_), summarise, med = median(precip), min = min(precip), max =
max(precip))

p <- ggplot(ganges_subbasin_a2pre_sort ,aes(x = Month, y = med, ymin =
min, ymax = max, subset = CATCHMENT_ == "Yamuna08")) +
ylab("Precipitation (mm/day)\n") + opts(title = subbasin) +
scale_colour_grey()


p + geom_pointrange(linetype = 2, colour = "navyblue") +
opts(axis.text.x = theme_text(colour = "black")) + opts(axis.text.y =
theme_text(colour = "black")) + opts(axis.line = theme_segment(colour
= "black")) + opts(axis.ticks = theme_segment(colour = "white"))

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brian Diggs

unread,
May 12, 2010, 10:50:03 AM5/12/10
to ggplot2


On May 11, 7:11 pm, "mwestp...@gmail.com" <miwestp...@gmail.com>
wrote:
> Hello:
>
> I have a point range plot of monthly precipitation for a river basin.
> What is the easiest way to change the order of months such that they
> are not alphabetical?  Using ddply sorts them alphabetically.  Either
> I want to resort the dataframe or change the order which ggplot plots
> the data.  Thank you.

The code isn't reproducible (since we don't have
ganges_subbasin_a2pre), and the structure of that data.frame (assuming
it is a data.frame since you are using d*ply on it) has a lot to do
with your problem. However, I have a few guesses.

I assume ganges_subbasin_a2pre$Month is a character variable. ddply
turns it into a factor, and factors by default alphabetically order
their levels, and ggplot2 uses the level order by default for
factors. One way to get around this is to turn ganges_subbasin_a2pre
$Month into a factor yourself with the levels in proper chronological
order. Then this order would be preserved. Alternatively, you can
convert your month information into a continuous date-like variable
(zoo::yearmon seems like it might be appropriate for you) and then it
will be treated as a continuous time scale.

--Brian Diggs
Reply all
Reply to author
Forward
0 new messages