简体   繁体   English

在ggplot中避免二次拟合中的锯齿形图案

[英]avoid zig-zag pattern in quadratic fit in ggplot

I struggle with plotting a quadratic fit in ggplot, as the line between the overlapping x-values jumps back and forth between the upper and lower side of the curve.我很难在 ggplot 中绘制二次拟合,因为重叠的 x 值之间的线在曲线的上侧和下侧之间来回跳跃。

在此处输入图像描述

However, doing the same in base plot, it works, which makes me think I am overlooking something (possibly really stupid) in ggplot.然而,在基础 plot 中做同样的事情,它可以工作,这让我觉得我在 ggplot 中忽略了一些东西(可能真的很愚蠢)。 Could anybody guide me towards how to receive a propper line in ggplot?任何人都可以指导我如何在 ggplot 中接收一条合适的线吗?

在此处输入图像描述

I unfortunately don`t know how to reproduce the exact problem, so just add code for a similar shaped "curve":不幸的是,我不知道如何重现确切的问题,所以只需为类似形状的“曲线”添加代码:

library(ggplot2)
x1 <- log(c(1:100, 99:1))
y1 <- log(seq(0.22, 0.2, length.out = 199))
dat <- data.frame(x = x1, y = y1)
ggplot(data = dat, aes(x = x, y = y)) + geom_line()
plot(y1 ~ x1, type = "l")

Thanks a lot in advance!提前非常感谢!

Try geom_path() instead.尝试geom_path()代替。

library(ggplot2)
x1 <- log(c(1:100, 99:1))
y1 <- log(seq(0.22, 0.2, length.out = 199))
dat <- data.frame(x = x1, y = y1)
ggplot(data = dat, aes(x = x, y = y)) + geom_path()
plot(y1 ~ x1, type = "l")

geom_path() connects the observations in the order in which they appear in the data. geom_path()按照它们在数据中出现的顺序连接观测值。 geom_line() connects them in order of the variable on the x axis. geom_line()按照 x 轴上的变量顺序连接它们。 Documentation.文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM