用样条线连接点 [英] Connect dots with splines

查看:48
本文介绍了用样条线连接点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何平滑线图中的边缘?我可以这样画线图:

How do I smooth the edges in line plot? I can do I line plot like this:

data <- data.frame(x=1:10, y=c(22,23,21,25,23,24,20,27,22,24))
ggplot(data, aes(x,y)) + 
  geom_line(colour='forestgreen')

但是,我不喜欢锋利的边缘.有没有办法在这些点上画一条线,使线平滑?

However, I don't like sharp edges. Is there a way to draw a line through those points so that the line is smooth?

推荐答案

这是一种实现方法:

library(ggplot2)
library(splines)
library(gridExtra)

dat <- data.frame(x=1:10, y=c(22,23,21,25,23,24,20,27,22,24))

plot.new() # have to do this unfortunately
res <- xspline(dat$x, dat$y, -0.25, draw=FALSE)

gg1 <- ggplot(dat, aes(x,y)) +
  geom_line(colour='forestgreen') +
  geom_point()

gg2 <- ggplot(data=data.frame(x=res$x, y=res$y), aes(x, y)) + 
  geom_point(data=dat, aes(x, y), size=1) +
  geom_line(color="blue")

grid.arrange(gg1, gg2, ncol=1)

这是使用 xspline 进行插值的.查找该函数以查看如何调整 -0.25 参数(范围从 -1 1 ).

This is using xspline to do the interpolation. Lookup the function to see what tweaking the -0.25 parameter (range is -1 to 1) will do.

这篇关于用样条线连接点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