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

时间:2015-03-02 22:13:44

标签: r ggplot2

我想结合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

1 个答案:

答案 0 :(得分:2)

由于选择器返回"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")
)