ggvis
ggvis copied to clipboard
layer_points and layer_boxplots do not align well when x-axis is category
Hi guys,
I tried to overlap points to boxplots. When x-axis is continuous, the points and boxes aligned perfectly, but when I changed x-axis to category, the points align to the left side edge of the boxes.
> mtc <- mtcars
> mtc$cyl <- factor(mtc$cyl)
> mtc %>% ggvis(~cyl, ~mpg) %>% layer_boxplots(width=0.5) %>% layer_points()
> mtcars %>% ggvis(~cyl, ~mpg) %>% layer_boxplots(width=0.5) %>% layer_points()
Is there any trick to change the alignment when x-axis is category?
Thanks a lot,
Ying
Here's a workaround for now. Hopefully we'll figure out a better way to do this:
mtc %>% ggvis(~cyl, ~mpg) %>% layer_boxplots(width=0.5) %>%
layer_points(prop("x", ~cyl, scale = "xcenter"))
Winston,
Thanks a lot for the trick!
Ying
Hi Winston,
Sorry one more question:
I tried to use add_tooltip() to my layer_boxplots() superimposed with layer_points() plots I want to use values other than the x or y axis as values showed by tooltips. I tried the method you provided in the google group thread. The add_tooltip() works, but it shows character(0) instead of real values for all items, such as:
mpg:character(0)
cyl:character(0)
...
The code is:
mtc <- mtcars
mtc$cyl <- factor(mtc$cyl)
mtc$id <- 1:nrow(mtc)
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
mtc %>% ggvis(~cyl, ~mpg) %>% layer_boxplots(width=0.5) %>%
layer_points(prop("x", ~cyl, scale = "xcenter")) %>%
add_tooltip(all_values, "hover")
Any suggestion?
Thanks a lot,
Ying
Latter question answered in #256.
Based on previous comments I have this
mtc <- mtcars
mtc$cyl <- factor(mtc$cyl)
mtc %>% ggvis(~cyl, ~mpg) %>%
layer_boxplots() %>%
layer_points(prop("x", ~cyl, scale = "xcenter"))
but the background grid does not align to the boxplots tails, any suggestions?