如何更改ggvis中的图例位置?

How to change legend position in ggvis?

我想更改图例在 ggvis 图中的默认位置。

library(ggvis)
data(mtcars)

mtcars %>% 
    ggvis(x=~wt, y = ~mpg, fill = ~cyl) %>%
    layer_points()

默认情况下,图例在右侧。怎么置顶?

使用 ggplot 可以简单地实现这一点,但我找不到任何类似的方法来使用 ggvis

library(ggplot2)
mtcars %>% 
    ggplot(aes(x=wt, y=mpg, fill=cyl)) +
    geom_point() + 
    theme(legend.position = 'top')

This thread 表明到目前为止您无法更改图例的方向,但位置也是如此吗?

查看 ?add_legend?legend_props。我不认为你可以做 position=top 等,但你可以使用数据的 x 和 y 范围将图例精确定位在中间顶部,就像 position='top' in ggplot() .

mtcars %>% 
    ggvis(x=~wt, y = ~mpg, fill = ~cyl) %>%
    layer_points() %>%
    add_legend("fill", properties=legend_props(
               legend=list(x=scaled_value("x", 3.25), y=scaled_value("y", 40))
    )
)