将 ggplot() 与 manipulate() 选择器集成

Integrating ggplot() with manipulate() pickers

我想像这样结合 ggplot 和操作:

library(ggplot2);library(manipulate)

manipulate(
  ggplot(diamonds, aes(x = type, y = price)) +
    geom_point(alpha = 1/10) +
    geom_smooth(method = lm)
  type = picker("carat","depth","table")
)

(其中操纵函数将使用 picker 更改 x 输入)

但是我得到了 Error: unexpected ')' in ")"


编辑 1:修复语法错误(参见:({ ... )},

manipulate({
  ggplot(diamonds, aes(x = type, y = price)) +
    geom_point(alpha = 1/10) +
    geom_smooth(method = lm)},
  type = picker("carat","depth","table")
)

现在我得到 Don't know how to automatically pick scale for object of type manipulator.picker. Defaulting to continuous Error: Aesthetics must either be length one, or the same length as the dataProblems:type

由于选择器 returns "carat" 而不是 carat,您应该使用 aes_string,而不是 aes

manipulate(
  ggplot(diamonds, aes_string(x = type, y = "price")) +
    geom_point(alpha = 1/10) +
    geom_smooth(method = lm),
  type = picker("carat","depth","table")
)