80

我想将样本大小值与绘图上的点相关联。我可以geom_text用来定位点附近的数字,但这很混乱。沿着情节的外边缘排列它们会更干净。

例如,我有:

df=data.frame(y=c("cat1","cat2","cat3"),x=c(12,10,14),n=c(5,15,20))

ggplot(df,aes(x=x,y=y,label=n))+geom_point()+geom_text(size=8,hjust=-0.5)

这产生了这个情节: 在此处输入图像描述

我更喜欢这样的东西: 在此处输入图像描述

我知道我可以创建第二个绘图并使用grid.arrange(a la this post),但是确定 textGrobs 的间距以与 y 轴对齐会很乏味。有没有更简单的方法来做到这一点?谢谢!

4

3 回答 3

80

这现在在 ggplot2 3.0.0 中很简单,因为现在可以通过使用clip = 'off'坐标函数中的参数来禁用裁剪,例如coord_cartesian(clip = 'off')or coord_fixed(clip = 'off')。下面是一个例子。

    # Generate data
    df <- data.frame(y=c("cat1","cat2","cat3"),
                     x=c(12,10,14),
                     n=c(5,15,20))

    # Create the plot
    ggplot(df,aes(x=x,y=y,label=n)) +
      geom_point()+
      geom_text(x = 14.25, # Set the position of the text to always be at '14.25'
                hjust = 0,
                size = 8) +
      coord_cartesian(xlim = c(10, 14), # This focuses the x-axis on the range of interest
                      clip = 'off') +   # This keeps the labels from disappearing
      theme(plot.margin = unit(c(1,3,1,1), "lines")) # This widens the right margin

在此处输入图像描述

于 2018-07-12T18:52:07.260 回答
63

您不需要绘制第二个图。您可以使用annotation_custom在绘图区域内部或外部的任何位置定位 grobs。grobs 的定位是根据数据坐标进行的。假设“5”、“10”、“15”与“cat1”、“cat2”、“cat3”对齐,则需要注意 textGrobs 的垂直定位 - 您的三个 textGrob 的 y 坐标由三个数据点的 y 坐标。默认情况下,ggplot2剪辑会抓取到绘图区域,但可以覆盖剪辑。相关边际需要扩大,以便为 grob 腾出空间。以下(使用 ggplot2 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 grob

for (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 clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

在此处输入图像描述

于 2012-09-14T02:48:49.323 回答
5

基于更简单的解决方案grid

require(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"))

p

grid.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"))
于 2017-09-05T12:08:16.917 回答