Ggvis plot legends overlap when using tooltip

I am creating a graph with ggvis

and the legends are on top of each other.

library(ggvis)
df1 <- data.frame(x=c(0.6,1,1.4), y=c(-2, -.8, -0.2), number=c(10,8,6), 
                  type=c('A', 'A', 'B'))
df1 %>% ggvis(x = ~x, y = ~y) %>% 
  layer_points(shape=~type, fill=~number) 

      

enter image description here

How can I fix this?

Thank!


Steven's solution works for a simple example, but it doesn't work when you add the tooltip:

library(ggvis)
df1 <- data.frame(x=c(0.6,1,1.4), y=c(-2, -.8, -0.2), number=c(10,8,6), 
                  type=c('A', 'A', 'B'), id=c(1:3))

tooltip <- function(x) {
  if(is.null(x)) return(NULL)
  row <- df1[df1$id == x$id, ]
  paste0(names(row), ": ", format(row), collapse = "<br />")
}

df1 %>% ggvis(x = ~x, y = ~y) %>% 
  layer_points(shape=~type, fill=~number, key := ~id)  %>% 
  add_tooltip(tooltip, "hover") %>%
  add_legend("shape", properties = legend_props(legend = list(y = 50)))

      

+3


source to share


1 answer


Try:

df1 %>% ggvis(x = ~x, y = ~y) %>% 
  layer_points(shape=~type, fill=~number) %>%
  add_legend("shape", properties = legend_props(legend = list(y = 50)))

      

enter image description here




Edit:

As @aosmith mentioned, you can use a workaround set_options()

:

df1 %>% ggvis(x = ~x, y = ~y) %>% 
  layer_points(shape=~type, fill=~number, key := ~id)  %>% 
  add_tooltip(tooltip, "hover") %>%
  add_legend("shape", properties = legend_props(legend = list(y = 50))) %>%
  set_options(duration = 0)

      

+3


source







All Articles