在用geom_abline创建的线之间绘制功能区

时间:2018-10-22 14:55:05

标签: r ggplot2

我试图在用geom_abline创建的行之间创建阴影区域

require(ggplot2)

val_intcpt <- c(-1,1)

ggplot() + 
  geom_point(data = iris, mapping = aes(x = Petal.Length, y = Sepal.Width)) +
  geom_abline(intercept = 0, slope = 1, linetype = 'dashed') +
  geom_abline(intercept = val_intcpt, slope = 1, linetype = 'dotted') 

enter image description here

该想法是将虚线之间的区域阴影化。

  • geom_ribbon不起作用,因为它需要ymin/ymax并且我没有此信息(当然,我可以对数据帧进行硬编码,但这并不是一个很好的解决方案,因为它不适用于任何给定的数据。)
  • 使用ggplot_build无济于事,因为数据框不提供x / y数据。

我确定我缺少一些非常明显的东西:(

1 个答案:

答案 0 :(得分:3)

也许绘制一个多边形?

# let ss be the slope for geom_abline
ss <- 1

p <- ggplot() + 
  geom_point(data = iris, mapping = aes(x = Petal.Length, y = Sepal.Width)) +
  geom_abline(intercept = 0, slope = ss, linetype = 'dashed') +
  geom_abline(intercept = val_intcpt, slope = ss, linetype = 'dotted') 

# get plot limits
p.x <- layer_scales(p)$x$get_limits()
p.y <- layer_scales(p)$y$get_limits()

# create polygon coordinates, setting x positions somewhere
# beyond the current plot limits
df <- data.frame(
  x = rep(c(p.x[1] - (p.x[2] - p.x[1]),
            p.x[2] + (p.x[2] - p.x[1])), each = 2),
  intcpt = c(val_intcpt, rev(val_intcpt))
) %>%
  mutate(y = intcpt + ss * x)

# add polygon layer, & constrain to previous plot limits
p +
  annotate(geom = "polygon",
           x = df$x,
           y = df$y,
           alpha = 0.2) +
  coord_cartesian(xlim = p.x, ylim = p.y)

plot

工作原理的解释

让我们考虑一个正常情节:

ss <- 0.75 # this doubles up as illustration for different slope values

p <- ggplot() +
  geom_point(data = iris, aes(x = Petal.Length, y = Sepal.Width), color = "grey75") +
  geom_abline(intercept = val_intcpt, slope = ss, linetype = 'dashed', 
              color = c("blue", "red"), size = 1) +
  annotate(geom = "text", x = c(6, 3), y = c(2.3, 4), color = c("blue", "red"), size = 4,
           label = c("y == a[1] + b*x", "y == a[2] + b*x"), parse = TRUE)
  coord_fixed(ratio = 1.5) +
  theme_classic()

p + ggtitle("Step 0: Construct plot")

step 0

p.x获取限制p.y / p,并查看图中本身的对应位置(紫色):

p.x <- layer_scales(p)$x$get_limits()
p.y <- layer_scales(p)$y$get_limits()

p1 <- p + 
  geom_point(data = data.frame(x = p.x, y = p.y) %>% tidyr::complete(x, y),
             aes(x = x, y = y), 
             size = 2, stroke = 1, color = "purple")

p1 + ggtitle("Step 1: Get plot limits")

step 1

记下x轴极限值(仍为紫色):

p2 <- p1 +
  annotate(geom = "text", x = p.x, y = min(p.y), label = c("x[0]", "x[1]"), 
           vjust = -1, parse = TRUE, color = "purple", size = 4)

p2 + 
  ggtitle("Step 2: Note x-axis coordinates of limits") +
  annotate(geom = "segment", 
           x = p.x[1] + diff(p.x), 
           xend = p.x[2] - diff(p.x), 
           y = min(p.y), yend = min(p.y),
           color = "purple", linetype = "dashed", size = 1,
           arrow = arrow(ends = "both")) +
  annotate(geom = "text", x = mean(p.x), y = min(p.y), label = "x[1] - x[0]",
           vjust = -1, parse = TRUE, color = "purple", size = 4)

step 2

我们要构造一个多边形(准确地说是平行四边形),其角远超出原始图的范围,因此在图中不可见。实现此目的的一种方法是采用现有图的x轴限制并将其向外偏移与现有图的x轴范围相同的量:生成的位置(黑色)非常远:

p3 <- p2 +
  annotate(geom = "point", 
           x = c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)), y = min(p.y),
           shape = 4, size = 1, stroke = 2) +
  annotate(geom = "text", 
           x = c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)), y = min(p.y),
           label = c("x[0] - (x[1] - x[0])", "x[1] + (x[1] - x[0])"),
           vjust = -1, parse = TRUE, size = 5, hjust = c(0, 1))

