只是想分享一种我通常用来处理这个问题的技术,即为脚本和漂亮打印的因子变量的级别使用不同的名称:
# Load packages
library(tidyverse)
library(sjlabelled)
library(patchwork)
# Create data frames
df <- data.frame(v = c(1, 2, 3), f = c("a", "b", "c"))
df_labelled <- data.frame(v = c(1, 2, 3), f = c("a", "b", "c")) %>%
val_labels(
# levels are characters
f = c(
"a" = "Treatment A: XYZ", "b" = "Treatment B: YZX",
"c" = "Treatment C: ZYX"
),
# levels are numeric
v = c("1" = "Exp. Unit 1", "2" = "Exp. Unit 2", "3" = "Exp. Unit 3")
)
# df and df_labelled appear exactly the same when printed and nothing changes
# in terms of scripting
df
#> v f
#> 1 1 a
#> 2 2 b
#> 3 3 c
df_labelled
#> v f
#> 1 1 a
#> 2 2 b
#> 3 3 c
# Now, let's take a look at the structure of df and df_labelled
str(df)
#> 'data.frame': 3 obs. of 2 variables:
#> $ v: num 1 2 3
#> $ f: chr "a" "b" "c"
str(df_labelled) # notice the attributes
#> 'data.frame': 3 obs. of 2 variables:
#> $ v: num 1 2 3
#> ..- attr(*, "labels")= Named num [1:3] 1 2 3
#> .. ..- attr(*, "names")= chr [1:3] "Exp. Unit 1" "Exp. Unit 2" "Exp. Unit 3"
#> $ f: chr "a" "b" "c"
#> ..- attr(*, "labels")= Named chr [1:3] "a" "b" "c"
#> .. ..- attr(*, "names")= chr [1:3] "Treatment A: XYZ" "Treatment B: YZX" "Treatment C: ZYX"
# Lastly, create ggplots with and without pretty names for factor levels
p1 <- df_labelled %>% # or, df
ggplot(aes(x = f, y = v)) +
geom_point() +
labs(x = "Treatment", y = "Measurement")
p2 <- df_labelled %>%
ggplot(aes(x = to_label(f), y = to_label(v))) +
geom_point() +
labs(x = "Treatment", y = "Experimental Unit")
p1 / p2

由reprex 包于 2021-08-17 创建 (v2.0.0 )