英文:
How to apply ascending order within groups in bar plots
问题
我有一个名为 `dat` 的数据框,我想以每个地区从低到高排序的方式绘制按地区分组的条形图。我已经查看了其他类似的问题,但找不到解决我的问题的方法。如果有人能指导我如何处理,我将不胜感激。
dat <- tribble(
~Country, ~Region, ~var,
"Jordan", "MENA", 42,
"Iran", "MENA", 26,
"Turkey", "ECA", 37,
"Pakistan", "AP", 42,
"Indonesia", "AP", 84,
"Bangladesh", "AP", 24,
"Israel", "MENA", 65
)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = as.factor(Country), y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
英文:
I have data frame like dat
, and I wanted to graph region-wise grouped bar plots in a way that each region is ordered from low to high. I have checked other similar questions but I couldn’t find a solution to my problem. Would appreciate if someone could advise me how to handle it.
dat <- tribble(
~Country, ~Region, ~var,
"Jordan", "MENA", 42,
"Iran", "MENA", 26,
"Turkey", "ECA", 37,
"Pakistan", "AP", 42,
"Indonesia", "AP", 84,
"Bangladesh", "AP", 24,
"Israel", "MENA", 65
)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = as.factor(Country), y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
答案1
得分: 1
你可以根据你的地区内因子的级别顺序来对因子进行order
操作,代码如下:
library(tidyverse)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = factor(Country, levels = unique(Country[order(Region, var)]), ordered = TRUE), y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
创建于2023-06-26,使用 reprex v2.0.2
英文:
You could order
the factor
within your region based on the levels
order of your factor like this:
library(tidyverse)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = factor(Country, levels = unique(Country[order(Region, var)]), ordered = TRUE), y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
<!-- -->
<sup>Created on 2023-06-26 with reprex v2.0.2</sup>
答案2
得分: 0
另一种方法是使用forcats的fct_reorder()
函数,首先按变量(var)排序,然后按地区(Region)排序,代码如下:
library(tidyverse)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = fct_reorder(Country, var) %>%
fct_reorder(Region, .fun = sort),
y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
英文:
An alternative would be using forcats fct_reorder()
function to sort first by var and then by Region like this:
library(tidyverse)
dat %>%
mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
levels = c("AP", "ECA", "MENA"),
ordered = TRUE))) %>%
ggplot(aes(x = fct_reorder(Country,var) %>%
fct_reorder(Region, .fun = sort),
y = var, fill = Region)) +
geom_bar(stat="identity", position = "dodge")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论