【问题标题】:manipulate() slider function with ggplot2 not workingggplot2的操纵()滑块功能不起作用
【发布时间】:2021-01-13 07:51:56
【问题描述】:

当我使用manipulate() 函数时,下面的 sn-p 不起作用。滑块的初始值仅显示图表,但不显示控件。我用了plot()函数,控件出现了。

library(UsingR)
data(galton)
myHist <- function(mu){
    mse <- mean((galton$child - mu)^2)
    g <- ggplot(galton, aes(x = child)) + geom_histogram(fill = "salmon", colour = "black", binwidth=1) +
        geom_vline(xintercept = mu, size = 3) +
        ggtitle(paste("mu = ", mu, ", MSE = ", round(mse, 2), sep = ""))
    g
}
manipulate(myHist(mu), mu = slider(62, 74, step = 0.5))

【问题讨论】:

  • 没有数据的“片段”不符合 minimal reproducible example 的规范。
  • 欢迎来到 SO!请使用dput为绘图添加示例数据,否则我们不知道错误来自哪里
  • Hm 您的代码在 UsingR 包中的 galton 数据集上运行良好。

标签: r ggplot2 manipulate


【解决方案1】:

与ggplot有关。不是代码。

它发生在我身上。但是,如果我之前使用另一个示例,带有基本图,它是固定的。也就是说,在它与基础图一起使用之后,它对我来说也适用于 ggplot。例如:

f <- function(disp) {
 set.seed(1516)
 plot(density(x = rnorm(n = 40000, mean = 15, sd = disp), 
      from = 0, to = 30))
}

manipulate(f(disp), dispersion = slider(min = 0.5, 
                max = 4, initial = 2, step = 0.5, 
                label = 'Est. Dev.'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2022-01-18
    • 2016-12-17
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多