Currently, I have to hard code the labels within scale_x_discrete()
but I need to dynamically create a subset of them and have them in descending order with additional labels added to the bottom of the y-axis that have static locations.
目前,我必须硬编码scale_x_discrete()中的标签,但我需要动态创建它们的子集,并将它们按照降序添加到y轴底部的具有静态位置的附加标签中。
library(dplyr)
library(ggplot2)
library(scales)
set.seed(1)
samp <- data.frame(Names = sample(paste0("Sample_", 1:5), 90, replace = TRUE),
Mode = rep(c("UN", "CN", "CF"), 150))
ggplot(samp, aes(x=Names)) +
geom_bar(aes(fill=Mode), position="fill", width = .5) +
scale_fill_manual(values = c("#a0de6f","tan1","#DAEBFF")) +
scale_y_continuous(label=percent, expand = c(0,0)) +
coord_flip() +
labs(fill="",
x="",
y="") +
theme_minimal() +
theme(
legend.position = "bottom",
plot.margin = unit(c(1,1,.5,0),"cm"),
legend.key.size = unit(.5, "lines")
) +
guides(fill = guide_legend(reverse = TRUE)) +
scale_x_discrete("",
labels= c(
bquote("Sample_5" * " (n = " * .(sum(samp$Names == "Sample_5")) * ")"),
bquote(italic("Sample_1") * " (n = " * .(sum(samp$Names == "Sample_1")) * ")"),
bquote(italic("Sample_3") * " (n = " * .(sum(samp$Names == "Sample_3")) * ")"),
bquote(italic("Sample_4") * " (n = " * .(sum(samp$Names == "Sample_4")) * ")"),
bquote(italic("Sample_2") * " (n = " * .(sum(samp$Names == "Sample_2")) * ")")))
This bit:
这一点:
scale_x_discrete("",
labels= c(
bquote("Sample_5" * " (n = " * .(sum(samp$Names == "Sample_5")) * ")"),
bquote(italic("Sample_1") * " (n = " * .(sum(samp$Names == "Sample_1")) * ")"),
bquote(italic("Sample_3") * " (n = " * .(sum(samp$Names == "Sample_3")) * ")"),
bquote(italic("Sample_4") * " (n = " * .(sum(samp$Names == "Sample_4")) * ")"),
bquote(italic("Sample_2") * " (n = " * .(sum(samp$Names == "Sample_2")) * ")")))
Is what I need to be dynamically created and put in descending order by count but I don't really know how.
是我需要动态创建的,按计数降序排列,但我不知道怎么做。
How it should look:
应该:
1 个解决方案
#1
3
You can generate the list of labels separately for your dynamic part and your static part and then combine them
您可以为您的动态部分和静态部分分别生成标签列表,然后将它们组合在一起
excludes <- "Sample_5"
cat_counts <- table(samp$Names) %>% {.[-match(excludes, names(.))]}
my_sorted_labels <- Map(function(label, count) {
bquote(italic(.(label)) * " (n = " * .(count) * ")")
}, names(cat_counts), cat_counts)[order(cat_counts)]
my_sorted_limits <- names(sort(cat_counts))
my_fixed_labels <- Map(function(label, count) {
bquote(.(label) * " (n = " * .(count) * ")")
}, excludes, table(samp$Names)[excludes])
my_labels <- c(my_fixed_labels, my_sorted_labels)
my_limits <- c(excludes, my_sorted_limits)
and then use that with
然后用它
scale_x_discrete("", limits=my_limits, labels= my_labels)
#1
3
You can generate the list of labels separately for your dynamic part and your static part and then combine them
您可以为您的动态部分和静态部分分别生成标签列表,然后将它们组合在一起
excludes <- "Sample_5"
cat_counts <- table(samp$Names) %>% {.[-match(excludes, names(.))]}
my_sorted_labels <- Map(function(label, count) {
bquote(italic(.(label)) * " (n = " * .(count) * ")")
}, names(cat_counts), cat_counts)[order(cat_counts)]
my_sorted_limits <- names(sort(cat_counts))
my_fixed_labels <- Map(function(label, count) {
bquote(.(label) * " (n = " * .(count) * ")")
}, excludes, table(samp$Names)[excludes])
my_labels <- c(my_fixed_labels, my_sorted_labels)
my_limits <- c(excludes, my_sorted_limits)
and then use that with
然后用它
scale_x_discrete("", limits=my_limits, labels= my_labels)