ggvis icon indicating copy to clipboard operation
ggvis copied to clipboard

layer_points and layer_boxplots do not align well when x-axis is category

Open yingchen69 opened this issue 10 years ago • 5 comments

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

yingchen69 avatar Aug 19 '14 15:08 yingchen69

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"))

wch avatar Aug 19 '14 16:08 wch

Winston,

Thanks a lot for the trick!

Ying

yingchen69 avatar Aug 19 '14 18:08 yingchen69

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

yingchen69 avatar Aug 19 '14 19:08 yingchen69

Latter question answered in #256.

wch avatar Sep 30 '14 21:09 wch

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?

DarioBoh avatar Oct 12 '16 21:10 DarioBoh