我有一个df
带有曲线信息:
df <- data.frame(
x = c(0,0,1,1),
xend = c(0,1,1,0),
y = c(0,1,0,1),
yend = c(1,0,1,1),
curvature = c(-.2,-.5,.1,1)
)
我可以用单独的曲线绘制这些曲线curvature
论据(想法来自here https://stackoverflow.com/questions/55627528/how-can-i-pass-individual-curvature-arguments-in-ggplot2-geom-curve-functi):
library(ggplot2)
ggplot(df) +
lapply(split(df, 1:nrow(df)), function(dat) {
geom_curve(data = dat, aes(x = x, y = y, xend = xend, yend = yend), curvature = dat["curvature"]) }
) + xlim(-1,2) + ylim(-1,2) + theme_void()
现在我想用相同的曲线重叠绘制该图像,但每条曲线应在开头和结尾处剪切约 10%。
首先,我想我也许可以使用我的信息gg
物体但看不到在哪里ggplot2
存储信息(另请参阅我的问题here https://stackoverflow.com/questions/55686910/how-can-i-access-dimensions-of-labels-plotted-by-geom-text-in-ggplot2).
然后我尝试使用以下方法重新调整起点和终点:
offset <- function(from, to) return((to - from)/10)
recalculate_points <- function(df) {
df$x <- df$x + offset(df$x, df$xend)
df$xend = df$xend - offset(df$x, df$xend)
df$y = df$y + offset(df$y, df$yend)
df$yend = df$yend - offset(df$y, df$yend)
return(df)
}
df2 <- recalculate_points(df)
ggplot(df) +
lapply(split(df, 1:nrow(df)), function(dat) {
geom_curve(data = dat, aes(x = x, y = y, xend = xend, yend = yend), curvature = dat["curvature"]) }
) +
lapply(split(df2, 1:nrow(df2)), function(dat) {
geom_curve(data = dat, aes(x = x, y = y, xend = xend, yend = yend), curvature = dat["curvature"], color = "red") }
) + xlim(-1,2) + ylim(-1,2) + theme_void()
像这样我可以切割曲线的起点和终点。但正如我们所看到的red曲线与原始曲线不相符black那些很好。
我怎样才能提高我的offset
and recalculate_points
函数为了red曲线拟合black curves better?
或者甚至更好:我在哪里可以找到曲线信息gg
对象以及如何使用该信息来重新调整我的曲线?
注意:我不需要 100% 适合。但视觉上的贴合度应该有所改善。所以我的预期输出应该类似于: