向 ggvis 添加情节标题 [英] Add a plot title to ggvis
问题描述
我想为 ggvis 绘图添加标题.我在任何地方都找不到示例.使用其他 R 绘图很简单,例如
I want to add a title to a ggvis plot. I can't find an example anywhere. It's simple to do with other R plots, e.g.
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??
感觉好像我遗漏了一些明显的东西.任何帮助表示赞赏.
Feel like I'm missing something obvious. Any help appreciated.
推荐答案
好吧,似乎还没有实现(或文档化?).我很确定这将很快添加.现在,你可以像这样使用一个肮脏的黑客:
Well, seems like it has not been implemented (or documented?) yet. I'm pretty sure this will be added soon. For now, you can use a dirty hack like so:
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)))
此外,如果您想多次使用此技巧,您可以为它创建一个简写.
Furthermore, if you would like to use this hack multiple times, you can create a shorthand for it.
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 轴标签,修复下面提到的重叠.
Now you can set both the main title and the x axis label simultaneously, fixing the overlap mentioned below.
这篇关于向 ggvis 添加情节标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!