我使用ggplot生成了一个折线图。但它的边缘不平滑。我怎么能这样做?
我尝试使用geom_smooth()
和stat_summary()
函数,但我没有得到结果。它显示错误。
这是我的行路径代码:
df <- data.frame(A=c(2,3,4,5,6,7,3,7,8,9,2),B=c(3,7,8,9,2,1,2,3,4,5,6),C=c(1,1,1,2,2,1,1,1,1,2,2))
go <- ggplot(df, aes(x=A, y=B, colour = C), pch = 17) +geom_point()
go + geom_path(data = rbind(cbind(tail(df, -1), grp = 1:(nrow(df)-1)),
cbind(head(df, -1), grp = 2:nrow(df)-1)),
aes(group = interaction(grp)))
答案 0 :(得分:4)
使用平滑样条和贝塞尔曲线的几个选项
plot(df[,1:2])
xspline(df[,1:2], shape=-0.2, lwd=2) # play with the shape parameter
library(bezier)
res <- bezier(seq(0, 1, len=100), df[,1:2], deg=nrow(df)-1)
points(res, type="l", col="green", lwd=2)
或ggplot2
## Get points
ps <- data.frame(xspline(df[,1:2], shape=-0.2, lwd=2, draw=F))
## Add to your plot
go <- ggplot(df, aes(x=A, y=B, colour = factor(C)), pch = 17) +
geom_point(size=5) +
geom_path(data=ps, aes(x, y), col=1)