R ggplot2:突出显示堆叠条形图中的值
R ggplot2: Highlight Values in Stacked Barplot
我有一个名为 Participants10 的小数据框(来自 dput()
):
structure(list(AUC_numeric = c(0.59, 0.68, 0.57, 0.59, 0.74,
0.53, 0.63, 0.59, 0.62, 0.51, 0.78, 0.55, 0.5, 0.5, 0.61), AUC_Factor = structure(c(3L,
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("aEMA",
"pEMA", "fEMA"), class = "factor"), ParticipantNr = c(1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), lab_ypos = c(0.295,
0.93, 1.555, 0.295, 0.96, 1.595, 0.315, 0.925, 1.53, 0.255, 0.9,
1.565, 0.25, 0.75, 1.305)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list(
ParticipantNr = 1:5, .rows = list(1:3, 4:6, 7:9, 10:12, 13:15)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
我想使用 ggplot 创建一个堆积条形图,如下所示:
ggplot(data = Participants10, aes(x = ParticipantNr, y = AUC_numeric))+
geom_col(aes(fill = factor(AUC_Factor), width = 0.5)) +
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor) ), color = "white", parse = T)+
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
如何突出显示每个 ParticipantNR 的堆叠条中的最高值(例如使用颜色或粗体文本或周围边缘)?
一种方法是预先计算要突出显示的组。我将使用 dplyr
:
这里,我们设置highlight
到"yes"
,当它是每个ParticipantNr
的最大值时。然后我们可以使用 scale_color_manual
根据 highlight
变量更改文本颜色。
library(dplyr)
library(ggplot2)
Participants10 %>%
group_by(ParticipantNr) %>%
mutate(highligth = case_when(AUC_numeric == max(AUC_numeric) ~ "yes",
TRUE ~ "no")) %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), color = highligth),
width = 0.5) +
geom_text(aes(y = lab_ypos, label = AUC_numeric,
group = factor(AUC_Factor),color = highligth )) +
scale_color_manual(values = c("no" = "white", "yes" = "black"), guide = FALSE) +
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
不幸的是,由于条形区域之间的剪裁,轮廓选项看起来不太好。
Participants10 %>%
group_by(ParticipantNr) %>%
mutate(highligth = case_when(AUC_numeric == max(AUC_numeric) ~ "yes",
TRUE ~ "no")) %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), color = highligth), width = 0.5) +
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor),color = highligth ), parse = T) +
scale_color_manual(values = c("no" = "white", "yes" = "black"), guide = FALSE) +
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
可能有一个现有的包可能满足您的需要,但这是强调部分情节的一种方式。
您可以将要在 aes()
中使用的值(如本例中的大小和颜色)映射到您传递到 ggplot()
的数据框。
library(dplyr)
library(tibble)
Participants10 <- Participants10 %>%
mutate(ismax = AUC_numeric == max(AUC_numeric)) %>%
mutate(size = ifelse(ismax, 10, 4)) %>% #size for the text
mutate(isline = ifelse(ismax, "grey25", NA)) %>% #line around rectangle
mutate(line = ifelse(ismax, 1.5, NA)) #whether to show the line around the rectangle or not
您可以稍后在使用 scales_*_identity()
指定中断时使用它们,它采用您要映射的各个值的命名向量。
Participants10 %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), col = isline, size = line)) + #added col and size here
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor), size = size), color = "white")+
scale_size_identity(breaks = tibble::deframe(distinct(Participants10, ParticipantNr, size))) + #added scale for size
scale_color_identity(breaks = tibble::deframe(distinct(Participants10, ParticipantNr, line))) + #added scale for color
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
您可以根据输出的维度修改实际值。
我有一个名为 Participants10 的小数据框(来自 dput()
):
structure(list(AUC_numeric = c(0.59, 0.68, 0.57, 0.59, 0.74,
0.53, 0.63, 0.59, 0.62, 0.51, 0.78, 0.55, 0.5, 0.5, 0.61), AUC_Factor = structure(c(3L,
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("aEMA",
"pEMA", "fEMA"), class = "factor"), ParticipantNr = c(1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), lab_ypos = c(0.295,
0.93, 1.555, 0.295, 0.96, 1.595, 0.315, 0.925, 1.53, 0.255, 0.9,
1.565, 0.25, 0.75, 1.305)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list(
ParticipantNr = 1:5, .rows = list(1:3, 4:6, 7:9, 10:12, 13:15)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
我想使用 ggplot 创建一个堆积条形图,如下所示:
ggplot(data = Participants10, aes(x = ParticipantNr, y = AUC_numeric))+
geom_col(aes(fill = factor(AUC_Factor), width = 0.5)) +
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor) ), color = "white", parse = T)+
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
如何突出显示每个 ParticipantNR 的堆叠条中的最高值(例如使用颜色或粗体文本或周围边缘)?
一种方法是预先计算要突出显示的组。我将使用 dplyr
:
这里,我们设置highlight
到"yes"
,当它是每个ParticipantNr
的最大值时。然后我们可以使用 scale_color_manual
根据 highlight
变量更改文本颜色。
library(dplyr)
library(ggplot2)
Participants10 %>%
group_by(ParticipantNr) %>%
mutate(highligth = case_when(AUC_numeric == max(AUC_numeric) ~ "yes",
TRUE ~ "no")) %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), color = highligth),
width = 0.5) +
geom_text(aes(y = lab_ypos, label = AUC_numeric,
group = factor(AUC_Factor),color = highligth )) +
scale_color_manual(values = c("no" = "white", "yes" = "black"), guide = FALSE) +
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
不幸的是,由于条形区域之间的剪裁,轮廓选项看起来不太好。
Participants10 %>%
group_by(ParticipantNr) %>%
mutate(highligth = case_when(AUC_numeric == max(AUC_numeric) ~ "yes",
TRUE ~ "no")) %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), color = highligth), width = 0.5) +
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor),color = highligth ), parse = T) +
scale_color_manual(values = c("no" = "white", "yes" = "black"), guide = FALSE) +
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
可能有一个现有的包可能满足您的需要,但这是强调部分情节的一种方式。
您可以将要在 aes()
中使用的值(如本例中的大小和颜色)映射到您传递到 ggplot()
的数据框。
library(dplyr)
library(tibble)
Participants10 <- Participants10 %>%
mutate(ismax = AUC_numeric == max(AUC_numeric)) %>%
mutate(size = ifelse(ismax, 10, 4)) %>% #size for the text
mutate(isline = ifelse(ismax, "grey25", NA)) %>% #line around rectangle
mutate(line = ifelse(ismax, 1.5, NA)) #whether to show the line around the rectangle or not
您可以稍后在使用 scales_*_identity()
指定中断时使用它们,它采用您要映射的各个值的命名向量。
Participants10 %>%
ggplot(aes(x = ParticipantNr, y = AUC_numeric)) +
geom_col(aes(fill = factor(AUC_Factor), col = isline, size = line)) + #added col and size here
geom_text(aes(y = lab_ypos, label = AUC_numeric, group = factor(AUC_Factor), size = size), color = "white")+
scale_size_identity(breaks = tibble::deframe(distinct(Participants10, ParticipantNr, size))) + #added scale for size
scale_color_identity(breaks = tibble::deframe(distinct(Participants10, ParticipantNr, line))) + #added scale for color
theme(legend.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()) +
coord_flip()
您可以根据输出的维度修改实际值。