传递列名以使用ggvis填充为字符串

时间:2015-04-04 12:44:51

标签: r ggplot2 ggvis

ggvis将根据我传递的因子列自动为我的绘图着色。因此,如果我的因子列被命名为" area"我可以写这个,它会完美地执行。

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

但是,如果我想将列的名称作为字符串传递,我就无法使其工作。 e.g。

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

我已尝试过.name,eval,quote等,但我似乎无法正常工作。有没有人有任何想法?

1 个答案:

答案 0 :(得分:3)

properties and scales提示:

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