简体   繁体   English

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

[英]Scatter plot in R with ggvis: how to plot multiple groups with different shape markers and corresponding fitted regression lines

To plot the following in R with the ggvis package, 要使用ggvis包在R中绘制以下内容,

在此输入图像描述

the code is 代码是

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

If I change the fill to shape in the above, there would be an error: 如果我将fill更改为上面的shape ,则会出现错误:

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

Why? 为什么? How to achieve the desired outcome? 如何达到预期的效果?

You have to specify the shape in the layer_points() call: 您必须在layer_points()调用中指定shape

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)

(Note that I use transform() to convert cyl into a factor. This means you don't have to convert cyl into a factor in the ggvis() call, and the plot key is a little bit neater.) (注意,我使用transform()cyl转换为一个因子。这意味着你不必在ggvis()调用ggvis() cyl转换成一个因子,并且绘图键有点整洁。)


在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM