在条形图中应用升序排列到分组内部。

huangapple go评论63阅读模式
英文:

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 &lt;- tribble(
  ~Country, ~Region, ~var,
  &quot;Jordan&quot;, &quot;MENA&quot;, 42,
  &quot;Iran&quot;, &quot;MENA&quot;, 26,
  &quot;Turkey&quot;, &quot;ECA&quot;, 37,
  &quot;Pakistan&quot;, &quot;AP&quot;, 42,
  &quot;Indonesia&quot;, &quot;AP&quot;, 84,
  &quot;Bangladesh&quot;, &quot;AP&quot;, 24,
  &quot;Israel&quot;, &quot;MENA&quot;, 65
)

dat %&gt;%
  mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x, 
                                                            levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;), 
                                                            ordered = TRUE))) %&gt;%
  ggplot(aes(x = as.factor(Country), y = var, fill = Region)) +
  geom_bar(stat=&quot;identity&quot;, position = &quot;dodge&quot;)

在条形图中应用升序排列到分组内部。

答案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 %&gt;%
  mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x, 
                                                            levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;), 
                                                            ordered = TRUE))) %&gt;%
  ggplot(aes(x = factor(Country, levels = unique(Country[order(Region, var)]), ordered = TRUE), y = var, fill = Region)) +
  geom_bar(stat=&quot;identity&quot;, position = &quot;dodge&quot;)

在条形图中应用升序排列到分组内部。<!-- -->

<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 %&gt;%
  mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x, 
                                                            levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;), 
                                                            ordered = TRUE))) %&gt;%
  ggplot(aes(x = fct_reorder(Country,var) %&gt;% 
                 fct_reorder(Region, .fun = sort), 
             y = var, fill = Region)) +
  geom_bar(stat=&quot;identity&quot;, position = &quot;dodge&quot;)

Plot output

huangapple
  • 本文由 发表于 2023年6月26日 18:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定