Scatter plot in R with ggvis: how to build multiple groups with different shape markers and corresponding corresponding regression lines

To build the following in R with the ggvis package,

enter image description here

code

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

If I changed the fill value to shape in the above, an error would occur:

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

Why? How to achieve the desired result?

+6
source share
1 answer

You must specify shape in the layer_points() call:

 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 to a coefficient. This means that you do not need to convert cyl to a coefficient in the call to ggvis() , and the plot key is a little neater.)


enter image description here

+6
source

Source: https://habr.com/ru/post/974604/


All Articles