我想为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??
Run Code Online (Sandbox Code Playgroud)
觉得我错过了一些明显的东西.任何帮助赞赏.
ton*_*nov 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)))
Run Code Online (Sandbox Code Playgroud)
此外,如果您想多次使用此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")
Run Code Online (Sandbox Code Playgroud)
现在,您可以同时设置主标题和x轴标签,修复下面提到的重叠.
另外,要更改标题的字体大小,请使用此代码(改编自@ 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)
), ...)
}
Run Code Online (Sandbox Code Playgroud)