问题
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