r - 如何用ggplot2绘制样条曲线?

标签 r ggplot2 histogram splines

我有一些数据想制作直方图。但是,我想用线表示这个直方图。我试过使用 ggplot2freq_poly。然而,产生的线非常参差不齐。我想知道是否可以将 splinesggplot2 一起使用,这样 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/

相关文章:

r - R直方图-频率范围

r - ggplot2:更改 facet_grid 图中的 strip.text 位置

r - 如何在ggplot2中的堆叠条组之间放置空间

r - ggplot将颜色渐变缩放到数据范围之外的范围

python - 如何使用TensorBoard write_grads函数?

matplotlib - Matplotlib 子图Fig 和axs 变量的用途是什么

R: `rbind`,其中列指示绑定(bind)

r - 将列表列表简化为单个列表

r caretEnsemble 警告 : indexes not defined in trControl

r - 用 [[<-