参数函数的geom_line

时间:2015-08-17 08:44:09

标签: r ggplot2

以下是可视化贝塞尔曲线的示例:

library(ggplot2)
t = seq(0, 1, 0.001)
x0 = 4
y0 = 1
x1 = 28
y1 = 48
x2 = 50
y2 = 42
x3 = 40
y3 = 5
x = x0 * (1 - t)^3 + 3 * x1 * t * (1 - t)^2 + 3 * x2 * t^2 * (1 - t) + x3 * t^3 
y = y0 * (1 - t)^3 + 3 * y1 * t * (1 - t)^2 + 3 * y2 * t^2 * (1 - t) + y3 * t^3 
data = data.frame(x, y)
data1 = data.frame(x = c(x0, x2), xend = c(x1, x3), y = c(y0, y2), yend = c(y1, y3))
data2 = data.frame(x = c(x0, x1, x2, x3), y = c(y0, y1, y2, y3))
mplot = ggplot(data, aes(x, y)) + geom_line() + geom_segment(data = data1, aes(x = x, y = y, xend = xend, yend = yend)) + geom_point(data = data2, aes(x, y))
mplot

结果:

enter image description here

曲线的右侧部分已呈现为区域。我想这是因为y值对于某些x值不是唯一的。怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以使用geom_line()

,而不是使用geom_path()
ggplot(data, aes(x, y)) + 
    geom_path() + 
    geom_segment(data = data1, aes(x = x, y = y, xend = xend, yend = yend)) + 
    geom_point(data = data2, aes(x, y))

This is the output of the code using geom_path()

...甚至geom_point()都可以解决这个问题,但我猜你必须使用磅值来获得你想要的东西。

enter image description here

相关问题