ggplot2根据数据拆分颜色直方图:facet_grid

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

ggplot2 split color histograms according to data: facet_grid

问题

建立在这个问题的基础上:

是否有一种方法可以创建一个直方图网格,其中在任意值上方和下方的柱子有不同的颜色(无重叠的柱子),而不需要引用ggplot()之外的环境?我可以使用单个直方图做到这一点,就像这样(仅用中位数进行说明):

  1. set.seed(123)
  2. value = stats::rnorm(100, mean = 0, sd = 1)
  3. df = data.frame(value)
  4. df %>%
  5. {
  6. ggplot(data = ., aes(x = value, fill = ifelse(value > median(value), "0", "1"))) +
  7. geom_histogram(boundary = median(.$value), alpha = 0.5, position = "identity") +
  8. theme(legend.position = "none")
  9. }

ggplot2根据数据拆分颜色直方图:facet_grid

是否可以为分面图创建这样的效果,其中每个图根据一个分组变量使用不同的值?例如,这个方法不起作用:

  1. set.seed(456)
  2. value = stats::rnorm(200, mean = 0, sd = 1)
  3. group = c(rep(1,100), rep(2,100))
  4. df = data.frame(value, group)
  5. df %>%
  6. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %>%
  7. dplyr::group_by(group) %>%
  8. dplyr::mutate(above_median = value > median(value)) %>%
  9. {
  10. ggplot(data = ., aes(x = value, fill = above_median)) +
  11. facet_grid(rows = group) +
  12. geom_histogram(boundary = median(.$value), alpha = 0.5, position = "identity") +
  13. theme(legend.position = "none")
  14. }

ggplot2根据数据拆分颜色直方图:facet_grid

英文:

Building on this question:

Is there a way to create a grid of histograms where the bins are different colors above vs. below arbitrary values (without overlapping bins), without needing to refer to the environment outside of ggplot()? I can do this with a single histogram, like this (using median for illustration purposes):

  1. set.seed(123)
  2. value = stats::rnorm(100, mean = 0, sd = 1)
  3. df = data.frame(value)
  4. df %>%
  5. {
  6. ggplot(data = ., aes(x = value, fill = ifelse(value > median(value), "0", "1"))) +
  7. geom_histogram(boundary = median(.$value), alpha = 0.5, position = "identity") +
  8. theme(legend.position = "none")
  9. }

ggplot2根据数据拆分颜色直方图:facet_grid

Can this be done for faceted plots, where each plot uses a different value, according to a grouping variable? E.g. this doesn't work:

  1. set.seed(456)
  2. value = stats::rnorm(200, mean = 0, sd = 1)
  3. group = c(rep(1,100), rep(2,100))
  4. df = data.frame(value, group)
  5. df %>%
  6. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %>%
  7. dplyr::group_by(group) %>%
  8. dplyr::mutate(above_median = value > median(value)) %>%
  9. {
  10. ggplot(data = ., aes(x = value, fill = above_median)) +
  11. facet_grid(rows = group) +
  12. geom_histogram(boundary = median(.$value), alpha = 0.5, position = "identity") +
  13. theme(legend.position = "none")
  14. }

ggplot2根据数据拆分颜色直方图:facet_grid

答案1

得分: 2

以下是代码部分的中文翻译:

一种选项是使用多个 geom_histogram 层来添加直方图,即按组拆分数据,然后使用 lapply 为每个组添加一个 geom_histogram

  1. library(dplyr, warn=FALSE)
  2. library(ggplot2)
  3. df %>%
  4. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %>%
  5. dplyr::group_by(group) %>%
  6. dplyr::mutate(above_median = value > median(value)) %>%
  7. {
  8. ggplot(data = ., aes(x = value, fill = above_median)) +
  9. facet_grid(rows = vars(group)) +
  10. lapply(split(., .$group), function(x) {
  11. geom_histogram(data = x, boundary = median(x$value), alpha = 0.5, position = "identity")
  12. }) +
  13. theme(legend.position = "none")
  14. }
  15. #> `stat_bin()` 使用 `bins = 30`。使用 `binwidth` 选择更好的值。
  16. #> `stat_bin()` 使用 `bins = 30`。使用 `binwidth` 选择更好的值。

ggplot2根据数据拆分颜色直方图:facet_grid

英文:

One option would be to add you histograms using multiple geom_histogram layers, i.e. split your data by group, then use lapply to add a geom_histogram for each group:

  1. library(dplyr, warn=FALSE)
  2. library(ggplot2)
  3. df %>%
  4. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %>%
  5. dplyr::group_by(group) %>%
  6. dplyr::mutate(above_median = value > median(value)) %>%
  7. {
  8. ggplot(data = ., aes(x = value, fill = above_median)) +
  9. facet_grid(rows = vars(group)) +
  10. lapply(split(., .$group), function(x) {
  11. geom_histogram(data = x, boundary = median(x$value), alpha = 0.5, position = "identity")
  12. }) +
  13. theme(legend.position = "none")
  14. }
  15. #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
  16. #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot2根据数据拆分颜色直方图:facet_grid<!-- -->

答案2

得分: 2

这是我解决问题的方式,但@stefan的答案更好(+1)。

  1. library(tidyverse)
  2. set.seed(456)
  3. value = stats::rnorm(200, mean = 0, sd = 1)
  4. group = c(rep(1,100), rep(2,100))
  5. df = data.frame(value, group)
  6. df %>%
  7. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %>%
  8. dplyr::group_by(group) %>%
  9. dplyr::mutate(above_median = value > median(value)) %>%
  10. ungroup() %>%
  11. group_split(group) %>%
  12. map(~{
  13. ggplot(data = .x, aes(x = value, fill = above_median)) +
  14. facet_grid(rows = .x$group) +
  15. geom_histogram(boundary = median(.x$value), alpha = 0.5, position = "identity") +
  16. theme(legend.position = "none")
  17. })
  18. #> [[1]]
  19. #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot2根据数据拆分颜色直方图:facet_grid

#>
#> [2]
#> stat_bin() using bins = 30. Pick better value with binwidth.

ggplot2根据数据拆分颜色直方图:facet_grid

创建于2023年03月16日,使用 reprex v2.0.2

英文:

This is how I would tackle the problem, but @stefan's answer is better (+1),

  1. library(tidyverse)
  2. set.seed(456)
  3. value = stats::rnorm(200, mean = 0, sd = 1)
  4. group = c(rep(1,100), rep(2,100))
  5. df = data.frame(value, group)
  6. df %&gt;%
  7. dplyr::mutate(value = ifelse(group == 2, value + 1, value)) %&gt;%
  8. dplyr::group_by(group) %&gt;%
  9. dplyr::mutate(above_median = value &gt; median(value)) %&gt;%
  10. ungroup() %&gt;%
  11. group_split(group) %&gt;%
  12. map(~{
  13. ggplot(data = .x, aes(x = value, fill = above_median)) +
  14. facet_grid(rows = .x$group) +
  15. geom_histogram(boundary = median(.x$value), alpha = 0.5, position = &quot;identity&quot;) +
  16. theme(legend.position = &quot;none&quot;)
  17. })
  18. #&gt; [[1]]
  19. #&gt; `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot2根据数据拆分颜色直方图:facet_grid<!-- -->

  1. #&gt;
  2. #&gt; [[2]]
  3. #&gt; `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot2根据数据拆分颜色直方图:facet_grid<!-- -->

<sup>Created on 2023-03-16 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月15日 21:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745359.html
匿名

发表评论

匿名网友

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

确定