LEATH
你不需要再画一个情节。你可以用annotation_custom将Grobs定位在绘图区域内或之外的任何位置。Grobs的定位是根据数据坐标进行的。假设“5”、“10”、“15”与“cat 1”、“cat 2”、“cat 3”对齐,TextGrobs的垂直定位将由三个数据点的y坐标给出。默认情况下,ggplot2剪辑格罗布到绘图区域,但裁剪可以被覆盖。需要扩大有关的差额,以便为格罗布会议腾出空间。下面(使用ggplot 2 0.9.2)给出了与第二幅图类似的图:library (ggplot2)library(grid)df=data.frame(y=c("cat1","cat2","cat3"),x=c(12,10,14),n=c(5,15,20))p <- ggplot(df, aes(x,y)) + geom_point() +
# Base plot
theme(plot.margin = unit(c(1,3,1,1), "lines")) # Make room for the grobfor (i in 1:length(df$n)) {p <- p + annotation_custom(
grob = textGrob(label = df$n[i], hjust = 0, gp = gpar(cex = 1.5)),
ymin = df$y[i], # Vertical position of the textGrob
ymax = df$y[i],
xmin = 14.3, # Note: The grobs are positioned outside the plot area
xmax = 14.3)
} # Code to override clippinggt <- ggplot_gtable(ggplot_build(p))gt$layout$clip[gt$layout$name == "panel"] <- "off"grid.draw(gt)
白猪掌柜的
基于gridrequire(grid)df = data.frame(y = c("cat1", "cat2", "cat3"), x = c(12, 10, 14), n = c(5, 15, 20))p <- ggplot(df, aes(x, y)) + geom_point() + # Base plottheme(plot.margin = unit(c(1, 3, 1, 1), "lines"))pgrid.text("20", x = unit(0.91, "npc"), y = unit(0.80, "npc"))grid.text("15", x = unit(0.91, "npc"), y = unit(0.56, "npc"))grid.text("5", x = unit(0.91, "npc"), y = unit(0.31, "npc"))