首页 > 解决方案 > 如何在ggplot中按百分比排列图例并将其他人留在最后

问题描述

我想按百分比排列图例并将其他留在最后,到目前为止我得到了这个:

在此处输入图像描述

我尝试使用重新排序功能,但没有成功。

这是我的代码:

library(dplyr)
library(ggplot2)
library(ggrepel)
library(forcats)
library(scales)
library(ggplot2) 
library(grid)
library(gridExtra) 

figvad <- read.csv(url("https://raw.githubusercontent.com/learnseq/learning/main/vadev.txt"),sep = '\t',header = TRUE)


legend233 <-ggplot(melt(table(figvad$Type)) %>% 
         mutate(perc = value/sum(value)) %>%
         mutate(label = paste0(Var1, "\n", value, " (", 
                               scales::percent(perc), ")")),
       aes(label, value, fill = label)) + 
  geom_col(position = "stack", color = "gray20", alpha = 0.4, width = 0.5) +
   coord_polar(theta = "x", clip = "off") +
  theme_minimal()+
  theme(panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(5, "cm"))),
       axis.text.y = element_text(face="italic", size=8, 
                                 color="black"),
       legend.justification = c(0, 0),
         legend.direction = "vertical",
         legend.key = element_rect(size = 0.01,fill='white'),
         legend.title = element_blank(),
         axis.text.x = element_blank(),
         legend.key.height = unit(2, "cm"),
         legend.key.width = unit(2, "cm"),
         legend.key.size = unit(0.05, "lines"),
       legend.text=element_text(size=20, face="bold")) +
  xlab("Types of Vaccines") +
  ylab("")

legend <- cowplot::get_legend(legend233)

grid.newpage()
grid.draw(legend)

很抱歉我没有早点上传数据。

标签: rggplot2

解决方案


您需要将标签制作成因子,然后图例的顺序将遵循因子级别的顺序。您可以分两步完成:

  1. label使用一个因子perc对级别进行排序。
  2. 用于forcats::fct_reorder将“其他”级别移至最后。

见下文,我添加了两个mutate来完成工作:

legend233 <-ggplot(melt(table(figvad$Type)) %>% 
                     mutate(perc = value/sum(value)) %>%
                     mutate(label = paste0(Var1, "\n", value, " (", 
                                           scales::percent(perc), ")")) %>%
                     mutate(label = factor(label,levels = label[order(-perc)])) %>% # Order by perc
                     mutate(label = forcats::fct_relevel( # Move the label associated with "Other" to last
                       label, 
                       as.character(label[grepl(label, pattern = "Other")]), 
                       after = Inf)),
                   aes(label, value, fill = label)) + 
  geom_col(position = "stack", color = "gray20", alpha = 0.4, width = 0.5) +
  coord_polar(theta = "x", clip = "off") +
  theme_minimal()+
  theme(panel.grid = element_blank(), 
        axis.text.y.right = element_text(margin = margin(l = unit(5, "cm"))),
        axis.text.y = element_text(face="italic", size=8, 
                                   color="black"),
        legend.justification = c(0, 0),
        legend.direction = "vertical",
        legend.key = element_rect(size = 0.01,fill='white'),
        legend.title = element_blank(),
        axis.text.x = element_blank(),
        legend.key.height = unit(2, "cm"),
        legend.key.width = unit(2, "cm"),
        legend.key.size = unit(0.05, "lines"),
        legend.text=element_text(size=20, face="bold")) +
  xlab("Types of Vaccines") +
  ylab("")

legend <- cowplot::get_legend(legend233)
grid.newpage()
grid.draw(legend)

推荐阅读