Problem: Plotting by numeric order instead of factor order

29 views
Skip to first unread message

Pat Schmitz

unread,
Sep 19, 2009, 5:30:43 PM9/19/09
to ggp...@googlegroups.com
I am using factored/ordered data, and ggplot seems to be ignoring the factor order, and instead following the continuous/numeric data order.  I have attached a jpeg of an ouput plot, this ordering makes it really difficult to interpret the data, and I am sure I am missing something small.  How do I get this to plot by factor correctly?

below is a subset of data, and attached is subset plotted

Data
          time           treatment    variable    value
4   08/25/09:N Ambient CO2 Control  X10cm.mean 36.39274
16  08/25/09:N Ambient CO2 Control  X20cm.mean 40.36536
28  08/25/09:N Ambient CO2 Control  X30cm.mean 42.46743
40  08/25/09:N Ambient CO2 Control  X40cm.mean 43.48431
52  08/25/09:N Ambient CO2 Control  X50cm.mean 47.00896
64  08/25/09:N Ambient CO2 Control  X60cm.mean 48.11792
76  08/25/09:N Ambient CO2 Control  X70cm.mean 47.54671
88  08/25/09:N Ambient CO2 Control  X80cm.mean 48.06299
100 08/25/09:N Ambient CO2 Control  X90cm.mean 47.04015
112 08/25/09:N Ambient CO2 Control X100cm.mean 47.50861

form$time <- ordered(form$time)
form$treatment <- factor(form$factor)
form$variable <- ordered(form$variable)
form$time <- ordered(form$time)

Data Classes
> sapply(form, data.class)
     time treatment  variable     value
"ordered"  "factor" "ordered" "numeric"

Plot Code
p <- ggplot(form, aes(x=value, y=variable, group=treatment, colour=treatment))
p + geom_line() + geom_point()

--
Patrick Schmitz
Graduate Student
Plant Biology
1206 West Gregory Drive
RM 1500
trtmeans.jpg

Pat Schmitz

unread,
Sep 19, 2009, 6:38:11 PM9/19/09
to Luciano Selzer, ggp...@googlegroups.com
The x axis is a continuous value, labeled value.

The Y axis is a categorical orderd factor, labeled variable

the geom_line() should plot the continuous value points along the categorical value.

Where instead it is plotting the categorical variable along the continuous value.

Pat

On Sat, Sep 19, 2009 at 5:19 PM, Luciano Selzer <luciano...@gmail.com> wrote:
Hi Patrick, I'm not sure I'm understanding. What do you want to plot? What should be on the X axis? and on the Y?
Luciano


2009/9/19 Pat Schmitz <p.sc...@gmail.com>

hadley wickham

unread,
Sep 20, 2009, 10:43:24 AM9/20/09
to Pat Schmitz, Luciano Selzer, ggp...@googlegroups.com
Hi Pat,

A line is order by the x values. If you want to order by something
different, using the path geom and the order aesthetic.

Hadley
--
http://had.co.nz/

Pat Schmitz

unread,
Sep 20, 2009, 6:59:22 PM9/20/09
to hadley wickham, Luciano Selzer, ggp...@googlegroups.com
Thanks Hadley

geom_path offered the functionality I was looking for.

One further question. I have been attempting to change the order and direction of the data as plotted on the y axis.  Where 0 would be at the top of the y axis, and 100 at the bottom, conceptually representing depth down from surface.

I have been attempting to change the ordering of my factor or trying to find an order method along with aes, but can't seem get the factor to order and plot in a reverse fashion.

Pat

Luciano Selzer

unread,
Sep 20, 2009, 8:55:57 PM9/20/09
to Pat Schmitz, ggp...@googlegroups.com
Hi Pat, try this:

form$variables<-factor(form$variables, levels=rev(levels(form$variables)), ordered=T)

bye

Luciano


2009/9/20 Pat Schmitz <p.sc...@gmail.com>

Pat Schmitz

unread,
Sep 21, 2009, 2:39:45 PM9/21/09
to Luciano Selzer, ggp...@googlegroups.com
That does it! Thanks so much.
Pat
Reply all
Reply to author
Forward
0 new messages