0

I want to eliminate not used factor value from x-axis in ggvis plot. I have 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) 

and when I want to create a ggvis only with data based on Product2 I get

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

enter image description here

Plot shows also unused factor values. I do not want to see that so I used x = ~factor(short_name) to eliminate these unused cases.

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

enter image description here

====================================================================
So that's fine. The problem is when I want to do it the same thing in Shiny app, which has possibility to manipulate axis. There are 2 dataframes in which there are 2 Products with different number of name. When I change Product1 to Produt2 in Select Product section in result I have all factors in x-axis, also unused factors. My code is here. If anyone knows how to implement it correctly?

Nicolabo
  • 1,337
  • 12
  • 30
  • 2
    `droplevels` is usually the easiest way to remove unused factor levels. You can apply it to a whole data.frame. You could try: `df1 %>% dplyr::filter(product == "Product2") %>% droplevels() %>% ... ` – talat Mar 25 '15 at 15:17

2 Answers2

1

As @docendo discimus pointed out, you can use droplevels. I forked your code and made the changes here

jentjr
  • 352
  • 2
  • 9
  • ,docendo discimus Ok,but in result, when I manipulate `Select` section e.g. when I choose `Value1` `Value2`, `Value3` and `Value4` , `Value4` has red color, but when I click only `V1` and `V4` ,`V4` has orange color as `V2` previously. How to save the color of each `Value`s? – Nicolabo Mar 25 '15 at 19:48
  • I don't believe this is possible yet in `ggvis`. I tried using `scale_nominal("fill", c("AT1","AT2","AT3","AT4","AT5","RT1","AT6","AT7","AT8","AT9","AT10","RT2"), c("green","blue", "purple", "aliceblue", "yellow", "sienna", "navy", "orchid","maroon", "linen", "grey", "khaki"))` to assign color values to the variables, but then the unused factors show up again in the legend. There is an open issue related to this on github https://github.com/rstudio/ggvis/issues/208 and a related question here http://stackoverflow.com/questions/24810470/custom-fill-color-in-ggvis-and-other-options – jentjr Mar 26 '15 at 03:07
0

It is possible. Only have to do is to create a new reactive variable datasetInput() and change some lines of code. Here is my code.

Nicolabo
  • 1,337
  • 12
  • 30