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

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

How to apply ascending order within groups in bar plots

问题

  1. 我有一个名为 `dat` 的数据框,我想以每个地区从低到高排序的方式绘制按地区分组的条形图。我已经查看了其他类似的问题,但找不到解决我的问题的方法。如果有人能指导我如何处理,我将不胜感激。
  2. dat <- tribble(
  3. ~Country, ~Region, ~var,
  4. "Jordan", "MENA", 42,
  5. "Iran", "MENA", 26,
  6. "Turkey", "ECA", 37,
  7. "Pakistan", "AP", 42,
  8. "Indonesia", "AP", 84,
  9. "Bangladesh", "AP", 24,
  10. "Israel", "MENA", 65
  11. )
  12. dat %>%
  13. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  14. levels = c("AP", "ECA", "MENA"),
  15. ordered = TRUE))) %>%
  16. ggplot(aes(x = as.factor(Country), y = var, fill = Region)) +
  17. 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.

  1. dat &lt;- tribble(
  2. ~Country, ~Region, ~var,
  3. &quot;Jordan&quot;, &quot;MENA&quot;, 42,
  4. &quot;Iran&quot;, &quot;MENA&quot;, 26,
  5. &quot;Turkey&quot;, &quot;ECA&quot;, 37,
  6. &quot;Pakistan&quot;, &quot;AP&quot;, 42,
  7. &quot;Indonesia&quot;, &quot;AP&quot;, 84,
  8. &quot;Bangladesh&quot;, &quot;AP&quot;, 24,
  9. &quot;Israel&quot;, &quot;MENA&quot;, 65
  10. )
  11. dat %&gt;%
  12. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  13. levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;),
  14. ordered = TRUE))) %&gt;%
  15. ggplot(aes(x = as.factor(Country), y = var, fill = Region)) +
  16. geom_bar(stat=&quot;identity&quot;, position = &quot;dodge&quot;)

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

答案1

得分: 1

你可以根据你的地区内因子的级别顺序来对因子进行order操作,代码如下:

  1. library(tidyverse)
  2. dat %>%
  3. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  4. levels = c("AP", "ECA", "MENA"),
  5. ordered = TRUE))) %>%
  6. ggplot(aes(x = factor(Country, levels = unique(Country[order(Region, var)]), ordered = TRUE), y = var, fill = Region)) +
  7. 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:

  1. library(tidyverse)
  2. dat %&gt;%
  3. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  4. levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;),
  5. ordered = TRUE))) %&gt;%
  6. ggplot(aes(x = factor(Country, levels = unique(Country[order(Region, var)]), ordered = TRUE), y = var, fill = Region)) +
  7. 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)排序,代码如下:

  1. library(tidyverse)
  2. dat %>%
  3. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  4. levels = c("AP", "ECA", "MENA"),
  5. ordered = TRUE))) %>%
  6. ggplot(aes(x = fct_reorder(Country, var) %>%
  7. fct_reorder(Region, .fun = sort),
  8. y = var, fill = Region)) +
  9. 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:

  1. library(tidyverse)
  2. dat %&gt;%
  3. mutate(across(.cols = Region, .fns = ~readr::parse_factor(.x,
  4. levels = c(&quot;AP&quot;, &quot;ECA&quot;, &quot;MENA&quot;),
  5. ordered = TRUE))) %&gt;%
  6. ggplot(aes(x = fct_reorder(Country,var) %&gt;%
  7. fct_reorder(Region, .fun = sort),
  8. y = var, fill = Region)) +
  9. 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:

确定