legends on ggvis graph are overlaping when using tooltip

隐身守侯 提交于 2019-12-09 07:03:07

问题


I'm generating a graph with ggvis and the legends are in 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) 

How can I fix this?

Thanks!


Steven's solution works for the simple example but It does not work when you add a 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)))

回答1:


Try:

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


Edit:

As mentionned by @aosmith, you could use the set_options() workaround:

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)


来源:https://stackoverflow.com/questions/30916337/legends-on-ggvis-graph-are-overlaping-when-using-tooltip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!