Search code examples
rggplot2ggvis

Passing column name to fill as a string with ggvis


ggvis will automatically colour my plot based on a factor column I pass it. So if my factor column was named "area" I could write this and it would execute perfectly.

names = c("Bacilli", "Actinobacteria", "area")
b_counts <- c(1,5,8,100,34,3)
a_counts <- c(1,3,11,55,67,11)
area <- c("Gut", "Skin", "Gut", "Gut", "Skin", "Oral")
rel_data <- data.frame(b_counts, a_counts, area)
names(rel_data) <- names

library(ggvis)
library(dplyr)

rel_data %>% ggvis(x = input_select(names(rel_data[,-3]), map = as.name, label = "X Axis"), 
               y = input_select(names(rel_data[,-3]), map = as.name, label = "Y Axis")) %>%
  filter(area %in% eval(input_checkboxgroup(unique(rel_data$area), selected = "Gut"))) %>%
  layer_points(fill = ~area) ### section of interest

However, if I want to pass the name of the column as a string, I can't get it to work. e.g.

region <- "area"
layer_points(fill = ~region)

I've tried as.name, eval, quote, etc but I can't seem to get anything to work. Does anyone have any ideas?


Solution

  • There's a hint at properties and scales:

    layer_points(prop("fill", as.name(region)))