【问题标题】:How to place a legend on top of the plot with ggvis如何使用ggvis在情节顶部放置图例
【发布时间】:2015-12-13 08:55:55
【问题描述】:

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 实现这一目标?

【问题讨论】:

    标签: r ggplot2 legend ggvis


    【解决方案1】:

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

    【讨论】:

    • 谢谢!这接近我所需要的,但我正在寻找一种更类似于ggplot2 的行为,其中图例“推动”情节区域,而不是将其置于情节之上。我需要它来进行交互式可视化,所以我现在不能提前在哪里放置它。
    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2020-02-28
    • 1970-01-01
    相关资源
    最近更新 更多