我的数据集是这样的:
type <- rep(c("a", "b"), each=7)
con <- rep(c(32, 16, 8, 4, 2, 1, 0), 2)
Abs_net <- c(0.06, 0.142, 0.231, 0.504, 1.648, 1.759, 1.525, 0.063, 0.091,0.115, 0.117, 0.118, 0.881, 1.515)
data <- data.frame(con, Abs_net, type)我试过这段代码
ggplot(data, aes(con, Abs_net, color=type)) +
geom_point()+
geom_smooth(se=FALSE)我也在geom_smooth中尝试了span,但是它没有帮助。
我希望看到这样的非线性图,即Abs_net只在con值较高时减少,而只在con值较低时增加(我不想要增加和减少模式)。我可以把它放在图板棱镜中,但不能放在R中。
发布于 2019-06-12 12:28:04
下面是一种可能的拟合,假设模型为y= e^-x
ggplot(data, aes(con, Abs_net, color=type)) +
geom_point(size = 2)+
geom_smooth(method = "gam",
formula = y ~ exp(-x),
se=FALSE, lty = "dashed")

发布于 2019-06-12 13:35:01
老实说,我花了相当长的时间来理解你的问题,但我想我最终还是理解了它。问题似乎是,尽管Abs_net在“欺骗”中单调下降,但情节并没有这样做。
要解决这个问题,您可以:-使用不同的平滑函数
library(ggplot2)
ggplot(data, aes(con, Abs_net, color=type)) +
geom_point()+
geom_smooth(se=FALSE, method = "loess", formula = y~x) (这里的method =可以用"lm“代替(我假设它不是你真正想要的,而是更接近的东西)
你得到处找别的办法,我找不到一个能解决你问题的办法。
就像这样
library(ggplot2)
ggplot(data, aes(con, Abs_net, color=type)) +
geom_point()+
geom_smooth(se=FALSE, method = "glm", formula = y ~ poly(x, 2, raw=TRUE))看起来更好了,但仍然存在问题(没有准确地命中数据点,最终会向上倾斜)。
还请注意,在https://ggplot2.tidyverse.org/reference/geom_smooth.html下,您可以看到可以将哪些函数用于方法参数。您还可以更新loess使用的公式(在我的geom_smooth中指明)。如果有帮助的话请告诉我。
https://stackoverflow.com/questions/56554110
复制相似问题