【问题标题】:Add a plot title to ggvis为 ggvis 添加绘图标题
【发布时间】:2016-06-29 04:42:57
【问题描述】:

我想为 ggvis 绘图添加标题。我在任何地方都找不到示例。使用其他 R 图很简单,例如

library(ggplot2)
library(ggvis)
x <- 1:10
y <- x^2
df <- data.frame(x, y)

plot(x, y, main = "Plot Title")  # Base R with title
ggplot(df, aes(x, y)) + geom_point() + ggtitle("Plot Title")  # ggplot2 with title
df %>% ggvis(~x, ~y) %>% layer_points()  # ggvis but no title??

感觉好像我遗漏了一些明显的东西。任何帮助表示赞赏。

【问题讨论】:

    标签: r title ggvis


    【解决方案1】:

    嗯,似乎它还没有实施(或记录在案?)。我很确定这将很快添加。现在,您可以像这样使用肮脏的 hack:

    df %>% ggvis(~x, ~y) %>% layer_points() %>% 
      add_axis("x", title = "X units") %>%
      add_axis("x", orient = "top", ticks = 0, title = "Plot Title",
               properties = axis_props(
                 axis = list(stroke = "white"),
                 labels = list(fontSize = 0)))
    

    此外,如果您想多次使用此 hack,您可以为其创建一个简写。

    add_title <- function(vis, ..., x_lab = "X units", title = "Plot Title") 
    {
      add_axis(vis, "x", title = x_lab) %>% 
        add_axis("x", orient = "top", ticks = 0, title = title,
                 properties = axis_props(
                   axis = list(stroke = "white"),
                   labels = list(fontSize = 0)
                 ), ...)
    }
    df %>% ggvis(~x, ~y) %>% layer_points() %>% add_title() #same as before
    df %>% ggvis(~x, ~y) %>% layer_points() %>% add_title(title = "Hello", x_lab = "ton")
    

    编辑:

    现在您可以同时设置主标题和 x 轴标签,修复下面提到的重叠。

    【讨论】:

    • 谢谢托尼。有时 hack 可能比它们的价值更麻烦,但这很整洁!
    • 不客气!一旦出现更优雅的原生解决方案,我会尽量记住更新这篇文章。
    • 不幸的是,这个 hack 与在实际 x 轴上设置标签不兼容。下面显示了使用 add_title() 时 x 轴标签与原始标签“wt”重叠。看起来像 ggvis 中的错误。 mtcars %&gt;% ggvis(~wt, ~mpg) %&gt;% layer_points() %&gt;% add_axis("x", title = "long axis label") %&gt;% add_title(title = "Any Title")
    • @ChrisWarth 试试我建议的修复方法,似乎可以解决问题。
    • @dsh 尽管您建议的编辑被审阅者拒绝,但它实际上很有帮助。我更新了答案并确保它有效,尽管我省略了 type = 规范,因为它是多余的(?add_axis 中的第一个示例支持)。无论如何,只想对您的努力和有意义的编辑解释表示感谢。
    【解决方案2】:

    此外,要更改标题的字体大小,请使用以下代码(改编自 @tonytonov 的答案):

    add_title <- function(vis, ..., x_lab = "X units", title = "Plot Title") 
    {
      add_axis(vis, "x", title = x_lab) %>% 
        add_axis("x", orient = "top", ticks = 0, title = title,
                 properties = axis_props(
                   axis = list(stroke = "white"),
                   title = list(fontSize = 32),
                   labels = list(fontSize = 0)
                 ), ...)
    }
    

    【讨论】:

      【解决方案3】:

      我更新了@tonytonov 的答案以不干扰默认的 X 轴。它仍然作为轴实现,但实现了自己的“标题”比例,并将用户提供的标题属性(如字体大小和颜色)与使轴不可见所需的默认属性正确合并。此版本隐藏轴而不假定特定的背景颜色。 函数后面三个例子。

      library(ggvis)
      
      # ggvis lacks a plot title function, so add one.
      # based on clever hack by tonytonov
      # http://stackoverflow.com/a/25030002/1135316
      add_title <- function(vis, ..., properties=NULL, title = "Plot Title") 
      {
          # recursively merge lists by name
          # http://stackoverflow.com/a/13811666/1135316
          merge.lists <- function(a, b) {
              a.names <- names(a)
              b.names <- names(b)
              m.names <- sort(unique(c(a.names, b.names)))
              sapply(m.names, function(i) {
                         if (is.list(a[[i]]) & is.list(b[[i]])) merge.lists(a[[i]], b[[i]])
                         else if (i %in% b.names) b[[i]]
                         else a[[i]]
                     }, simplify = FALSE)
          }
      
          # default properties make title 'axis' invisible
          default.props <- axis_props(
              ticks = list(strokeWidth=0),
              axis = list(strokeWidth=0),
              labels = list(fontSize = 0),
              grid = list(strokeWidth=0)
              )
          # merge the default properties with user-supplied props.
          axis.props <- do.call(axis_props, merge.lists(default.props, properties))
      
          # don't step on existing scales.
          vis <- scale_numeric(vis, "title", domain = c(0,1), range = 'width')
          axis <- ggvis:::create_axis('x', 'title', orient = "top",  title = title, properties = axis.props, ...)
          ggvis:::append_ggvis(vis, "axes", axis)
      }
      
      # add title with default X axis.
      iris %>% 
        ggvis(x = ~Petal.Width) %>% 
        layer_points(y = ~Petal.Length, fill = ~Species) %>%
        add_title(title = "Petal.Width vs. Petal.Length")
      
      # Add title with overridden X axis
      iris %>% 
        ggvis(x = ~Petal.Width) %>% 
        layer_points(y = ~Petal.Length, fill = ~Species) %>%
        add_axis("x", title = "X-axis!!!!", title_offset=40,
                 properties = axis_props(title=list(fontSize=16),
                     labels = list(fontSize = 12))) %>%
        add_title(title = "Petal.Width vs. Petal.Length")
      
      # Change the font size of the title.
      iris %>% 
        ggvis(x = ~Petal.Width) %>% 
        layer_points(y = ~Petal.Length, fill = ~Species) %>%
        add_title(title = "Petal.Width vs. Petal.Length",
                  properties = axis_props(title=list(fontSize=20)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-27
        • 2019-09-08
        • 2021-01-11
        • 1970-01-01
        • 2020-07-05
        • 2020-12-18
        • 2020-11-12
        • 1970-01-01
        相关资源
        最近更新 更多