首页 > 解决方案 > 注释矩形以在两个因素之间开始

问题描述

这是我的例子,从这个问题获得:

ex <- data.frame(a = factor(letters[1:10]),
                 b = factor(rep(c('b', 'a'), 5)),
                 c = rep(letters[1:5], 2))

ex$a <- factor(ex$a, levels = ex$a[order(ex$b)])

当ggploting...

library(ggplot2)
ggplot(data = ex, aes(y = a, x = c)) +
    geom_point() +
    annotate(xmin = 'b', xmax = 'd', ymin = -Inf, ymax = Inf, geom = 'rect', alpha = 0.2)

...我们得到这个情节: 在此处输入图像描述

但是,灰色矩形如何在 和 之间开始 a 在** 和之间b 结束de

标签: rggplot2

解决方案


照原样 and is你必须a设置is (即,在and之间),然后对and做同样的事情。1b2xmid = 1.5abde

ggplot(ex, aes(c, a)) +
    geom_point() +
    annotate(xmin = 1.5, xmax = 4.5, 
             ymin = -Inf, ymax = Inf, 
             geom = "rect", alpha = 0.2)

在此处输入图像描述


推荐阅读