[英]Plotting a spline function with ggplot2
我想用ggplot2绘制样条函数。 我用这个时:
library(ggplot2)
spline_x <- 1:6
spline_y <- c(0, 0.5, 2, 2, 0.5, 0)
spl_fun <- splinefun(spline_x, spline_y)
p <- ggplot()
p + stat_function(fun = spl_fun) + xlim(min(spline_x), max(spline_x)) + ylim(min(spline_y), max(spline_y))
我得到一个空的情节。 plot()的作用相同:
plot(spl_fun,
xlim = c(min(spline_x), max(spline_x)),
ylim = c(min(spline_y) - 1, max(spline_y) + 1)
)
另外,如何在多行上使用stat_function
拆分行? 如果我只是输入换行符(比如plot()
示例),它(当然)在调用stat_function
后停止stat_function
,并且不再考虑x / ylim。
你没有给ggplot
任何东西使用,也没有传递给函数的值。 尝试:
library(ggplot2)
spline_x <- 1:6
spline_y <- c(0, 0.5, 2, 2, 0.5, 0)
spl_fun <- splinefun(spline_x, spline_y)
p <- ggplot(data.frame(x=spline_x, y=spline_y), aes(x, y))
p <- p + stat_function(fun = spl_fun)
p
p <- p + xlim(min(spline_x), max(spline_x))
p <- p + ylim(min(spline_y), max(spline_y))
p
在即将要发布的新GGPLOT2(通过devtools安装),可以使用geom_xspline
在我的(也即将要发布的) ggalt
具有包geom_xspline
功能(这将产生类似的结果取决于参数):
library(ggplot2) # devtools::install_github("hadley/ggplot2")
library(ggalt) # devtools::install_github("hrbrmstr/ggplot2")
spline_x <- 1:6
spline_y <- c(0, 0.5, 2, 2, 0.5, 0)
p <- ggplot(data.frame(x=spline_x, y=spline_y), aes(x, y))
p <- p + geom_xspline(spline_shape=1)
p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.