简体   繁体   English

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

[英]How to change legend position in ggvis?

I would like to change the default position of the legend in a ggvis plot. 我想在ggvis图中更改图例的默认位置。

library(ggvis)
data(mtcars)

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

By default, the legend is on the right side. 默认情况下,图例位于右侧。 How to put it on the top? 如何放在顶部?

With ggplot you can achieve this simply but I could not find any similar way to do the same with ggvis . 使用ggplot可以简单地实现这一点,但是我找不到使用ggvis进行相同操作的任何类似方法。

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

This thread suggests that up until now you cannot change the orientation of the legend, but is it true for the position as well? 该线索表明,到目前为止,您无法更改图例的方向,但是位置也是如此吗?

Look at ?add_legend and ?legend_props . 查看?add_legend?legend_props I don't think you can do position=top etc, but you could use the x and y ranges of the data to position the legend exactly at the mid-top like with position='top' in ggplot() . 我不认为可以进行position=top等操作,但是可以使用数据的x和y范围将图例准确定位在中上position='top'ggplot() position='top'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM