如何在ggplot2中绘制参数曲线

问题描述 投票:2回答:1

我想在ggplot2中绘制参数曲线。当只绘制点时,排序无关紧要,它可以正常工作:

library(ggplot2)
phi = seq(0, 2*pi, length.out=100)
df1 = data.frame(x=(phi+3)*cos(phi), y=(phi+3)*sin(phi))
ggplot(data=df1, aes(x, y)) + geom_point()

points in a spiral

不幸的是,ggplot2隐式地对点进行排序,所以当我尝试绘制一条线时

ggplot(data=df1, aes(x, y)) + geom_line()

我明白了

failed spiral

这不是我想要的。这些点的连接顺序应与它们在数据帧中的顺序相同。有没有办法在ggplot2中做到这一点?

(我在Plot a heart in R上阅读了答案,但我的问题是关于ggplot2并且使用极坐标不是一个选项)。

r ggplot2 parametric-equations
1个回答
4
投票

请尝试以下方法:

library(ggplot2)
phi = seq(0, 2*pi, length.out=100)
df1 = data.frame(x=(phi+3)*cos(phi), y=(phi+3)*sin(phi))
ggplot(data=df1, aes(x, y)) + geom_point() + geom_path()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.