我需要以某種方式註釋我的圖。繼答案here,我能想出這樣,用指定顏色繪製區域外的形狀進行註釋
df = data.frame(y= rep(c(1:20, 1:10), 5), x=c(rep("A", 20), rep("B", 10), rep("C", 20), rep("D", 10), rep("E", 20),
rep("F", 10), rep("G", 20), rep("H", 10), rep("I", 20), rep("J", 10)),
g= c(rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),
rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),
rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10)))
p <- ggplot(df, aes(factor(x), y)) + geom_boxplot()+ # Base plot
theme(plot.margin = unit(c(3,1,1,1), "lines"), plot.background= element_rect(color= "transparent")) # Make room for the grob
for (i in 1:length(df$g)) {
p <- p + annotation_custom(
grob = textGrob(label = df$g[i], hjust = 0, gp = gpar(cex = 1.5)),
xmin = df$x[i], # Vertical position of the textGrob
xmax = df$x[i],
ymin = 22, # Note: The grobs are positioned outside the plot area
ymax = 22)
}
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
這會產生該地塊。
我想在註釋和藍色的「+」號,而不是1.尺寸0.6磅,而不是2藍色三角形能否請你幫我在這裏?
謝謝!正是我想要的。 :) –