本文介绍了如何在ggplot2`geom_curve`函数中传递各个曲率参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a df with two curve definitions, each consists of two points and a curvature value. The goal is to plot two individual curves using ggplot2 geom_curve (or an alternative).

我可以使用以下命令生成期望的输出:

I can generate my expected output using:

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])

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

如何将单个curvature参数传递给geom_curve调用?

How can I pass an individual curvature argument to the geom_curve call?

我尝试过:

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))

这会使两条曲线相互重叠,并引发其他警告:

This plots both curves on top of each other and throws an additional warning:


所以我尝试了:


So I tried:

ggplot(df) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = curvature)

这会引发错误:


所以我试图显式传递curvature冒号:


So I tried to explicitly pass the curvature colon:

ggplot(df) + geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature)

这也会引发错误:


我从@markus的解决方案中学到,我们可以将lists传递给ggplot对象,所以我尝试了:


From @markus' solution I learned, that we can pass lists to a ggplot object, so I tried:

ggplot(df) + 
  lapply(df$curvature, function(i) {
    geom_curve(aes(x = x, y = y, xend = xend, yend = yend), curvature = i) }
  )

但这会用两个curvature参数绘制每条曲线:

But this plots each curve with both curvature argument:

如何为每一行分别传递curvature自变量?

How can I pass that curvature argument individually for each row?

推荐答案

更新

您可以先拆分数据,然后使用lapply遍历结果列表,然后我们将其馈送到geom_curve()

You might split your data first and then use lapply to iterate over the resulting list which we'll feed to the data argument of 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"]) }
  )

原始ansewr

curvature并不是一种美学.您可以将列表添加到ggplot(),以使其正常工作

curvature is not an aesthetic, as you have noted. You can add a list to ggplot(), to get it work

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) }
    )

来自help("+.gg")

...

您还可以提供一个列表,在这种情况下,列表中的每个元素都会依次添加.

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
  )

结果

这篇关于如何在ggplot2`geom_curve`函数中传递各个曲率参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:36