[英]Smoothing in ggplot
I have this ggplot 我有这个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))
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")
I would like to have the smooth lines constrained to lie above zero, perhaps something similar to a kernel density. 我希望将平滑线约束在零之上,这可能类似于核密度。 In fact if I had the underlying data, I expect a kernel density is exactly what I would want, but I only have the aggregated data.
事实上,如果我有基础数据,我希望内核密度正是我想要的,但我只有聚合数据。 Is there any way to do this ?
有没有办法做到这一点? I tried using different
method=
in the geom_smooth
, but the small dataset seems to prevent it. 我尝试在
geom_smooth
使用不同的method=
,但是小数据集似乎阻止了它。 I wondered about using stat_function
but I don't have much clue about how to proceed with finding a suitable function to plot. 我想知道如何使用
stat_function
但我没有太多关于如何继续寻找合适的绘图函数的线索。
Another possibility is to use method="glm"
with a spline curve and a log link (ie also tried method="gam"
, but its automatic complexity adjustment wanted to reduce the wiggliness too much: 另一种可能性是使用
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))
How about geom_density()
? geom_density()
怎么样?
ggplot(dt1, aes(x=pctOAC,y=NoP, colour=Age, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_density(stat="identity", fill=NA) +
theme(legend.position=c(.2,0.8))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.