如何将 ggplot 中的 “fill” 变量的条形图排列在一起?

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

How to arrange the bars of the "fill" variable in ggplot together?

问题

如何根据填充变量将柱形图分组?将不同颜色的柱形条分组在一起,而不是分开。这两个变量都是因子。

  1. 这是我的代码:

sur %>%
select(circumstances_bite, circumstances_bite_broad) %>%
drop_na() %>%
ggplot(aes(y = fct_infreq(circumstances_bite), fill = circumstances_bite_broad)) +
geom_bar(position = "dodge") +
xlab("No of people") +
ylab("circumstance of bite") +
ggtitle("circumstance of bite by a pet dog")

  1. ![我的图表](https://i.stack.imgur.com/2APAY.png)
英文:

This is my code:

  1. sur %>%
  2. select(circumstances_bite, circumstances_bite_broad) %>%
  3. drop_na() %>%
  4. ggplot(aes(y=fct_infreq(circumstances_bite), fill = circumstances_bite_broad))+
  5. geom_bar()+
  6. xlab("No of people")+
  7. ylab("circumstance of bite")+
  8. ggtitle ("circumstance of bite by a pet dog")

How do I group the bars according to the fill variable? Groping the different colour bars together and not seperately. Both the variables are factors.

如何将 ggplot 中的 “fill” 变量的条形图排列在一起?

答案1

得分: 0

一个简单的解决方案是在ggplot之外手动计算计数,然后按照所需的顺序对数据进行排序,即通过 circumstances_bite_broad 并通过 forecast::fct_inorder 来修复顺序。

使用一些假随机示例数据:

  1. set.seed(123)
  2. library(tidyverse)
  3. sur <- data.frame(
  4. circumstances_bite = sample(LETTERS[1:26], 100, replace = TRUE)
  5. ) %>%
  6. mutate(circumstances_bite_broad = case_when(
  7. circumstances_bite %in% LETTERS[1:9] ~ "a",
  8. circumstances_bite %in% LETTERS[10:18] ~ "b",
  9. circumstances_bite %in% LETTERS[19:26] ~ "c"
  10. ))
  11. sur %>%
  12. select(circumstances_bite, circumstances_bite_broad) %>%
  13. drop_na() %>%
  14. count(circumstances_bite, circumstances_bite_broad) %>%
  15. arrange(circumstances_bite_broad, desc(n)) %>%
  16. mutate(circumstances_bite = fct_inorder(circumstances_bite)) %>%
  17. ggplot(aes(x = n, y = circumstances_bite, fill = circumstances_bite_broad)) +
  18. geom_col() +
  19. xlab("No of people") +
  20. ylab("circumstance of bite") +
  21. ggtitle("circumstance of bite by a pet dog")

如何将 ggplot 中的 “fill” 变量的条形图排列在一起?

英文:

A simple solution would be to compute the counts manually outside of ggplot, then order you data in your desired order, i.e. by circumstances_bite_broad and fix the order via forecast::fct_inorder.

Using some fake random example data:

  1. set.seed(123)
  2. library(tidyverse)
  3. sur &lt;- data.frame(
  4. circumstances_bite = sample(LETTERS[1:26], 100, replace = TRUE)
  5. ) |&gt;
  6. mutate(circumstances_bite_broad = case_when(
  7. circumstances_bite %in% LETTERS[1:9] ~ &quot;a&quot;,
  8. circumstances_bite %in% LETTERS[10:18] ~ &quot;b&quot;,
  9. circumstances_bite %in% LETTERS[19:26] ~ &quot;c&quot;
  10. ))
  11. sur %&gt;%
  12. select(circumstances_bite, circumstances_bite_broad) %&gt;%
  13. drop_na() %&gt;%
  14. count(circumstances_bite, circumstances_bite_broad) |&gt;
  15. arrange(circumstances_bite_broad, desc(n)) |&gt;
  16. mutate(circumstances_bite = fct_inorder(circumstances_bite)) |&gt;
  17. ggplot(aes(x = n, y = circumstances_bite, fill = circumstances_bite_broad)) +
  18. geom_col() +
  19. xlab(&quot;No of people&quot;) +
  20. ylab(&quot;circumstance of bite&quot;) +
  21. ggtitle(&quot;circumstance of bite by a pet dog&quot;)

如何将 ggplot 中的 “fill” 变量的条形图排列在一起?

huangapple
  • 本文由 发表于 2023年4月7日 02:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952871.html
匿名

发表评论

匿名网友

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

确定