快速标签是否可以访问 ggplot 标题?
Expss labels are they accessible to ggplot title?
我希望从 Expss 变量标签向 ggplot 添加标题。 Expss package 创建了一个带有变量标签和值标签(带有底层数字)的新数据类型。是否有某种方法可以通过引用变量名并将其作为标题包含在 ggplot 中来访问分配的变量标签?同样,Expss table 标题是否可以来自变量标签?
一般来说,您可以使用var_lab
函数轻松获得变量标签:var_lab(my_dataframe$my_variable)
。在下面的示例中,您可以看到它如何与 table 标题和 ggplot:
一起使用
library(ggplot2)
library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (1000 lbs)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
# table with caption from label
cro_cpct(mtcars$am, mtcars$vs) %>% set_caption(var_lab(mtcars$am))
# ggplot with title from label
use_labels(mtcars, {
# '..data' is shortcut for all 'mtcars' data.frame inside expression
ggplot(..data) +
geom_point(aes(y = mpg, x = wt, color = qsec)) +
facet_grid(am ~ vs) +
ggtitle(var_lab(mpg))
})
我希望从 Expss 变量标签向 ggplot 添加标题。 Expss package 创建了一个带有变量标签和值标签(带有底层数字)的新数据类型。是否有某种方法可以通过引用变量名并将其作为标题包含在 ggplot 中来访问分配的变量标签?同样,Expss table 标题是否可以来自变量标签?
一般来说,您可以使用var_lab
函数轻松获得变量标签:var_lab(my_dataframe$my_variable)
。在下面的示例中,您可以看到它如何与 table 标题和 ggplot:
library(ggplot2)
library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (1000 lbs)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
# table with caption from label
cro_cpct(mtcars$am, mtcars$vs) %>% set_caption(var_lab(mtcars$am))
# ggplot with title from label
use_labels(mtcars, {
# '..data' is shortcut for all 'mtcars' data.frame inside expression
ggplot(..data) +
geom_point(aes(y = mpg, x = wt, color = qsec)) +
facet_grid(am ~ vs) +
ggtitle(var_lab(mpg))
})