Is there any way to plot error bars in one facet of time series line graphs?

25 views
Skip to first unread message

Jennifer Krauel

unread,
Feb 14, 2013, 11:34:50 AM2/14/13
to ggp...@googlegroups.com
I want to print relevant variables for one field season over time.  I'm comparing the amount of north wind, number of moths caught in traps, mass of the local bats, and the number of hours those bats are foraging.  You'll see that some dates are missing for some variables; bats were not sampled every day, for example, but ggplot handles that appropriately when I used na=rm in the melt.   In the code below, the first data frame contains the basic data for all the variables of interest. 

One of these variables is an average (daily bat mass). The second data frame below contains the standard error for bat mass, one of the variables.  Since I've melted the variables, I don't think it is possible to put the std error values into that first melted data frame.

As you can see in the ggplot call at the end, this does in fact produce a graph that's close to what I want.  It would be great if I could somehow add error bars to one of the facets.  Is this at all possible? 
Thanks,
Jennifer
University of Tennessee, Knoxville

library(ggplot2)

#load a data frame with several variables from a single year

#for printing as facets

combo2012 <- structure(list(date = structure(c(15568, 15569, 15570, 15571, 

15572, 15573, 15574, 15575, 15576, 15577, 15578, 15579, 15580, 

15581, 15582, 15583, 15584, 15585, 15586, 15587, 15588, 15589, 

15590, 15591, 15592, 15593, 15594, 15595, 15596, 15597, 15598, 

15599, 15600, 15601, 15602, 15603, 15604, 15605, 15606, 15607, 

15608, 15609, 15610, 15611, 15612, 15613, 15614, 15615, 15616, 

15617, 15618, 15619, 15620, 15621, 15622, 15623, 15624, 15625, 

15626, 15627, 15628, 15629, 15630, 15631, 15634, 15635, 15636, 

15637, 15638, 15639, 15640, 15641, 15642, 15643, 15644, 15645, 

15646, 15647, 15648, 15649, 15650, 15651, 15652, 15653, 15654, 

15655, 15656, 15657, 15658, 15659, 15578, 15579, 15580, 15581, 

15582, 15583, 15584, 15585, 15586, 15587, 15588, 15589, 15590, 

15591, 15592, 15593, 15594, 15595, 15596, 15597, 15598, 15599, 

15600, 15601, 15602, 15603, 15604, 15605, 15606, 15607, 15608, 

15609, 15610, 15611, 15612, 15613, 15614, 15615, 15616, 15617, 

15618, 15619, 15620, 15621, 15622, 15623, 15624, 15625, 15626, 

15627, 15628, 15629, 15630, 15631, 15632, 15633, 15634, 15635, 

15636, 15637, 15638, 15639, 15640, 15641, 15642, 15643, 15644, 

15645, 15646, 15647, 15648, 15649, 15650, 15651, 15652, 15653, 

15654, 15655, 15656, 15657, 15578, 15580, 15583, 15585, 15587, 

15589, 15591, 15593, 15595, 15598, 15600, 15602, 15604, 15606, 

15608, 15610, 15613, 15615, 15617, 15619, 15621, 15623, 15625, 

15627, 15629, 15631, 15633, 15635, 15637, 15638, 15641, 15643, 

15645, 15647, 15649, 15651, 15654, 15656, 15580, 15581, 15582, 

15583, 15584, 15585, 15586, 15587, 15588, 15589, 15590, 15591, 

15592, 15593, 15594, 15595, 15596, 15597, 15598, 15599, 15600, 

15601, 15602, 15603, 15604, 15605, 15606, 15607, 15608, 15609, 

15610, 15611, 15612, 15613, 15614, 15615, 15616, 15617, 15618, 

15619, 15620, 15621, 15622, 15623, 15624, 15625, 15626, 15627, 

15628, 15629, 15630, 15631, 15632, 15633, 15634, 15635, 15636, 

15637, 15638, 15639, 15640, 15641, 15642, 15643, 15644, 15645, 

15646, 15647, 15648, 15649, 15650, 15651, 15652, 15653, 15654, 

15655), class = "Date"), variable = structure(c(1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 

2L, 2L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 4, 4, 

1, 0, 0, 8, 19, 15, 5, 7, 57, 25, 3, 2, 5, 12, 17, 26, 7, 5, 

9, 12, 45, 48, 16, 69, 86, 55, 64, 39, 66, 42, 87, 30, 245, 364, 

277, 529, 211, 180, 143, 122, 101, 108, 477, 107, 119, 282, 211, 

186, 169, 749, 627, 160, 391, 314, 455, 207, 121, 127, 133, 162, 

145, 54, 47, 69, 64, 66, 54, 34, 44, 37, 1, 3, 1, 3, 8, 1, 5, 

14, 26, 14, 46, 2, 3, 9, 12, 16, 25, 1, 11.1, 12.96, 11.75, 11.34, 

11.17, 11.59, 12.88, 12.01, 11.57, 12.3, 12.02, 12.28, 11.34, 

11.29, 11.66, 11.78, 12.27, 13.4, 12.12, 11.58, 11.7, 11.28, 

11.96, 11.64, 12.72, 13.73, 11.56, 12.31, 11.98, 11.91, 12.92, 

12.59, 12.4, 12.49, 13.59, 13.02, 13.05, 14.28, 11.12, 11.37, 

10.87, 10.62, 11.38, 11.3, 11.58, 11.28, 11.78, 12.05, 14.13, 

11.12, 10.85, 10.65, 12.2, 12.78, 14.05, 11.55, 12.22, 11.73, 

11.55, 11.1, 12.22, 10.57, 11.43, 11.8, 11.9, 11.68, 11.62, 11.85, 

12.17, 11.98, 12.52, 11.87, 11.53, 11.37, 11.67, 11.52, 11.6, 

11.92, 11.95, 12.2, 12.65, 12.15, 12.52, 11.92, 12.27, 11.9, 

12.28, 11.97, 12.32, 11.1, 12.67, 12.12, 12.03, 12.22, 12.17, 

11.97, 12.28, 12.03, 11.85, 11.77, 11.37, 12.33, 12.15, 11.7, 

11.85, 12.02, 12.32, 12.27, 11.58, 11.7, 12.18, 12.3, 12.28, 

12.08)), .Names = c("date", "variable", "value"), row.names = c(1L, 

2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 

16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 

29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 

42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 

55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 67L, 68L, 69L, 

70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 

83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 103L, 104L, 

105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 

116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 

127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 

138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 

149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 

160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 

171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 

182L, 195L, 197L, 200L, 202L, 204L, 206L, 208L, 210L, 212L, 215L, 

217L, 219L, 221L, 223L, 225L, 227L, 230L, 232L, 234L, 236L, 238L, 

240L, 242L, 244L, 246L, 248L, 250L, 252L, 254L, 255L, 258L, 260L, 

262L, 264L, 266L, 268L, 271L, 273L, 289L, 290L, 291L, 292L, 293L, 

294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 

305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L, 315L, 

316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 

327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 

338L, 339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 

349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 

360L, 361L, 362L, 363L, 364L), class = "data.frame")


#this data frame contains standard error vals for one of the

#faceted values in the data structure above

sevals <- structure(list(date = structure(c(15578, 15580, 15583, 15585, 

15587, 15589, 15591, 15593, 15595, 15598, 15600, 15602, 15604, 

15606, 15608, 15610, 15613, 15615, 15617, 15619, 15621, 15623, 

15625, 15627, 15629, 15631, 15633, 15635, 15637, 15638, 15641, 

15643, 15645, 15647, 15649, 15651, 15654, 15656), class = "Date"), 

    massse = c(0.23, 0.2, 0.18, 0.14, 0.1, 0.23, 0.13, 0.13, 

    0.19, 0.2, 0.24, 0.2, 0.12, 0.16, 0.13, 0.18, 0.2, 0.23, 

    0.19, 0.17, 0.15, 0.14, 0.27, 0.19, 0.19, 0.32, 0.26, 0.27, 

    0.26, 0.22, 0.25, 0.28, 0.24, 0.26, 0.32, 0.27, 0.31, 0.29

    )), .Names = c("date", "massse"), row.names = c(145L, 147L, 

150L, 152L, 154L, 156L, 158L, 160L, 162L, 165L, 167L, 169L, 171L, 

173L, 175L, 177L, 180L, 182L, 184L, 186L, 188L, 190L, 192L, 194L, 

196L, 198L, 200L, 202L, 204L, 205L, 208L, 210L, 212L, 214L, 216L, 

218L, 221L, 223L), class = "data.frame")

#plot the first data frame

ggplot (data=combo2012, aes(x = date,y=value,group=variable)) +

        geom_line() +

        scale_x_date(limits = c(as.Date("2012-8-16"),

                             as.Date("2012-11-15")))+

        facet_grid(variable ~ ., scale = "free_y")

Ista Zahn

unread,
Feb 14, 2013, 2:16:40 PM2/14/13
to Jennifer Krauel, ggp...@googlegroups.com
Hi Jennifer,

Yep, should be no problem:

sevals$variable <- factor("avgmass",
levels = levels(combo2012$variable))
combo2012 <- merge(combo2012, sevals, all=TRUE)
combo2012 <- within(combo2012, {
lwr <- value - 1.96*massse
upr <- value + 1.96*massse
})

ggplot (data=combo2012, aes(x = date,y=value,group=variable)) +
geom_ribbon(aes(ymin = lwr, ymax=upr)) +
geom_line() +
scale_x_date(limits = c(as.Date("2012-8-16"), as.Date("2012-11-15")))+
facet_grid(variable ~ ., scale = "free_y")

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ista Zahn

unread,
Feb 14, 2013, 2:18:10 PM2/14/13
to Jennifer Krauel, ggp...@googlegroups.com
oops, I just say "error _bars_". Just replace geom_ribbon with
geom_errorbar for that.

Best,
Ista

Jennifer Krauel

unread,
Feb 14, 2013, 2:57:41 PM2/14/13
to ggp...@googlegroups.com, Jennifer Krauel
Woo hoo, awesome, it worked.  Thanks so much!
Reply all
Reply to author
Forward
0 new messages