我有以下数据,
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") +
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)
然而,当我调用grid.arrange时,不仅带有标签的vjust看起来有点俗气,我丢失了标签。有人可以帮我把标签放在grid.arrange中吗?也可以清理标签的外观(将它们放在每个条的误差条上方?)
提前谢谢
答案 0 :(得分:3)
似乎y
美学应设置在geom_text
内,我分别使用live
和dead
的值加上1.8倍的SE。另外ylim
,所以它不会滚动框架。
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)
这是您想要的结果吗?