Search code examples
rggvis

ggvis - using a variable to assign "x" property using props


According to the properties and scales reference and this SO post I should be able to use a props(prop()) call as shown below to create the graph. I'm getting an unintelligible error though.

test_df <- data.frame(cbind(x_vals = letters[1:5],
                            y_vals = 1:5)) 

Graphs correctly:

test_df %>% ggvis(x = ~x_vals, y = ~y_vals) %>% 
  layer_points() 

Has error:

x_val_variable <- "x_vals"

test_df %>% ggvis(y = ~y_vals) %>% 
      props(prop("x", as.name(x_val_variable)) %>% 
      layer_points()

Can anyone help me by telling me what I'm doing wrong?


Solution

  • I figured out the correct usage of prop(), it doesn't belong in the %>% chain, it's an argument of ggvis or layer_points(). I still don't understand when you would use props() though.

    test_df <- data.frame(cbind(x_vals = letters[1:5],
                                y_vals = 1:5)) 
    
    x_val_variable <- "x_vals"
    
    #you can use either one of these, they are identical
    p1 <- prop("x", as.name(x_val_variable))
    p2 <- prop("x", parse(text = x_val_variable)[[1]])
    
    test_df %>% ggvis(p1, y = ~y_vals) %>% layer_points()