简体   繁体   中英

ggvis legend disappears when fill is mapped with the set operator

I would like to be able to create a horizontal stacked bar chart like below.

    df = data.frame(name = c("A","A","B","B","C","C"),
                    low = c(3,9,2,7,5,10),
                    high = c(9,12,7,13,10,16),
                    type = c("X","Y","X","Y","X","Y"))

    df %>% ggvis(~low,~name) %>% layer_rects(x2 = ~high,height = band(),fill = ~type) %>% 
add_axis('x',title = "Value")

However, I'd like to map my own custom colors to the "Type" variable. When I use the set operator the legend disappears:

df = data.frame(name = c("A","A","B","B","C","C"),
                low = c(3,9,2,7,5,10),
                high = c(9,12,7,13,10,16),
                type = c("red","blue","red","blue","red","blue"))

df %>% ggvis(~low,~name) %>% layer_rects(x2 = ~high,height = band(),fill := ~type) %>% 
  add_axis('x',title = "Value")

Does anyone know how to fix this?

R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggvis_0.4.1   ggplot2_1.0.1 tidyr_0.2.0   plyr_1.8.1    dplyr_0.4.1  

loaded via a namespace (and not attached):
 [1] assertthat_0.1   colorspace_1.2-6 DBI_0.3.1        digest_0.6.8     grid_3.1.3       gtable_0.1.2    
 [7] htmltools_0.2.6  httpuv_1.3.2     jsonlite_0.9.16  lazyeval_0.1.10  magrittr_1.5     MASS_7.3-39     
[13] mime_0.3         munsell_0.4.2    parallel_3.1.3   proto_0.3-10     R6_2.0.1         Rcpp_0.11.5     
[19] reshape2_1.4.1   RJSONIO_1.3-0    rstudio_0.98.484 rstudioapi_0.3.1 scales_0.2.4     shiny_0.11.1    
[25] stringr_0.6.2    tools_3.1.3      xtable_1.7-4  

You have (at least) two options. If you just want to change the colors in the plot and legend, you can use one of the scale_* functions with fill as the property. For character variables, you can use scale_nominal .

df = data.frame(name = c("A","A","B","B","C","C"),
             low = c(3,9,2,7,5,10),
             high = c(9,12,7,13,10,16),
             type = c("X","Y","X","Y","X","Y"))

df %>% ggvis(~low, ~name) %>% 
    layer_rects(x2 = ~high, height = band(), fill = ~type) %>% 
    add_axis('x', title = "Value") %>%
    scale_nominal("fill", range = c("red", "blue"))

在此处输入图片说明

Using the set operator with your second dataset, you will need to add a scale via scale_nominal before you can go ahead and add a legend via add_legend . You'll use the values argument in add_legend to define what values the colors are mapped to.

df2 = data.frame(name = c("A","A","B","B","C","C"),
                low = c(3,9,2,7,5,10),
                high = c(9,12,7,13,10,16),
                type = c("red","blue","red","blue","red","blue"))

df2 %>% ggvis(~low, ~name) %>% 
    layer_rects(x2 = ~high, height = band(), fill := ~type) %>% 
    add_axis('x',title = "Value") %>%
    scale_nominal("fill", range = c("red", "blue")) %>%
    add_legend(scales = "fill", values = c("Xvalue", "Yvalue"))

在此处输入图片说明

This was all done using ggvis_0.4.2, and I didn't check what has changed since 0.4.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM