getting line through dodged points using geom_pointrange

2,154 views
Skip to first unread message

Juliet Hannah

unread,
Sep 16, 2010, 4:17:38 PM9/16/10
to ggplot2
Hi Group,

I have some example data and a plot below. I would like to get a line
to connect the points for each treatment. I have not
been able to do this maybe because it is dodged. Any suggestions?

Thanks,

Juliet

library(ggplot2)
myData <- structure(list(treatment = c(0L, 0L, 100L, 100L, 200L, 200L,
200L, 600L, 0L, 100L, 600L), time = structure(c(1L, 3L, 2L, 3L,
1L, 2L, 3L, 2L, 2L, 1L, 1L), .Label = c("1a", "1b", "1c"), class = "factor"),
mean = c(8.351316, 8.200078, 10.355362, 8.709843, 10.336664,
10.404017, 8.984964, 10.519717, 7.955357, 10.301795, 10.35637
), uci = c(8.592008, 8.454073, 10.65889, 9.105403, 10.62428,
10.754237, 9.425563, 10.710556, 8.402852, 10.301795, 10.530051
), lci = c(8.110625, 7.946084, 10.051834, 8.314283, 10.049048,
10.053798, 8.544365, 10.328879, 7.507862, 10.301795, 10.182688
)), .Names = c("treatment", "time", "mean", "uci", "lci"), class =
"data.frame", row.names = c(NA,
-11L))

p <- ggplot(myData)
p <- p + geom_pointrange(aes(x=time,y=mean,ymin=lci,ymax=uci,colour=factor(treatment)),position=position_dodge(width=1))
p

Brandon Hurr

unread,
Sep 16, 2010, 4:48:53 PM9/16/10
to Juliet Hannah, ggplot2
Juliet, 

I could get it to plot a line, but like you probably found because of the dodge the line did not match the location of the points. 

p+geom_line(aes(x=time, y=mean, group=treatment, position='dodge'))

Telling it that it was dodged didn't help or change anything. 

All that being said... would a different graph be just as good?

Something along these lines (but prettier)?

p <- ggplot(myData)
  p <- p + geom_bar(aes(x=factor(treatment), y=mean,fill=factor(treatment)),position=position_dodge(width=1))+facet_grid(.~time)
 p+geom_errorbar(aes(x=factor(treatment), ymin=lci, ymax=uci))

Brandon


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

Juliet Hannah

unread,
Sep 16, 2010, 6:07:32 PM9/16/10
to Brandon Hurr, ggplot2
Hi Brandon,

Thanks for the suggestion.

I do need to get the graph in the form I was trying. It was requested
in the form
I described.

Thanks again for your help.

Regards,

Juliet

Dennis Murphy

unread,
Sep 16, 2010, 6:14:12 PM9/16/10
to Brandon Hurr, Juliet Hannah, ggplot2
Hi:

No, Brandon...that's a dynamite plot. I don't care how pretty it is. I see I have to resurrect this link again:

http://biostat.mc.vanderbilt.edu/wiki/Main/DynamitePlots

Make sure you read to the bottom of the page, and follow the link to the poster :)


The problem Juliet has in trying to connect the lines between treatments is twofold:
     (i) Time is a factor, and the only way geom_line() will plot across factor levels is with geom_line(aes(group = 1)). That's a problem here because treatments are the desired grouping factors.
    (ii) Treatments are dodged by levels of Time.

Brandon is on the right track, but the better visual metaphor is her original linerange plots. Here are a couple of examples; the widths and sizes may be a bit exaggerated, but these can be tuned to taste. One can also use a different color from black for the line, but it will be added to the legend if you do.

q <- ggplot(myData)
q + geom_pointrange(aes(x= treatment, y=mean, ymin=lci, ymax=uci,
                        colour=factor(treatment)), width = 1.2, size = 1.2) +
    geom_line(aes(x = treatment, y = mean), size = 1) +
    facet_grid(. ~ time) +
    scale_colour_discrete('Treatment')

q + geom_pointrange(aes(x = time, y=mean, ymin=lci, ymax=uci,
                        colour=factor(treatment)), width = 1.2, size = 1.2) +
    geom_line(aes(x = time, y = mean, group = 1), size = 1) +
    facet_grid(. ~ treatment) +
    scale_colour_discrete('Treatment')

HTH,
Dennis

Dennis Murphy

unread,
Sep 16, 2010, 6:35:01 PM9/16/10
to ggplot2
Hi:

A couple of notes:

(1) In my last post on this thread, I mentioned that adding a line with color in geom_line() will add an entry to the legend; that's true if colour is mapped as an aesthetic, but not if it's set. Here's an example, with a toned down line size, using the myData data frame in the quoted text section:


q <- ggplot(myData)
q + geom_pointrange(aes(x= treatment, y=mean, ymin=lci, ymax=uci,
                        colour=factor(treatment)), width = 1.2, size = 1.2) +
    geom_line(aes(x = treatment, y = mean), colour = 'blue') +
    facet_grid(. ~ time) +
    scale_colour_discrete('Treatment')

Hope this clarifies matters.
Dennis

Brandon Hurr

unread,
Sep 17, 2010, 3:57:55 AM9/17/10
to Dennis Murphy, ggplot2
Juliet, 

Do you want to connect within time between treatments or within treatments between time?

Seems a bit odd to do it the way we've done it so far is all. 
#Previous way between treatments within time
q <- ggplot(myData)
q + geom_pointrange(aes(x= treatment, y=mean, ymin=lci, ymax=uci, 
                        colour=factor(treatment)), width = 1.2, size = 1.2) +
                        geom_line(aes(x = treatment, y = mean), colour = 'blue') + 
                        facet_grid(. ~ time) +
                        scale_colour_discrete('Treatment')
#ggsave("W.Time.B.Treat.png", width=4, height=4)
--------------------------------------------------------------------

#different way, between times within treatment
q <- ggplot(myData)
q + geom_pointrange(aes(x= factor(time), y=mean, ymin=lci, ymax=uci, 
colour=factor(treatment)), width = 1.2, size = 1.2) +
geom_line(aes(x = factor(time), y = mean, group=1), colour = 'blue') + 
facet_grid(. ~ treatment) +
scale_colour_discrete('Treatment')

#ggsave("W.Treat.B.Time.png", width=4, height=4)

Gives you more options anyway...

Brandon
W.Treat.B.Time.png
Reply all
Reply to author
Forward
0 new messages