p3 +
  ggtitle("Calculate x-axis coordinates of two points far beyond the limits") +
  annotate(geom = "segment", 
           x = p.x, 
           xend = p.x + c(-diff(p.x), diff(p.x)), 
           y = min(p.y), yend = min(p.y),
           linetype = "dashed", size = 0.5,
           arrow = arrow(ends = "both", length = unit(0.1, "inches"))) 

step 3

我们可以使用标准geom_abline公式,为每个y = a + b * x(以红色/蓝色表示)得出与x轴位置相关的相应y值:

p4 <- p3 + 
  annotate(geom = "point",
           x = c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)),
           y = val_intcpt[2] + ss * c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)),
           shape = 8, size = 2, stroke = 2, col = "red") + 
  annotate(geom = "point",
           x = c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)),
           y = val_intcpt[1] + ss * c(p.x[1] - diff(p.x), p.x[2] + diff(p.x)),
           shape = 8, size = 2, stroke = 2, col = "blue")

p4 +
  ggtitle("Calculate the corresponding y coordinates for both ab-lines") +
  annotate(geom = "text",
           x = p.x[1] - diff(p.x),
           y = val_intcpt + ss * (p.x[1] - diff(p.x)),
           label = c("y == a[1] + b * (x[0] - (x[1] - x[0]))", 
                     "y == a[2] + b * (x[0] - (x[1] - x[0]))"), 
           hjust = -0.2, parse = TRUE, 
           color = c("blue", "red")) +
  annotate(geom = "text",
           x = p.x[2] + diff(p.x),
           y = val_intcpt + ss * (p.x[2] + diff(p.x)),
           label = c("y == a[1] + b * (x[1] + (x[1] - x[0]))", 
                     "y == a[2] + b * (x[1] + (x[1] - x[0]))"), 
           hjust = 1.2, parse = TRUE, 
           color = c("blue", "red"))

step 4

现在我们有了角的x / y坐标,构造多边形是将它们连接在一起的简单问题:

p5 <- p4 +
  annotate(geom = "polygon",
           x = rep(c(p.x[1] - diff(p.x),
                     p.x[2] + diff(p.x)),
                   each = 2),
           y = c(val_intcpt + ss * (p.x[1] - diff(p.x)),
                 rev(val_intcpt) + ss * (p.x[2] + diff(p.x))),
           fill = "yellow", alpha = 0.4)

p5 +
  ggtitle("Step 5: Draw polygon based on calculated coordinates") +
  annotate(geom = "label",
           x = rep(c(p.x[1] - diff(p.x),
                     p.x[2] + diff(p.x)),
                   each = 2),
           y = c(val_intcpt + ss * (p.x[1] - diff(p.x)),
                 rev(val_intcpt) + ss * (p.x[2] + diff(p.x))),
           label = c("list(x[0] - (x[1] - x[0]), a[1] + b*(x[0] - (x[1] - x[0])))",
                     "list(x[0] - (x[1] - x[0]), a[2] + b*(x[0] - (x[1] - x[0])))",
                     "list(x[1] + (x[1] - x[0]), a[2] + b*(x[1] + (x[1] - x[0])))",
                     "list(x[1] + (x[1] - x[0]), a[1] + b*(x[1] + (x[1] - x[0])))"),
           parse = TRUE, hjust = rep(c(0, 1), each = 2))

step 5

应用原始绘图范围,并且我们有一个假装为填充色带的多边形,其中的边角安全地隐藏在视线之外:

p5 +
  ggtitle("Step 6: Reset plot range to original range") +
  coord_fixed(ratio = 1.5, xlim = p.x, ylim = p.y) 

step 6

(注意:这里有很多不必要的代码,用于标记和着色中间步骤以用于说明目的。对于实际使用,按照我的原始解决方案,这些都没有必要。但是就解释而言,这就是或素描并扫描我cr脚的笔迹...)