gpt4 book ai didi

r - 如何在 `curvature` `ggplot2` 函数中传递单个 `geom_curve` 参数?

转载 作者:行者123 更新时间:2023-12-04 11:57:24 42 4
gpt4 key购买 nike

我有一个 df有两个曲线定义,每个定义由两个点和一个曲率值组成。目标是使用 ggplot2 绘制两条单独的曲线。 geom_curve (或替代方案)。

我可以使用以下方法生成我的预期输出:

df <- data.frame(x = c(0,.2), y = c(0,.3), xend = c(1,.4), yend = c(1,.6), curvature = c(-.2,.4))
ggplot(df) + geom_curve(data = df[1, ], aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature[1]) + geom_curve(data = df[2, ], aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature[2])

enter image description here

但这并不是真正的解决方案,因为在我的真实情况下,我有更多的曲线(而且我不知道提前多少)。

个人如何通过curvature geom_curve 的论据称呼?

我试过:
df <- data.frame(x = c(0,0), y = c(0,0), xend = c(1,1), yend = c(1,1), curvature = c(-.2,.8))
library(ggplot2)
ggplot(df) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend, curvature = curvature))

这将两条曲线相互叠加并引发附加警告:

Warning: Ignoring unknown aesthetics: curvature



所以我试过:
ggplot(df) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = curvature)

这会引发错误:

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomCurve, : object 'curvature' not found



所以我试图明确地传递 curvature冒号:
ggplot(df) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature)

这也会引发错误:

Error in seq.default(0, dir * maxtheta, dir * maxtheta/(ncp + 1)) :
'to' muss Länge 1 haben In addition: Warning messages: 1: In if (curvature == 0) { : the condition has length > 1 and only the first element will be used 2: In if (curvature > 0) hand <- "right" else hand <- "left" : the condition has length > 1 and only the first element will be used



我从@markus 的解决方案中了解到,我们可以通过 listsggplot对象,所以我试过:
ggplot(df) + 
lapply(df$curvature, function(i) {
geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = i) }
)

但这用 curvature 绘制了每条曲线争论:

enter image description here

我怎么能通过那个curvature为每一行单独论证?

最佳答案

更新
您可以先拆分数据,然后使用 lapply迭代我们将提供给 data 的结果列表geom_curve() 的论据

df2 <- data.frame(x = c(0,.2), y = c(0,.3), xend = c(1,.4), yend = c(1,.6), curvature = c(-.2,.4))
ggplot() +
lapply(split(df2, 1:nrow(df)), function(dat) {
geom_curve(data = dat, aes(x = x, y = y, xend = xend, yend = yend), curvature = dat["curvature"]) }
)
enter image description here
原答案 curvature正如您所指出的,这不是一种美学。您可以将列表添加到 ggplot() , 让它工作
df <- data.frame(x = c(0,0), y = c(0,0), xend = c(1,1), yend = c(1,1), curvature = c(-.2,.8))
ggplot(df) +
lapply(df$curvature, function(i) {
geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = i) }
)
enter image description here
来自 help("+.gg")

What can you add?

...

You can also supply a list, in which case each element of the list will be added in turn.



如果您想在绘图中显示其他参数 - 每条线的颜色可能不同,大小不同等 - 使用 Map修改数据
df1 <- data.frame(x = c(0,0), y = c(0,0), xend = c(1,1), yend = c(1,1), curvature = c(-.2,.8),
colour = c("red", "blue"))
阴谋
ggplot(df1) + 
Map(function(i, col) {
geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = i, colour = col) },
i = df1$curvature, col = df1$colour
)
结果
enter image description here

关于r - 如何在 `curvature` `ggplot2` 函数中传递单个 `geom_curve` 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55627528/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com