OganM OganM - 2 months ago 17
R Question

Shape names in ggvis

In ggvis properties can be assigned to shapes as

mtcars %>%
ggvis(~wt,~mpg, shape= ~factor(cyl)) %>%
layer_points()


enter image description here

I am not sure how to set the scales however. I believe it's supposed to be using
scale_nominal
. But I failed to make it work so far.

I can change the order of things

mtcars %>%
ggvis(~wt,~mpg, shape= ~factor(cyl)) %>%
layer_points() %>% scale_nominal('shape',c(8,6,4))


enter image description here

But I can't force them to be a shape I want.

In ggplot I would to this by using
scale_shape_manual


mtcars %>% ggplot(aes(x = wt,y = mpg, shape= factor(cyl))) +
geom_point() +
scale_shape_manual(values = c('4' = 15, '6' = 18, '8' = 3))


enter image description here

Note that if I remove all 4s, other shapes are preserved

mtcars %>%
filter(cyl!=4) %>%
ggplot(aes(x = wt,y = mpg, shape= factor(cyl))) +
geom_point() +
scale_shape_manual(values = c('4' = 15, '6' = 18, '8' = 3))


enter image description here

Answer

Unlike ggplot's scale_{whatever}_manual you cannot set the range of values in a single variable. in scale_nominal domain and range variables control these properties.

mtcars  %>% 
    ggvis(~wt,~mpg, shape= ~factor(cyl)) %>%
    layer_points()  %>% 
    scale_nominal('shape',domain = c(4,6,8),range = c('square','cross','diamond'))

enter image description here

Note that ggvis allows a limited palette of shapes: circle (default), square, cross, diamond, triangle-up, or triangle-down