在图标题中使用数据框变量名称 [英] Use dataframe variable names in plot titles
问题描述
我有一个带有多个变量的数据框,希望将它们标记出来,然后在多个ggplots中使用.我已使用labeller
包通过以下代码应用标签.
I have a dataframe with several variables that I wish to label and then use in several ggplots. I have applied labels using the labeller
package with the following code.
library(tidyverse)
library(labeller)
library(ggpubr)
example.df <- data.frame(
origin = sample(c("hum_1", "mou_1"), 100, replace = TRUE),
v1 = rnorm(100, 100, 5),
v2 = rnorm(100, 10,5),
v3 = rnorm (100, 25, 5))
example.df <- example.df %>% set_variable_labels(origin = "original sample", v1 = "effect of Rx", v2 = "response", v3 = "weight (kg)")
这将使标签显示在数据框中.但是,当我使用ggpubr
中的ggqqplot
绘制这些变量时,在结果图中看不到标签.
This gets the labels to show up in the dataframe. However, when I use ggqqplot
from ggpubr
to plot these variables I don't see the labels in the resultant plots.
vars <- dput(colnames(select_if(example.df, is.numeric)))
lapply(vars, function(item) {
ggqqplot(example.df, x=item, combine = FALSE, facet.by = "origin")+
ggtitle(item)
}
)
我想显示原始样本,rx 的效果和重量(kg),而不是v1,v2和v3.任何帮助深表感谢.谢谢.
I would like to have original sample, effect of rx and weight (kg) show up instead of v1, v2 and v3. Any help is much appreciated. Thanks.
推荐答案
您可以将名称赋予vars
,然后使用purrr
包中的map2()
或imap()
函数循环浏览它们.要包含上标/下标/数学符号,请同时使用expression()
和parse(text = ...)
(另请参见以下 example1 , example2 ).
You can give the names to vars
then use either map2()
or imap()
functions from the purrr
package to cycle through them. To include superscripts/subscripts/math notation, use expression()
together with parse(text = ...)
(see also these example1, example2).
names(vars) <- c(expression('effect of Rx'^{1}),
"response",
expression(weight/individual %.% kg[2])
)
vars
#> "effect of Rx"^{\n 1\n} response
#> "v1" "v2"
#> weight/individual %.% kg[2]
#> "v3"
### from purrr package
map2(vars, names(vars), ~ ggqqplot(example.df, x = .x, combine = FALSE, facet.by = "origin") +
ggtitle(parse(text = .y)))
# or `imap(x, ...)` which is short hand for map2(x, names(x), ...)
imap(vars, ~ ggqqplot(example.df, x = .x, combine = FALSE, facet.by = "origin") +
ggtitle(parse(text = .y)))
#> $`"effect of Rx"^{\n 1\n}`
#>
#> $response
#>
#> $`weight/individual %.% kg[2]`
由 reprex软件包(v0.2.1.9000)创建于2019-03-11
Created on 2019-03-11 by the reprex package (v0.2.1.9000)
这篇关于在图标题中使用数据框变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!