如何在 facet_wrap 的每个栏中添加单独的文本?
How to add separate text onto each bar in facet wrap?
我正在尝试为每个条添加单独的文本以显示统计显着性。有人可以告诉我如何在 facet 的每个栏上添加单独的文本吗?另外,为什么最后一个面上的 a 轴文本变短了?我还要感谢以前的代码改进贡献者。
这是我的代码:
library(viridis)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)
df <- data.frame(
H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
names = c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))
df <- df %>%
pivot_longer(starts_with("H"), names_to = "h.names")
#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")
#add text onto each bar
df <- df %>%
arrange(desc(names)) %>%
group_by(names) %>%
mutate(
bar_labels = case_when(
names == "Ver" ~ "ab",
names == "Valery" ~ "e",
names == "Rio" ~ "a",
names == "RB" ~ "d",
names == "Purple" ~ "cd",
names == "POR12PG28-3" ~ "ab",
names == "Masquerade" ~ "ab",
names == "CO99076-6R" ~ "e",
names == "CO05068-1RU" ~ "c",
names == "Canela" ~ "ab",
names == "Atl" ~ "b",
names == "AC99330-1P/Y" ~ "ab",
TRUE ~ as.character(NA)
))
ggplot(data = df, mapping = aes(x = names, y = value)) +
geom_col(position = "dodge") +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
geom_text(aes(label = bar_labels, hjust = 0))
首先,我获取了数据的一个子集,以便对我想要绘制的每个条形图进行一次观察,并用 important_label
标记。当我们只查看输入图中的变量时,似乎所讨论的数据集每行都有 6 个相同的副本:h.names
、names
和 value
.
您可以通过在 OP 中将 geom_col(position = "dodge") +
替换为 geom_jitter() +
或 运行 df %>% count(h.names, names, value)
.[ 来查看此复制=23=]
df2 <- df %>%
distinct(h.names, names, value) %>%
mutate(custom_label = 1:n())
然后我用标有 custom_label
的 geom_text
图层绘制了条形图。为了防止最右边的轴文本被截断,我更改了主题中轴文本的水平对齐方式,将前三个值设为 0.5(居中),最后一个标签大部分右对齐。调整口味。或者,您可以在整个图上添加一些右边距,我认为 theme(plot.margin = ...
.
ggplot(data = df2, mapping = aes(x = names, y = value)) +
geom_col(position = "dodge") +
geom_text(aes(label = custom_label), hjust = -0.2) +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
theme(axis.text.x = element_text(hjust = c(rep(0.5,3), 0.8)))
我正在尝试为每个条添加单独的文本以显示统计显着性。有人可以告诉我如何在 facet 的每个栏上添加单独的文本吗?另外,为什么最后一个面上的 a 轴文本变短了?我还要感谢以前的代码改进贡献者。 这是我的代码:
library(viridis)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)
df <- data.frame(
H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
names = c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))
df <- df %>%
pivot_longer(starts_with("H"), names_to = "h.names")
#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")
#add text onto each bar
df <- df %>%
arrange(desc(names)) %>%
group_by(names) %>%
mutate(
bar_labels = case_when(
names == "Ver" ~ "ab",
names == "Valery" ~ "e",
names == "Rio" ~ "a",
names == "RB" ~ "d",
names == "Purple" ~ "cd",
names == "POR12PG28-3" ~ "ab",
names == "Masquerade" ~ "ab",
names == "CO99076-6R" ~ "e",
names == "CO05068-1RU" ~ "c",
names == "Canela" ~ "ab",
names == "Atl" ~ "b",
names == "AC99330-1P/Y" ~ "ab",
TRUE ~ as.character(NA)
))
ggplot(data = df, mapping = aes(x = names, y = value)) +
geom_col(position = "dodge") +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
geom_text(aes(label = bar_labels, hjust = 0))
首先,我获取了数据的一个子集,以便对我想要绘制的每个条形图进行一次观察,并用 important_label
标记。当我们只查看输入图中的变量时,似乎所讨论的数据集每行都有 6 个相同的副本:h.names
、names
和 value
.
您可以通过在 OP 中将 geom_col(position = "dodge") +
替换为 geom_jitter() +
或 运行 df %>% count(h.names, names, value)
.[ 来查看此复制=23=]
df2 <- df %>%
distinct(h.names, names, value) %>%
mutate(custom_label = 1:n())
然后我用标有 custom_label
的 geom_text
图层绘制了条形图。为了防止最右边的轴文本被截断,我更改了主题中轴文本的水平对齐方式,将前三个值设为 0.5(居中),最后一个标签大部分右对齐。调整口味。或者,您可以在整个图上添加一些右边距,我认为 theme(plot.margin = ...
.
ggplot(data = df2, mapping = aes(x = names, y = value)) +
geom_col(position = "dodge") +
geom_text(aes(label = custom_label), hjust = -0.2) +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
theme(axis.text.x = element_text(hjust = c(rep(0.5,3), 0.8)))