简体   繁体   English

ggplot2 自定义 grob 不会延伸到 plot 之外

[英]ggplot2 custom grob will not extend outside of plot

I am trying to use annotate_custom to create lines outside of my plot to divide my axis into sections.我正在尝试使用annotate_custom在我的 plot 之外创建线条,以将我的轴分成几部分。 I am aware of this post that asks a similar question, but for some reason, using a negative value as the min value for the line does not extend the lines outside of the plot.我知道这篇文章提出了类似的问题,但由于某种原因,使用负值作为线的最小值不会将线延伸到 plot 之外。

Sample code:示例代码:

library(ggplot2)
library(grid)

data("iris")
ggplot(iris, aes(x=Species, y=Petal.Width)) +
  geom_bar(stat='identity')+coord_flip()+
  annotation_custom(grob = linesGrob(), xmin = 2.5, xmax = 2.5, ymin = -90, ymax = 0)+
  annotation_custom(grob = linesGrob(), xmin = 1.5, xmax = 1.5, ymin = -90, ymax = 0)

What I get:我得到什么: 在此处输入图像描述

What I want:我想要的是: 在此处输入图像描述

Be default, the setting is to not allow any graphical elements to clip outside the plot area.默认情况下,设置是不允许任何图形元素剪辑到 plot 区域之外。 You can turn off clipping via any of the coord_* functions (ex. coord_cartesian() , coord_fixed() ...), so in your case, use coord_flip(clip="off") to allow grobs to extend anywhere in your plot:您可以通过任何coord_*函数(例如coord_cartesian()coord_fixed() ...)关闭剪辑,因此在您的情况下,使用coord_flip(clip="off")允许 grobs 扩展到 plot 中的任何位置:

ggplot(iris, aes(x=Species, y=Petal.Width)) +
  geom_bar(stat='identity')+coord_flip(clip='off')+
  annotation_custom(grob = linesGrob(), xmin = 2.5, xmax = 2.5, ymin = -90, ymax = 0)+
  annotation_custom(grob = linesGrob(), xmin = 1.5, xmax = 1.5, ymin = -90, ymax = 0)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM