Igo*_* F. 3 r ggplot2 parametric-equations
我想在 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()
Run Code Online (Sandbox Code Playgroud)
不幸的是,ggplot2 对点进行了隐式排序,因此当我尝试绘制一条线时
ggplot(data=df1, aes(x, y)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
我得到
这不是我想要的。这些点的连接顺序应与它们在数据框中的顺序相同。有没有办法在 ggplot2 中做到这一点?
(我阅读了Plot a heart in R 中的答案,但我的问题特别是关于 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() + geom_path()
Run Code Online (Sandbox Code Playgroud)