5

I have the following data,

ex <-  structure(list(stand = structure(c(4L, 4L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 5L, 5L, 5L), .Label = c("LC", "BH", "CS","BC", "MT"), class = "factor"),species = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Tree 1","Tree 2", "Tree 3"), class = "factor"), dead = c(134.8314607,71.16104869, 101.1235955, 666.6666667, 153.5580524, 0, 430.7116105, 210.6741573, 0, 44.94382022, 202.247191, 71.16104869, 597.3782772,125.4681648, 28.08988764), dead.se = c(56.89147726, 21.37751711,24.46934619, 94.10878505, 22.71697636, 0, 79.89037082, 56.06261191,0, 19.89272748, 39.9705907, 23.35791951, 74.23685618, 43.83572217, 12.41438416), live = c(0, 74.90636704, 528.0898876, 217.2284644,546.8164794, 0, 154.494382, 421.3483146, 9.36329588, 3.745318352,71.16104869, 168.5393258, 48.68913858, 331.4606742, 95.50561798), live.se = c(0, 48.90851124, 70.74483778, 62.88285082, 78.20454313, 0, 47.11205074, 102.9962547, 6.396348793, 3.745318352, 19.19385506, 45.74972309, 14.65745038, 61.91710544, 34.09712382), posthoc.l = structure(c(1L,3L, 6L, 3L, 1L, 1L, 3L, 1L, 2L, 1L, 5L, 5L, 3L, 1L, 4L), .Label = c("a", "a,b", "b", "b,c", "c", "d"), class = "factor"), posthoc.d = structure(c(1L, 1L, 1L, 3L, 1L, 5L, 3L, 1L, 6L, 1L, 1L, 2L, 3L, 1L, 4L), .Label = c("a","a,b", "b", "b,c,d", "c", "d"), class = "factor")), .Names = c("stand", "species", "dead", "dead.se", "live", "live.se", "posthoc.l", "posthoc.d"), row.names = c(NA, 15L), class = "data.frame")   

and I am making the following graphs

ex$stand <- as.factor(ex$stand)
ex$species <- as.factor(ex$species)
ex$stand <- factor(ex$stand, levels = c("LC", "BH", "CS", "BC", "MT"))
ex <- na.exclude(ex)
ex$posthoc.l <- as.factor(ex$posthoc.l)
ex$posthoc.d <- as.factor(ex$posthoc.d)



live <- ggplot(ex, aes(x = species, y = live, fill = species)) +  
  geom_bar(position = position_dodge(), stat="identity") + 
  geom_errorbar(aes(ymin=live-live.se, ymax=live+live.se)) +
  ylab("Live") +
  geom_text(aes(label = paste(posthoc.l), sep=""), vjust=-7) +  
  scale_fill_grey(start = .1, end = .9) +
  theme_bw() +
  theme(panel.grid.major = element_blank()) +
  theme(strip.background = element_rect(fill="white")) +
  theme(strip.text.x = element_text(face="bold")) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.ticks.x = element_blank()) +
  facet_grid( ~ stand)

dead <- ggplot(ex, aes(x = species, y = dead, fill = species)) +  
  geom_bar(position = position_dodge(), stat="identity") + 
  geom_errorbar(aes(ymin=dead-dead.se, ymax=dead+dead.se)) +
  xlab("Species") +
  ylab("Dead") +
  geom_text(aes(label = paste(posthoc.d), sep=""), vjust=-6) +  
  scale_fill_grey(start = .1, end = .9) +
  theme_bw() +
  theme(panel.grid.major = element_blank()) +
  theme(strip.background = element_rect(fill="white")) +
  theme(strip.text.x = element_text(face="bold")) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.ticks.x = element_blank()) +
  facet_grid( ~ stand)

gp.live <- ggplot_gtable(ggplot_build(live))
gp.dead <- ggplot_gtable(ggplot_build(dead))
maxWidth = unit.pmax(gp.live$widths[2:3], gp.dead$widths[2:3])
gp.live$widths[2:3] <- maxWidth
gp.dead$widths[2:3] <- maxWidth
legend = gtable_filter(ggplot_gtable(ggplot_build(live)), "guide-box") 
grid.arrange(arrangeGrob(live + theme(legend.position="none"), 
                     dead + theme(legend.position="none"),
                     left = textGrob("Trees per Hectare", rot = 90, vjust = 1)),
         sub=textGrob("Species", vjust=-.3, hjust=1.1),
         legend, 
         widths=unit.c(unit(1, "npc") - legend$width, legend$width), 
         nrow=1)

however, not only does the vjust with labels look kind of tacky, when I call the grid.arrange, I lose the labels. Can anybody help me keep my labels in the grid.arrange? And also maybe clean up the look of the labels (place them just above the error bars for each bar?)

Thank you in advance

final product

tonytonov
  • 25,060
  • 16
  • 82
  • 98
