我有一些数据想制作直方图。但是,我想用线表示这个直方图。我试过使用 ggplot2
的 freq_poly
。然而,产生的线非常参差不齐。我想知道是否可以将 splines
与 ggplot2
一起使用,这样 freq_poly
中生成的线条会更平滑。
d <- rnorm( 1000 )
h <- hist( d, breaks="FD", plot=F )
plot( spline( h$mids, h$counts ), type="l" )
这就是我想要完成的。但我想使用 ggplot2
来完成。
最佳答案
我假设您正在尝试使用 spline()
函数。如果不是,请忽略此答案。
spline()
返回一个包含两个分量 x 和 y 的列表对象:
List of 2
$ x: num [1:93] -3.3 -3.23 -3.17 -3.1 -3.04 ...
$ y: num [1:93] 1 0.1421 -0.1642 -0.0228 0.4294 ...
我们可以简单地将它们变成一个 data.frame 并绘制它们可能有更奇特的方法来做到这一点,但这会起作用:
h <- hist( d, breaks="FD", plot=F )
zz <- spline( h$mids, h$counts )
qplot(x, y, data = data.frame(x = zz$x, y = zz$y), geom = "line")
关于r - 如何用ggplot2绘制样条曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6102701/