r - 如何在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)
很抱歉我没有早点上传数据。
解决方案
您需要将标签制作成因子,然后图例的顺序将遵循因子级别的顺序。您可以分两步完成:
label
使用一个因子perc
对级别进行排序。- 用于
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)
推荐阅读
- sql - 以 yyyy-mm-dd 格式获取上一年第一个月的第一天
- python - 如何优化python列表中的比较组合
- java - 使用 mongoDB 的 Spring Boot 返回 NullPointerException
- excel - 无法识别日期
- javascript - PHP $_GET 值未从 jQuery AJAX 调用中提取 - PHP 包含问题?
- mysql - withCount() - 获得 2 个计数的差值
- php - 如何使用 REST API 为 WooCommerce 产品指定产品图像排序顺序?
- python - Pyinstaller 生成空 dist 文件夹
- javascript - 在 React Native 上调节按钮
- python - 将多个 Vega/vincent 图表添加到 Folium 弹出窗口