library(ggplot2)
x = c(0, 1, 1, 0, 0)
y = c(0, 0, 1, 1, 0)
plot(x, y, type='l')

这个图是正确的
而如果用ggplot
ggplot() + geom_line(aes(x, y))
# ggplot(NULL, aes(x, y)) + geom_line()

这个图就是错误的,很奇怪。
x = c(0, 1, 1, 0, 0) + rnorm(5)*0.01
y = c(0, 0, 1, 1, 0) + rnorm(5)*0.01
# plot(x, y, type='l')
ggplot() + geom_line(aes(x, y))

这张图也是错误的,实在搞不懂,不知道哪位大咖能帮忙回答一下
补充:
找到答案,geom_line会对数据进行排序,改用geom_path就可以了。

博客探讨了使用ggplot2绘制线条图时遇到的问题,即geom_line默认对数据进行排序导致图形错误。解决方案是改用geom_path来避免数据排序的影响,从而正确显示图形。
3119

被折叠的 条评论
为什么被折叠?



