【问题标题】:ggplot2: Draw geom_segment() outside of the plot areaggplot2:在绘图区域外绘制 geom_segment()
【发布时间】:2023-03-22 16:29:01
【问题描述】:

我在 ggplot2 中创建了两个平面图。我想在绘图区域外添加一个箭头。多个问题试图解决这个问题: How to draw lines outside of plot area in ggplot2? Displaying text below the plot generated by ggplot2

但我无法让我的示例工作。另外,我希望有一个更简单的方法来完成这个?

我尝试增加plot.margins 并使用coord_cartesian(),但都没有帮助。

相反,我想要:

我的虚拟示例:

# read library to assess free data
library(reshape2)
library(ggplot2)


ggplot(tips,
       aes(x=total_bill,
           y=tip/total_bill)) + 
  geom_point(shape=1) +
  facet_grid(. ~ sex) +
  # define the segment outside the plot
  geom_segment(aes(x = 10, 
                   y = -0.25, 
                   xend = 10, 
                   yend = 0),
               col = "red",
               arrow = arrow(length = unit(0.3, "cm"))) +
  theme_bw() +
  # limit the displayed plot extent
  coord_cartesian(ylim = c(0, 0.75)) +
  # increase plot margins - does not help
  theme(plot.margin = unit(c(1,1,1,0), "lines"))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用coord_cartesian 中的参数clip="off" 在面板外绘制。我还使用scale_yexpand 参数将y 轴从零开始。 y 开始位置有一点手动选择。

    你的例子

    library(reshape2)
    library(ggplot2)
    
    ggplot(tips,
           aes(x=total_bill,
               y=tip/total_bill)) + 
      geom_point(shape=1) +
      facet_grid(. ~ sex) +
      annotate("segment", x=12, y=-0.05, xend=12, yend=0,
                   col="red", arrow=arrow(length=unit(0.3, "cm"))) +
      scale_y_continuous(expand=c(0,0))+
      theme_bw() +
      coord_cartesian(ylim = c(0, 0.75), clip="off") +
      theme(plot.margin = unit(c(1,1,1,0), "lines"))
    

    (我将geom_segment 更改为annotate,因为你没有映射美学)

    哪个产生

    【讨论】:

    • 感谢您的回答@user20650!拜托,你能在其中包括一个结果图吗?每个人都可以看到结果图,而无需在他们的 R 环境中重新运行代码。
    • coord_cartesian 正在将一些绘图点移到绘图区域之外。
    • 你是什么意思@HermanToothrot;是不是如果点就在轴边界上,在使用clip 之后,有些点在绘图面板之外?或
    • 绘图上部的点被截断了,我不得不设置ylim = c(0, 1)。我只是建议您的解决方案可能不适用于所有地块。
    • 嗨@HermanToothrot ;啊,对了。好的,是的,限制是特定于情节的
    【解决方案2】:

    这是一个不太令人满意的解决方法,因为您需要调整地块的位置。主要是使用自定义注解,可以使用cowplot

    library(ggplot2) 
    library(cowplot)
    library(reshape2)
    

    首先定义您的地块,使用相同的坐标限制。

    p <- 
      ggplot(tips, aes(x = total_bill, y = tip/total_bill)) + 
      geom_point(shape = 1) +
      facet_grid(. ~ sex) +
      theme_bw() +
      coord_cartesian(ylim = c(0, 0.75), xlim = c(0, 50)) 
    
    arrow_p <- 
      ggplot(tips) +
      geom_segment(aes(x = 1, y = -1, xend = 1, yend = 0),
                   col = "red",
                   arrow = arrow(length = unit(0.3, "cm"))) +
      coord_cartesian(ylim = c(0, 0.75), xlim = c(0,50)) +
      theme_void()
    

    arrow_p 在空白背景上创建,用作叠加层。您现在可以将其放置在您想要的位置:

     ggdraw() +
        draw_plot(p) +
        draw_plot(arrow_p, x = 0.10, y = 0.05) +
        draw_plot(arrow_p, x = 0.57, y = 0.05)
    

    不幸的是,您必须在这里反复试验。您还可以更改指定draw_plot() 的宽度/高度/比例参数。 可能有更好的方法,但这是一个相当直接的解决方法......

    【讨论】:

    • 感谢您的回答,这是非常有趣的方法!
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2015-11-10
    相关资源
    最近更新 更多