你可以编写你自己的geom,就像他们在另一个问题中所做的那样.这是另一个可能的解决方案
geom_ray <- function(mapping = NULL, data = NULL,
...,
na.rm = FALSE,
show.legend = NA) {
layer(
data = data,
mapping = mapping,
stat = StatIdentity,
geom = GeomRay,
position = PositionIdentity,
show.legend = show.legend,
inherit.aes = FALSE,
params = rlang::list2(
na.rm = na.rm,
...
)
)
}
GeomRay <- ggproto("GeomRay", Geom,
draw_panel = function(data, panel_params, coord, lineend = "butt") {
ranges <- coord$backtransform_range(panel_params)
if (coord$clip == "on" && coord$is_linear()) {
# Ensure the line extends well outside the panel to avoid visible line
# ending for thick lines
ranges$x <- ranges$x + c(-1, 1) * diff(ranges$x)
}
slope <- (data$towardy - data$y)/(data$towardx - data$x)
intercept <- data$y - data$x*slope
data$x <- data$x
data$y <- data$y
data$xend <- ifelse(data$towardx > data$x,
ranges$x[2],
ranges$x[1]
)
data$yend <- ifelse(data$towardx > data$x,
ranges$x[2] * slope + intercept,
ranges$x[1] * slope + intercept
)
GeomSegment$draw_panel(ggplot2:::unique0(data), panel_params, coord, lineend = lineend)
},
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
required_aes = c("x", "y", "towardx", "towardy"),
draw_key = draw_key_abline,
rename_size = TRUE,
check_constant_aes = FALSE
)
这个代码基本上直接从ggplot::geom_abline
代码改编而来.
在此设置中,x
和y
是希望光线开始的位置,然后towardx
和towardy
是希望光线指向的方向.因此,对于您的数据,您可以这样使用它
ggplot(data = dat, aes(X,Y)) +
geom_point() +
geom_point(data = centroid, aes(X, Y), col = "red") +
geom_ray(x = centroid$X, y = centroid$Y,
aes(towardx = X, towardy = Y))
which produces