user2325155
  • 147
  • 2
  • 10
  • What if you try `geom_text(aes(y=dead + dead.se, ...` plus maybe a little spacer? – Midnighter Apr 03 '14 at 18:47
  • Thanks Midnighter, my labels are from the "posthoc" column in the data.frame. I added a figure of the final product. Notice how the label is cut off in a couple of the large valued bars. – user2325155 Apr 03 '14 at 18:55
  • Either reduce the spacer or add `ylim(0,1000)` (or whatever) to make the facets cover enough y space to include the label – user2034412 Apr 04 '14 at 00:01
  • possible duplicate of [ggplot2 bar plot, no space between bottom of geom and x axis keep space above](http://stackoverflow.com/questions/20220424/ggplot2-bar-plot-no-space-between-bottom-of-geom-and-x-axis-keep-space-above) – baptiste Apr 04 '14 at 11:01
  • I understand that ylim works, but what I am really trying to do is make each label relative to its own bar, this way I don't have 300 units of empty space and labels floating mysteriously above bars. – user2325155 Apr 04 '14 at 18:21

1 Answers1

6

It seems like the y aesthetic should be set within geom_text, I used the value of live and dead respectively plus 1.8 times SE. Also ylim, so it doesn't roll of the frame.

ex <-  structure(list(stand = structure(c(4L, 4L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 5L, 5L, 5L), .Label = c("LC", "BH", "CS","BC", "MT"), class = "factor"),species = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Tree 1","Tree 2", "Tree 3"), class = "factor"), dead = c(134.8314607,71.16104869, 101.1235955, 666.6666667, 153.5580524, 0, 430.7116105, 210.6741573, 0, 44.94382022, 202.247191, 71.16104869, 597.3782772,125.4681648, 28.08988764), dead.se = c(56.89147726, 21.37751711,24.46934619, 94.10878505, 22.71697636, 0, 79.89037082, 56.06261191,0, 19.89272748, 39.9705907, 23.35791951, 74.23685618, 43.83572217, 12.41438416), live = c(0, 74.90636704, 528.0898876, 217.2284644,546.8164794, 0, 154.494382, 421.3483146, 9.36329588, 3.745318352,71.16104869, 168.5393258, 48.68913858, 331.4606742, 95.50561798), live.se = c(0, 48.90851124, 70.74483778, 62.88285082, 78.20454313, 0, 47.11205074, 102.9962547, 6.396348793, 3.745318352, 19.19385506, 45.74972309, 14.65745038, 61.91710544, 34.09712382), posthoc.l = structure(c(1L,3L, 6L, 3L, 1L, 1L, 3L, 1L, 2L, 1L, 5L, 5L, 3L, 1L, 4L), .Label = c("a", "a,b", "b", "b,c", "c", "d"), class = "factor"), posthoc.d = structure(c(1L, 1L, 1L, 3L, 1L, 5L, 3L, 1L, 6L, 1L, 1L, 2L, 3L, 1L, 4L), .Label = c("a","a,b", "b", "b,c,d", "c", "d"), class = "factor")), .Names = c("stand", "species", "dead", "dead.se", "live", "live.se", "posthoc.l", "posthoc.d"), row.names = c(NA, 15L), class = "data.frame")   

ex$stand <- as.factor(ex$stand)
ex$species <- as.factor(ex$species)
ex$stand <- factor(ex$stand, levels = c("LC", "BH", "CS", "BC", "MT"))
ex <- na.exclude(ex)
ex$posthoc.l <- as.factor(ex$posthoc.l)
ex$posthoc.d <- as.factor(ex$posthoc.d)

live <- ggplot(ex, aes(x = species, y = live, fill = species)) +  
  geom_bar(position = position_dodge(), stat="identity") + 
  geom_errorbar(aes(ymin=live-live.se, ymax=live+live.se)) +
  ylab("Live") +
  scale_fill_grey(start = .1, end = .9) +
  theme_bw() +
  theme(panel.grid.major = element_blank()) +
  theme(strip.background = element_rect(fill="white")) +
  theme(strip.text.x = element_text(face="bold")) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.ticks.x = element_blank()) +
  facet_grid( ~ stand) + 
  geom_text(aes(label=posthoc.l, y=live+(live.se/1.8)), vjust=-1.5) +
  ylim(0, 700)

dead <- ggplot(ex, aes(x = species, y = dead, fill = species)) +  
  geom_bar(position = position_dodge(), stat="identity") + 
  geom_errorbar(aes(ymin=dead-dead.se, ymax=dead+dead.se)) +
  xlab("Species") +
  ylab("Dead") +
  scale_fill_grey(start = .1, end = .9) +
  theme_bw() +
  theme(panel.grid.major = element_blank()) +
  theme(strip.background = element_rect(fill="white")) +
  theme(strip.text.x = element_text(face="bold")) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.ticks.x = element_blank()) +
  facet_grid( ~ stand) + 
  geom_text(aes(label=posthoc.d, y=dead+(dead.se/1.8)), vjust=-1.5) +
  ylim(0, 850)

gp.live <- ggplot_gtable(ggplot_build(live))
gp.dead <- ggplot_gtable(ggplot_build(dead))
maxWidth = unit.pmax(gp.live$widths[2:3], gp.dead$widths[2:3])
gp.live$widths[2:3] <- maxWidth
gp.dead$widths[2:3] <- maxWidth
legend = gtable_filter(ggplot_gtable(ggplot_build(live)), "guide-box") 

grid.arrange(arrangeGrob(live + theme(legend.position="none"), 
                         dead + theme(legend.position="none"),
                         left = textGrob("Trees per Hectare", rot = 90, vjust = 1)),
             sub=textGrob("Species", vjust=-.3, hjust=1.1),
             legend, 
             widths=unit.c(unit(1, "npc") - legend$width, legend$width), 
             nrow=1)

Is this your desired result?

enter image description here

jakub
  • 4,774
  • 4
  • 29
  • 46