【发布时间】:2019-12-05 08:12:03
【问题描述】:
我会很快告诉你这个问题是如何产生的。
我想在 ggplot 的轴之外绘制一些东西。即,围绕 x 轴刻度线标签的多边形。我找到了一种方法,方法是使用 annotation_custom() 和 grid::polygonGrob() 函数并关闭剪辑。我想我的问题是我对这些对象还不太熟悉。我所知道的是,在我的操作之后,我得到了一个 grob 对象。这样就可以了,但我想使用cowplot 将其包含在绘图网格中。
这是我的代码:
# Required packages
library(ggplot2)
library(grid)
require(cowplot)
# dput(data_B)
data_B <- structure(list(n = c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L), sample = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("sample_1",
"sample_2"), class = "factor"), fraction = c(0.01, 0.05, 0.24,
0.45, 0.19, 0.045, 0.01, 0.005, 0.005, 0.04, 0.225, 0.53, 0.14,
0.04, 0.02, 0), err = c(0.002, 0.01, 0.04, 0.06, 0.034, 0.009,
0.002, 0.001, 0.001, 0.008, 0.04, 0.05, 0.03, 0.008, 0.004, 0
)), class = "data.frame", row.names = c(NA, -16L))
# The plot
panel_B <- ggplot(data=data_B,(aes(x=n,y=fraction,fill=sample)))+
geom_bar(stat = "identity",position=position_dodge()) +
geom_errorbar(aes(ymin=fraction-err, ymax=fraction+err),
width=.2, position=position_dodge(.9)) +
scale_fill_manual(values=c('#f2a340','#998fc2'))+
scale_x_continuous(expand = c(0, 0),
breaks=c(seq(min(data_B$n),max(data_B$n),by = 1)),
limits = c(min(data_B$n),max(data_B$n))) +
scale_y_continuous(expand = c(0, 0),
breaks=c(seq(0,0.6,by=0.1)),
limits = c(0,0.6)
)
# Add polygons around tick mark labels (unfinished work)
panel_B <- panel_B +
annotation_custom(polygonGrob(x = c(-0.03,0.03,0),y = c(-0.09,-0.09,-0.02),
gp=gpar(col="black",fill=NA, lwd=3))
) +
annotation_custom(polygonGrob(x = c(0.12,0.17,0.17,0.12),y = c(-0.09,-0.09,-0.03,-0.03),
gp=gpar(col="black",fill=NA, lwd=2)))
# Turn off panel clipping
gt <- ggplot_gtable(ggplot_build(panel_B))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
panel_B <- grid.draw(gt)
# And a dummy version of a cowplot grid
plot_grid(c(),panel_B,nrow=2)
另外,我的问题是绘制的多边形不是固定的,但是当情节比例改变时往往会错位。也许有一个完全不同的方法。为任何 cmets 感到高兴。
【问题讨论】: