[英]ggplot y-axis labels should be in alphabetical order when values are tied
我有一個與這篇文章類似的問題,但我想知道是否有可能兩者都1.保持我的 y 軸值相同的順序,從最低到最高 -> aes(y = reorder(species, -sal, FUN = median), x = sal))
和2.在值綁定時更改 y 軸標簽的默認順序; 此解決方案不能解決問題: scale_y_discrete(limits=rev)
。 目前,如果值是綁定的,則 y 軸標簽 go 以相反的字母順序排列。 這通常不會有問題,但我希望標簽與相應的樣本大小注釋相匹配。 當值綁定時,這會混淆標簽的正確順序。 每個 y 軸的樣本大小 label 在我的真實 dataframe 中有很大不同,我只是不知道如何在示例 df 中更改它。
“E”和“D”不再按字母順序排列,值是綁定的。 當值綁定時,順序應為 A、B、D、E、C(如 pivot 表)。
set.seed(123)
df <- data.frame(
species = LETTERS[seq(from = 1, to = 5)],
sal = round(rnorm(n = 5, mean = 27, sd = .01), 2),
num = sample(x = 1:20, size = 20, replace = F)
)
pivot_df <- df %>%
group_by(species) %>%
summarize(n = n(), median_sal = median(sal, na.rm = T)) %>%
arrange(median_sal)
ggplot(
data = subset(df, !is.na(sal)),
aes(y = reorder(species, -sal, FUN = median), x = sal)
) +
geom_boxplot(outlier.shape = 1, outlier.size = 1, orientation = "y") +
coord_cartesian(clip = "off") +
annotation_custom(grid::textGrob(pivot_df$n,
x = 1.035,
y = c(0.89, 0.70, 0.51, 0.32, 0.13),
gp = grid::gpar(cex = 0.6)
)) +
annotation_custom(grid::textGrob(expression(bold(underline("N"))),
x = 1.035,
y = 1.02,
gp = grid::gpar(cex = 0.7)
)) +
ylab("") + xlab("") +
theme(
axis.text.y = element_text(size = 7, face = "italic"),
axis.text.x = element_text(size = 7),
axis.title.x = element_text(size = 9, face = "bold"),
axis.line = element_line(colour = "black"),
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 1),
panel.grid.major = element_line(colour = "#E0E0E0"),
plot.title = element_text(hjust = 0.5),
plot.margin = margin(21, 40, 20, 20))
一種選擇是根據pivot_df
中的順序設置species
順序,而不是使用reorder
。 為此,我首先使用forcats::fct_inorder(species)
將pivot_df$species
轉換為一個factor
。 之后,我使用factor(species, levels = rev(levels(pivot_df$species))
將df$species
轉換為一個因子:
library(dplyr)
library(ggplot2)
pivot_df <- df %>%
group_by(species) %>%
summarize(n = n(), median_sal = median(sal, na.rm = T)) %>%
arrange(median_sal) |>
mutate(species = forcats::fct_inorder(species))
ggplot(
data = subset(df, !is.na(sal)),
aes(y = factor(species, levels = rev(levels(pivot_df$species))), x = sal)
) +
geom_boxplot(outlier.shape = 1, outlier.size = 1, orientation = "y") +
coord_cartesian(clip = "off") +
annotation_custom(grid::textGrob(pivot_df$n,
x = 1.035,
y = c(0.89, 0.70, 0.51, 0.32, 0.13),
gp = grid::gpar(cex = 0.6)
)) +
annotation_custom(grid::textGrob(expression(bold(underline("N"))),
x = 1.035,
y = 1.02,
gp = grid::gpar(cex = 0.7)
)) +
ylab("") + xlab("") +
theme(
axis.text.y = element_text(size = 7, face = "italic"),
axis.text.x = element_text(size = 7),
axis.title.x = element_text(size = 9, face = "bold"),
axis.line = element_line(colour = "black"),
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 1),
panel.grid.major = element_line(colour = "#E0E0E0"),
plot.title = element_text(hjust = 0.5),
plot.margin = margin(21, 40, 20, 20))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.