R 将绘图标题添加到ggvis
我想给ggvis情节添加一个标题。我到处找不到一个例子。使用其他R图很简单,例如R 将绘图标题添加到ggvis,r,title,ggvis,R,Title,Ggvis,我想给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
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??
库(ggplot2)
图书馆(ggvis)
x嗯,似乎还没有实施(或记录在案?)。我很确定这会很快被添加。现在,您可以使用如下脏黑客:
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”,orient=“top”,记号=0,标题=title,
属性=轴/支柱(
轴=列表(笔划=“白色”),
标签=列表(fontSize=0)
), ...)
}
df%%>%ggvis(~x,~y)%%>%layer_points()%%>%add_title()#与之前相同
df%%>%ggvis(~x,~y)%%>%layer\u points()%%>%add\u title(title=“Hello”,x\u lab=“ton”)
编辑:
现在,您可以同时设置主标题和x轴标签,修复下面提到的重叠。此外,要更改标题的字体大小,请使用以下代码(改编自@tonytonov的答案):
添加标题%
添加_轴(“x”,orient=“top”,记号=0,标题=title,
属性=轴/支柱(
轴=列表(笔划=“白色”),
标题=列表(字体大小=32),
标签=列表(fontSize=0)
), ...)
}
我更新了@tonytonov的答案,使其不干扰默认的X轴。它仍然实现为轴,但实现了自己的“标题”比例,并将用户提供的标题属性(如fontsize和color)与使轴不可见所需的默认属性正确合并。此版本在不假定特定背景颜色的情况下隐藏轴。
函数后面有三个示例
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)))
库(ggvis)
#ggvis缺少plot title函数,所以添加一个。
#基于tonytonov的聪明黑客
# http://stackoverflow.com/a/25030002/1135316
添加标题谢谢Tony。有时黑客可能会比他们的价值更麻烦,但这是整洁!不客气!当一个更优雅的本机解决方案出现时,我会尽量记住更新这篇文章。不幸的是,这种黑客行为与在实际的x轴上设置标签不兼容。下图显示,使用add_title()时,x轴标签与原始标签“wt”重叠。看起来像是ggvis里的一只虫子mtcars%%>%ggvis(~wt,~mpg)%%>%layer_points()%%>%add_axis(“x”,title=“long axis label”)%%>%add_title(title=“Any title”)
@ChrisWarth尝试一下我建议的修复方法,似乎可以完成这项工作。@dsh尽管您建议的编辑被审阅者拒绝,但实际上还是很有帮助的。我更新了答案并确保它能工作,尽管我省略了类型=
规范,因为它是多余的(由?add_axis
中的第一个示例支持)。不管怎样,我只是想感谢你的努力和有意义的编辑解释。
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)))