How to change legend position in ggvis?

夙愿已清 提交于 2020-01-05 09:12:34

问题


I would like to change the default position of the legend in a ggvis plot.

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.

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?


回答1:


Look at ?add_legend and ?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().

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


来源:https://stackoverflow.com/questions/35359622/how-to-change-legend-position-in-ggvis

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