有谁知道如何在 ggplot 上使用 manipulate(),以便轻松选择平滑(跨度)级别?我尝试了以下但没有成功:
# fake data
xvals <- 1:10
yvals <- xvals^2*exp(rnorm(2,5,0.6))
data <- data.frame(xvals,yvals)
# plot with manipulate
manipulate(
ggplot(data,aes(xvals,yvals)) +
geom_smooth(span=slider(0.5,5)) +
geom_point()
)
我希望能够轻松地循环通过“平滑级别”。
最佳答案
更改了您的数据以获得更多数据点。
xvals <- 1:100
yvals <- rnorm(100)
data <- data.frame(xvals,yvals)
您必须在 geom_smooth()
中为与 span=
一起使用的值命名(例如,span.val
),然后在 ggplot()
函数之外定义 span.val=slider(0.1,1)
- 在本例中作为 manipulate()
的第二个参数。
library(manipulate)
library(ggplot2)
manipulate({
#define plotting function
ggplot(data,aes(xvals,yvals)) +
geom_smooth(method="loess",span=span.val) +
geom_point()},
#define variable that will be changed in plot
span.val=slider(0.1,1)
)
关于r - 如何在 ggplot geom_smooth() 上使用 manipulate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905744/