在ggvis中添加图表标题

23

我想为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??

感觉自己错过了一些显而易见的东西。希望能得到帮助。

3个回答

34

好像这个功能还没有被实现(或者没有记录)?我很确定它很快就会被添加。现在你可以使用一个“脏”技巧,像这样:

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

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

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轴标签,修复下面提到的重叠问题。


谢谢Tony。有时候,一些“hack”可能会带来更多麻烦,但这个很好! - Anto
不客气!我会尽快更新这篇文章,一旦出现更优雅的本地解决方案。 - tonytonov
1
很遗憾,这个hack与在实际x轴上设置标签不兼容。以下显示了当使用add_title()时,x轴标签重叠了原始标签“wt”。看起来是ggvis中的一个bug。mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% add_axis("x", title = "长轴标签") %>% add_title(title = "任何标题") - Chris Warth
@ChrisWarth 请尝试我提出的修复方案,似乎可以解决问题。 - tonytonov
12
尽管您的建议编辑已被审核员拒绝,但它确实很有帮助。我更新了答案并确保其可行性,尽管我省略了 type = 规范,因为它是多余的(在 ?add_axis 的第一个示例中提供支持)。无论如何,只是想感谢您的努力和有意义的编辑说明。 - tonytonov
显示剩余2条评论

5
此外,要更改标题的字体大小,请使用以下代码(改编自@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轴。它仍然作为一个轴实现,但是它实现了自己的'title'比例尺,并将用户提供的标题属性(如字体大小和颜色)与使轴不可见所需的默认属性正确地合并。这个版本隐藏了轴,而不预设特定的背景色。以下是三个示例函数。
library(ggvis)

# ggvis lacks a plot title function, so add one.
# based on clever hack by tonytonov
# https://dev59.com/AV8f5IYBdhLWcg3wHfk_#25030002
add_title <- function(vis, ..., properties=NULL, title = "Plot Title") 
{
    # recursively merge lists by name
    # https://dev59.com/dGYr5IYBdhLWcg3wRoOW#13811666
    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)))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接