首页 文章

如何使用ggvis将图例放在图表的顶部

提问于
浏览
0

ggvis 中,如果要更改图例的位置,则必须使用 add_legend(orient = 'left') 函数,但它只允许您在'left'和'right'之间进行选择 . 在 ggplot2 中,我可以使用 theme 函数将图例放在图的顶部

ggplot(iris, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
  geom_point() + 
  theme(legend.position = 'top')

是否可以用 ggvis 实现这一目标?

1 回答

  • 0

    根据ggvis文档,它是可能的 . 以下示例来自该文档 .

    试试这个

    mtcars %>% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) %>%
      layer_points() %>%
      add_relative_scales() %>%
    add_legend("fill", title = "Cylinders",
               properties = legend_props(
                 legend = list(
                   x = scaled_value("x_rel", 0.5),
                   y = scaled_value("y_rel", 0.5)
                 )
               )
    )
    

相关问题