Oxboys type line graph problem?

11 views
Skip to first unread message

neal

unread,
Aug 16, 2010, 5:42:22 PM8/16/10
to ggplot2
Hi

I am having this problem. My data starts out like this:

dat<-data.frame(prmtr=rep(1:4,c(3,3,3,3)),algo=rnorm(12))

(actually i have about 120 rows but this should do). Im trying to get
something that looks like the oxboys example, except I don't have a
grouping variable like subject, only the implicit order within each
value of prmtr.

i want to draw 3 lines joining 4 prmtr points (like the
Oxboys example in the book) based on the 3 instances of algo
within each parameter range. so i want the 1st line to join
4 points polygonally:

(1,1st algo next to prmtr=1)
(2,1st algo next to prmtr=2)
(3,1st algo next to prmtr=3)
(4,1st algo next to prmtr=4)

then the 2nd line should join:
(1,2nd algo next to prmtr=1)
(2,2nd algo next to prmtr=2)
(3,2nd algo next to prmtr=3)
(4,2nd algo next to prmtr=4)

and the 3rd line likewise.

but unlike the Oxboys sample, i don't have any Subject names
or id codes. they're just implicit.

(in fact, the ordering of the algo results within prmtr is
essentially random, so id also be interested in drawing 3 lines
involving the maxes, next highest, and lowest values within each
prmtr)

thanks a lot!

neal

Luciano Selzer

unread,
Aug 16, 2010, 6:33:57 PM8/16/10
to neal, ggplot2
Hi Neal, I don't quite understand what you want. But I believe the easy way is to add the implicit variable and then use the group argument in your plot.

Cheers
Luciano


2010/8/16 neal <googleac....@antichef.net>

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

Dennis Murphy

unread,
Aug 17, 2010, 7:14:23 AM8/17/10
to neal, ggplot2
Hi:

Try this:

dat <- data.frame(prmtr=rep(1:4,c(3,3,3,3)),algo=rnorm(12),
                   gp = factor(rep(1:3, 4)) )
g <- ggplot(dat, aes(x = prmtr, y = algo))
g + geom_line(aes(group = gp, colour = gp), size = 1.5) +
     scale_colour_discrete('Group')

In this code, the groups are defined by position in the data frame within level of prmtr. See below for more.

On Mon, Aug 16, 2010 at 2:42 PM, neal <googleac....@antichef.net> wrote:
Hi

I am having this problem.  My data starts out like this:

dat<-data.frame(prmtr=rep(1:4,c(3,3,3,3)),algo=rnorm(12))

(actually i have about 120 rows but this should do).  Im trying to get
something that looks like the oxboys example, except I don't have a
grouping variable like subject, only the implicit order within each
value of prmtr.

i want to draw 3 lines joining 4 prmtr points (like the
Oxboys example in the book) based on the 3 instances of algo
within each parameter range.  so i want the 1st line to join
4 points polygonally: 

(1,1st algo next to prmtr=1)
(2,1st algo next to prmtr=2)
(3,1st algo next to prmtr=3)
(4,1st algo next to prmtr=4)

then the 2nd line should join:
(1,2nd algo next to prmtr=1)
(2,2nd algo next to prmtr=2)
(3,2nd algo next to prmtr=3)
(4,2nd algo next to prmtr=4)

and the 3rd line likewise.

but unlike the Oxboys sample, i don't have any Subject names
or id codes.  they're just implicit.

But that doesn't mean you can't define a grouping variable in your data frame. See my example above for one approach. Implicit can be made explicit :)


(in fact, the ordering of the algo results within prmtr is
essentially random, so id also be interested in drawing 3 lines
involving the maxes, next highest, and lowest values within each
prmtr)

This would not be easy to do within ggplot2 per se because you're asking the software to do dynamic grouping when it expects static groups to be predefined. If you intend to make your response random and group conditionally on size, then it may be more prudent to (i) generate the data frame; (ii) add an intermediate step where you define the groups according to rank within prmtr and add that variable to the data frame, and then (iii) use ggplot(). It shouldn't be too difficult to write all of that up in a wrapper function. You want to do as much data manipulation and summarization outside of ggplot2 as possible, if for no other good reason than to simplify your code.

Here's an example:

dat<-data.frame(prmtr=rep(1:4,c(3,3,3,3)),algo=rnorm(12))
d2 <- ddply(dat, .(prmtr), transform, algo = sort(algo), gp = seq(algo))
g <- ggplot(d2, aes(x = prmtr, y = algo))
g + geom_line(aes(group = factor(gp), colour = factor(gp)), size = 1.5) +
    scale_colour_discrete('Group')

The idea:  generate the random data, pass it through ddply to do the sorting and grouping, then call ggplot() to generate the graph.
If you intend to do this repeatedly, then write a self-contained wrapper function that you can simply call in one line.

HTH,
Dennis

Hadley Wickham

unread,
Aug 17, 2010, 9:47:28 AM8/17/10
to Dennis Murphy, neal, ggplot2
> But that doesn't mean you can't define a grouping variable in your data
> frame. See my example above for one approach. Implicit can be made explicit
> :)

This is very much in keeping with the philosophy of data that
underlies ggplot2 and plyr - you're better off being explicit about
what you want, even if it requires a little typing.

The places in ggplot2 were things are implicit/automated/magical tend
to be the things that people struggle the most with (e.g. grouping).

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Brandon Hurr

unread,
Aug 17, 2010, 10:10:54 AM8/17/10
to Hadley Wickham, Dennis Murphy, neal, ggplot2
I'm going with "magical" until proven otherwise. 


--

neal

unread,
Aug 17, 2010, 1:33:38 PM8/17/10
to ggplot2
Hi dennis and hadley

[picking jaw up off the floor]

thanks so much. i can't believe this works so well. what a piece of
code and thanks so much for helping me!

-neal



On Aug 17, 7:14 am, Dennis Murphy <djmu...@gmail.com> wrote:
> Hi:
>
> Try this:
>
> dat <- data.frame(prmtr=rep(1:4,c(3,3,3,3)),algo=rnorm(12),
>                    gp = factor(rep(1:3, 4)) )
> g <- ggplot(dat, aes(x = prmtr, y = algo))
> g + geom_line(aes(group = gp, colour = gp), size = 1.5) +
>      scale_colour_discrete('Group')
>
> In this code, the groups are defined by position in the data frame within
> level of prmtr. See below for more.
>
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>
> > More options:http://groups.google.com/group/ggplot2
Reply all
Reply to author
Forward
0 new messages