R 在使用geom_abline创建的线之间打印功能区
我试图在使用R 在使用geom_abline创建的线之间打印功能区,r,ggplot2,R,Ggplot2,我试图在使用geom\u 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_int
geom\u 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')
require(ggplot2)
val_intcpt也许可以绘制一个多边形
# 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)
注意x轴限制的值(仍为紫色):
现在我们有了角点的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))
(注意:这里有很多不必要的代码,用于为中间步骤添加标签和颜色,以便于演示。对于实际使用,按照我最初的解决方案,这些都不是必需的。但就解释而言,要么是这个,要么是我蹩脚的笔迹中的草图+扫描…)是的,这是一个很好的破解:)我不知道图层缩放
。再一次,我从你身上学到了很多:)我会等一小会儿再接受答案,不过,可能还有其他建议。解决方案对我的示例(以及我的要求)很好,但不适用于不同的坡度。你是建议编辑这个问题,使之更一般,还是提出一个新问题?很高兴两者都做(对不起,我觉得很愚蠢)。我承认我根本不理解你的解决方案为什么有效。为什么使用绘图限制的坐标会产生正确的多边形??那超出了我的想象ken@Tjebo我刚看到你的问题&现在我感觉很糟糕将更新我的答案以合并不同的坡度。哇!!非常感谢。需要在周末花一点时间一步一步地了解它:)
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")
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)
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")))
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"))
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))
p5 +
ggtitle("Step 6: Reset plot range to original range") +
coord_fixed(ratio = 1.5, xlim = p.x, ylim = p.y)