Annotations to demonstrate significant differences

1,186 views
Skip to first unread message

Tom W

unread,
Apr 6, 2012, 12:49:23 PM4/6/12
to ggp...@googlegroups.com
i had to plot some quantities and report the Tukey HSD results, so I adopted a  graphical technique which is common in many journals which mostly report exeperimental findings.


I'm embarrassed to include my code below, which involved me making new data frames for both facets' brackets, (which were drawn with geom_line) and then two additional data frames for the asterisks (which i included via geom_text). I futzed with coordinates until they looked uniform

my impression is that might be the sort of thing which would benefit from having a dedicated geom (maybe geom_signif) to save people the time of drawing them manually.  it would be pretty useful to a whole class of ggplot users.

tom


meanstable <- structure(list(courtn2 = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("Supreme Court", "Circuit Court", "District Court"
), class = "factor"), retireparty = structure(c(1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("Retirement\nCensored", "Democrat", 
"Republican"), class = "factor"), mu = c(0.0940190473232757, 
-0.062969184294343, 0.0639042453123973, 0.0856735609169068, 0.0162000814965422, 
0.0940789330503618, 0.0455207685436224, -0.00246113327864025, 
0.0720931271533302), lo = c(-0.0163260122239444, -0.223138576735342, 
-0.073602100432773, 0.062882398511004, -0.0328726041969101, 0.0540441984602888, 
0.0300675554538981, -0.0262136089624866, 0.0519872930763044), 
    hi = c(0.204364106870496, 0.097200208146656, 0.201410591057568, 
    0.10846472332281, 0.0652727671899945, 0.134113667640435, 
    0.0609739816333467, 0.0212913424052061, 0.092198961230356
    )), .Names = c("courtn2", "retireparty", "mu", "lo", "hi"
), row.names = c(NA, 9L), class = "data.frame")

segdf <- structure(list(x = c(2, 2, 3, 2, 1, 2), y = c(0.1, 0.107, 0.107, 
0.075, 0.075, 0.075), xend = c(2, 3, 3, 1, 1, 2), yend = c(0.107, 
0.107, 0.1, 0.075, 0.068, 0.068), courtn2 = structure(c(1L, 1L, 
1L, 1L, 1L, 1L), class = "factor", .Label = "District Court")), .Names = c("x", 
"y", "xend", "yend", "courtn2"), row.names = c(NA, -6L), class = "data.frame")

segdf2 <- structure(list(x = c(2, 2, 3, 2, 2, 1), y = c(0.139, 0.146, 0.146, 
0.116, 0.123, 0.123), xend = c(2, 3, 3, 2, 1, 1), yend = c(0.146, 0.146, 0.139, 0.123, 0.123, 0.116), courtn2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "Circuit Court")), .Names = c("x", "y", "xend", "yend", "courtn2"), row.names = c(NA, -6L), class = "data.frame")

anodf <- structure(list(x = c(2.5, 1.5), y = c(0.115, 0.083), courtn2 = structure(c(1L, 
1L), class = "factor", .Label = "District Court")), .Names = c("x", 
"y", "courtn2"), row.names = c(NA, -2L), class = "data.frame")

anodf2 <- structure(list(x = c(2.5, 1.5), y = c(0.154, 0.131), courtn2 = structure(c(1L, 
1L), class = "factor", .Label = "Circuit Court")), .Names = c("x", 
"y", "courtn2"), row.names = c(NA, -2L), class = "data.frame")

ggplot(meanstable)  +
                         geom_pointrange(size = 1.2, aes(x = retireparty, ymin = lo, ymax = hi, y = mu, color = retireparty)) +
                         facet_wrap(~courtn2, nrow = 1) + 
                         scale_color_manual(values=c("grey75","dodgerblue", "firebrick1")) +
                         scale_x_discrete("Presidential Party at retirement") +
                         scale_y_continuous("Judicial Ideology") + 
                         opts(legend.position = "none") +
                         geom_segment(data =  segdf, size = .8, aes(x=x, 
                                                                    y=y, 
                                                                    xend=xend, 
                                                                    yend=yend, 
                                                                    courtn2 = courtn2)) +
                         geom_text(data = anodf, 
                                   aes(x=x,
                                       y=y, 
                                       courtn2 = courtn2),
                                      label="***", size = 8)+
                          geom_segment(data =  segdf2, size = .8, aes(x=x, 
                                           y=y, 
                                           xend=xend, 
                                           yend=yend, 
                                           courtn2 = courtn2)) +
                          geom_text(data = anodf2, 
                                    aes(x=x,
                                        y=y,
                                        courtn2 = courtn2),
                                    label="**", size = 8)

Hadley Wickham

unread,
Apr 11, 2012, 4:48:25 PM4/11/12
to Tom W, ggp...@googlegroups.com
Nice idea - do you want to post something to https://github.com/hadley/ggplot2/issues?
Hadley

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



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

Tom W

unread,
Apr 11, 2012, 5:26:23 PM4/11/12
to ggp...@googlegroups.com
thanks Hadley--it's posted as #481. 


On Wednesday, April 11, 2012 3:48:25 PM UTC-5, Hadley Wickham wrote:
Nice idea - do you want to post something to https://github.com/hadley/ggplot2/issues?
Hadley

On Fri, Apr 6, 2012 at 11:49 AM, Tom W
i had to plot some quantities and report the Tukey HSD results, so I adopted a  graphical technique which is common in many journals which mostly report exeperimental findings.
Reply all
Reply to author
Forward
0 new messages