R中的散点图与ggvis:如何绘制具有不同形状标记和相应拟合回归线的多个组

时间:2014-08-31 10:39:51

标签: r plot scatter-plot ggvis

使用ggvis包在R中绘制以下内容,

enter image description here

代码是

mtcars %>% 
  ggvis(~wt, ~mpg, fill = ~factor(cyl)) %>% 
  layer_points() %>% 
  group_by(cyl) %>% 
  layer_model_predictions(model = "lm")

如果我在上面将fill更改为shape,则会出现错误:

Error: Unknown properties: shape.
Did you mean: stroke?

为什么呢?如何达到预期的效果?

1 个答案:

答案 0 :(得分:6)

您必须在shape电话中指定layer_points()

mtcars %>% 
  transform(cyl = factor(cyl)) %>%
  ggvis(~wt, ~mpg) %>% 
  layer_points(shape = ~cyl, fill = ~cyl) %>% 
  group_by(cyl) %>% 
  layer_model_predictions(model = "lm", formula=mpg~wt)

(请注意,我使用transform()cyl转换为系数。这意味着您无需将cyl转换为ggvis()调用中的因子,情节键有点整洁。)


enter image description here