底部和顶部绘图标签 facet ggplot2
Bottom and top plot labels facet ggplot2
数据结构:假设你有 2 组变量,每组有 3 个子级别。我们将它们命名为 A1、A2、A3(A 组)、B1、B2、B3(B 组)。都是5-25不等的整型。每个变量都有'A1Lower / A1Upper'等形式的描述。
数据集和包:您可以使用以下代码生成数据集:
library(dplyr)
library(ggplot2)
# We will need subscales later
subscales <- paste0(rep(c('A', 'B'), each = 3), 1:3)
# Dataset
data <- replicate(6, sample(5:25, 500, replace = T)) %>%
as_tibble() %>%
setNames(subscales)
我根据描述创建了 facet_wrap
of geom_violion
和 geom_boxplot()
标签(自定义标签),代码如下:
subscales_labeller <- paste0(subscales, 'Lower / ', subscales, 'Upper') %>%
setNames(subscales) %>% #as a named vector
as_labeller() #as a ggplot2 labeller
data %>%
gather(subscale, value, factor_key = T) %>%
ggplot(aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
facet_wrap(~subscale, ncol = 3, labeller = subscales_labeller) +
theme_minimal() +
labs(x = '', y = '')
目标: 问题是整个标签都在最上面。我想要小提琴情节上方的上层描述和下方的下层描述。到目前为止,我设法将 x 刻度设置为具有下部描述的字符和仅具有上部描述的贴标机。它并不完美,因为它使我的很多事情变得复杂(例如,我想使用 color = Sex
美学),这就是为什么我更愿意一次设置底部和顶部标签之类的东西。更不用说带有 'A group' 和 'B group' 描述的旁白(这就像天堂,但不是必须的 post)
此答案是作为对 OP 对使用 geom_text()
标记 upper/lower 边界的多面图版本的评论的回应而发布的。
我做了两个版本,一个是静态代码,一个是自适应的。我还冒昧地切换到 facet_grid()
以适应 OP 表示感兴趣的侧标签。下面是静态版本,假设 data
来自您发布的代码:
df <- data %>% gather(subscale, value, factor_key = T) %>%
mutate(letter = paste("Group", substr(subscale, 1, 1)),
number = substr(subscale, 2, 2))
ggplot(df, aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
geom_text(data = data.frame(value = c(max(df$value), min(df$value)),
label = c("Upper", "Lower")),
aes(label = label),
nudge_y = c(1, -1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()
在这种情况下,"Upper" 和 "Lower" 将始终是一个 y-axis 单位 above/below 整个数据的极值。您可能需要调整 nudge_y
以适合您的地块大小。
以下代码段将根据组 maximum/minimum 自适应地放置标签。为了说明这一点,我抛出了一些观察结果以表明标签放置正确。
df <- df[!(df$subscale == "A2" & df$value > 15),]
ggplot(df, aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
geom_text(aes(label = "Upper"), stat = "summary", fun.y = "max",
position = position_nudge(y = 1)) +
geom_text(aes(label = "Lower"), stat = "summary", fun.y = "min",
position = position_nudge(y = -1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()
编辑:每个方面、性别和年龄都有自定义标签:
labs <- expand.grid(value = range(df$value),
letter = unique(df$letter),
number = unique(df$number))
labs$label <- c("Sweet", "Sour", "Introvert", "Extrovert",
"Idiot", "Genius", "Dark", "Light",
"Small", "Big", "Tom-eh-to", "Tom-ah-to")
df$sex <- sample(c("F", "M"), size = nrow(df), replace = TRUE)
df$age <- sample(c("<20", "20-30", ">30"), size = nrow(df), replace = TRUE)
ggplot(df, aes(x = sex, y = value)) +
geom_violin(aes(fill = age)) +
geom_boxplot(aes(fill = age), width = 0.1,
position = position_dodge(0.9)) +
geom_text(data = labs,
aes(x = 1.5, label = label),
nudge_y = c(-1, 1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()
数据结构:假设你有 2 组变量,每组有 3 个子级别。我们将它们命名为 A1、A2、A3(A 组)、B1、B2、B3(B 组)。都是5-25不等的整型。每个变量都有'A1Lower / A1Upper'等形式的描述。
数据集和包:您可以使用以下代码生成数据集:
library(dplyr)
library(ggplot2)
# We will need subscales later
subscales <- paste0(rep(c('A', 'B'), each = 3), 1:3)
# Dataset
data <- replicate(6, sample(5:25, 500, replace = T)) %>%
as_tibble() %>%
setNames(subscales)
我根据描述创建了 facet_wrap
of geom_violion
和 geom_boxplot()
标签(自定义标签),代码如下:
subscales_labeller <- paste0(subscales, 'Lower / ', subscales, 'Upper') %>%
setNames(subscales) %>% #as a named vector
as_labeller() #as a ggplot2 labeller
data %>%
gather(subscale, value, factor_key = T) %>%
ggplot(aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
facet_wrap(~subscale, ncol = 3, labeller = subscales_labeller) +
theme_minimal() +
labs(x = '', y = '')
目标: 问题是整个标签都在最上面。我想要小提琴情节上方的上层描述和下方的下层描述。到目前为止,我设法将 x 刻度设置为具有下部描述的字符和仅具有上部描述的贴标机。它并不完美,因为它使我的很多事情变得复杂(例如,我想使用 color = Sex
美学),这就是为什么我更愿意一次设置底部和顶部标签之类的东西。更不用说带有 'A group' 和 'B group' 描述的旁白(这就像天堂,但不是必须的 post)
此答案是作为对 OP 对使用 geom_text()
标记 upper/lower 边界的多面图版本的评论的回应而发布的。
我做了两个版本,一个是静态代码,一个是自适应的。我还冒昧地切换到 facet_grid()
以适应 OP 表示感兴趣的侧标签。下面是静态版本,假设 data
来自您发布的代码:
df <- data %>% gather(subscale, value, factor_key = T) %>%
mutate(letter = paste("Group", substr(subscale, 1, 1)),
number = substr(subscale, 2, 2))
ggplot(df, aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
geom_text(data = data.frame(value = c(max(df$value), min(df$value)),
label = c("Upper", "Lower")),
aes(label = label),
nudge_y = c(1, -1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()
在这种情况下,"Upper" 和 "Lower" 将始终是一个 y-axis 单位 above/below 整个数据的极值。您可能需要调整 nudge_y
以适合您的地块大小。
以下代码段将根据组 maximum/minimum 自适应地放置标签。为了说明这一点,我抛出了一些观察结果以表明标签放置正确。
df <- df[!(df$subscale == "A2" & df$value > 15),]
ggplot(df, aes(x = '', y = value)) +
geom_violin() +
geom_boxplot(width = 0.1) +
geom_text(aes(label = "Upper"), stat = "summary", fun.y = "max",
position = position_nudge(y = 1)) +
geom_text(aes(label = "Lower"), stat = "summary", fun.y = "min",
position = position_nudge(y = -1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()
编辑:每个方面、性别和年龄都有自定义标签:
labs <- expand.grid(value = range(df$value),
letter = unique(df$letter),
number = unique(df$number))
labs$label <- c("Sweet", "Sour", "Introvert", "Extrovert",
"Idiot", "Genius", "Dark", "Light",
"Small", "Big", "Tom-eh-to", "Tom-ah-to")
df$sex <- sample(c("F", "M"), size = nrow(df), replace = TRUE)
df$age <- sample(c("<20", "20-30", ">30"), size = nrow(df), replace = TRUE)
ggplot(df, aes(x = sex, y = value)) +
geom_violin(aes(fill = age)) +
geom_boxplot(aes(fill = age), width = 0.1,
position = position_dodge(0.9)) +
geom_text(data = labs,
aes(x = 1.5, label = label),
nudge_y = c(-1, 1)) +
facet_grid(letter ~ number) +
labs(x = "", y = "") +
theme_minimal()