gpt4 book ai didi

r - 在 ggplot 中平滑

转载 作者:行者123 更新时间:2023-12-04 01:49:37 25 4
gpt4 key购买 nike

我有这个 ggplot

ggplot(dt.1, aes(x=pctOAC,y=NoP, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_smooth(aes(x=pctOAC,y=NoP, colour=Age), se=F, method="loess",show_guide = FALSE,lwd=0.7) +
theme(legend.position=c(.2,0.8))

enter image description here
dt1 <- structure(list(Age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("o80", "u80"), class = "factor"), NoP = c(47L, 5L, 33L, 98L, 287L, 543L, 516L, 222L, 67L, 14L, 13L, 30L, 1L, 6L, 17L, 30L, 116L, 390L, 612L, 451L, 146L, 52L), pctOAC = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)), .Names = c("Age", "NoP", "pctOAC"), row.names = c(NA, -22L), class = "data.frame")

我希望平滑线被限制在零以上,也许类似于内核密度。事实上如果我 底层数据,我预计内核密度为 正好我想要什么,但我只有汇总数据。有没有办法做到这一点?我尝试使用不同的 method=geom_smooth ,但小数据集似乎阻止了它。我想知道如何使用 stat_function但我对如何继续寻找合适的函数进行绘图没有太多线索。

最佳答案

另一种可能性是使用 method="glm"带有样条曲线和对数链接(即也尝试过 method="gam" ,但它的自动复杂度调整想要减少太多的摆动:

library(splines)
ggplot(dt.1, aes(x=pctOAC,y=NoP, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_smooth(aes(colour=Age), se=F,
method="glm",
formula=y~ns(x,8),
family=gaussian(link="log"),
show_guide = FALSE,lwd=0.7) +
theme(legend.position=c(.2,0.8))

enter image description here

关于r - 在 ggplot 中平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com