消除ggvis Shiny中未使用的因子值

时间:2015-03-25 15:14:45

标签: r shiny ggvis

我想在ggvis图中消除x-axis中未使用的因子值。 我有data.frame

choices <- c("Value1", "Value2",
                "Value3", "Value4",
                "Value5", "Value6",
                "Value7", "Value8",
                "Value9", "Value10",
                "Value11", "Value12")

levele <- c("AT1","AT2","AT3","AT4","AT5","RT1","AT6","AT7","AT8","AT9","AT10","RT2")

number_value1 <- sample(1:100,22)
number_value2 <- sample(1:100,22)

df1 <- data.frame(product = c(rep("Product1",12),rep("Product2",10)), name = c(choices,choices[1:10]),
                  short_name = c(levele,levele[1:10]),number = number_value1, number2 = number_value2) 

当我想创建一个仅基于Product2的数据的ggvis时,我得到了

df1 %>%
  dplyr::filter(product == "Product2") %>%
  ggvis(x = ~short_name,y =  ~number) %>%
  layer_points(size := 120,fill = ~short_name)

enter image description here

图表还显示未使用的因子值。我不想看到这一点,所以我使用x = ~factor(short_name)来消除这些未使用的案例。

df1 %>%
  dplyr::filter(product == "Product2") %>%
  ggvis(x = ~short_name,y =  ~number) %>%
  layer_points(size := 120,fill = ~short_name)

enter image description here

=============================================== =====================
所以没关系。问题是当我想在Shiny app中做同样的事情时,它有可能操纵轴。有2个dataframes,其中有2个Productsname的数量不同。当我在Product1部分中将Produt2更改为Select Product时,我会在x轴中包含所有因素,也会使用未使用的因子。我的代码是here。如果有人知道如何正确实现它?

2 个答案:

答案 0 :(得分:1)

正如@docendo discimus指出的那样,你可以使用droplevels。我分叉你的代码并进行了更改here

答案 1 :(得分:0)

有可能。 只需要创建一个新的反应变量datasetInput()并更改一些代码行。 这是my code.

相关问题