通过垂直分割容器,通过精确的截止点为ggplot直方图着色

时间:2019-04-17 09:56:59

标签: r ggplot2

我想通过不同的垂直截止点为ggplot直方图着色。我可以使用this answer,但是发现在我的数据中,垃圾箱被拆分并缩短了。下面是最小的示例和图表。

如何在不将这些切碎的较短垃圾箱切碎的情况下垂直拆分垃圾箱?

library(tidyverse)

set.seed(42)

# define cutoffs
cutoff_1 <- -21
cutoff_2 <- 60

df <- data.frame(rand = rnorm(10000)*100) %>% 
  mutate(colors = case_when(
    rand < cutoff_1 ~ "red",
    rand >= cutoff_1 & rand <= cutoff_2 ~ "blue",
    rand > cutoff_2 ~ "green"
    )
  )
n.bins <- 20 # number of bins
additional.cutoffs <- c(cutoff_1, cutoff_2) # additional bins

bins <- seq(min(df$rand), max(df$rand), length.out = n.bins)    
bins <- c(bins, additional.cutoffs) %>% sort()

df %>% 
  ggplot(aes(x=rand, fill=colors)) +
  geom_histogram(breaks=bins) +
  geom_vline(xintercept=c(cutoff_1, cutoff_2), colour="black") 

enter image description here

1 个答案:

答案 0 :(得分:1)

我可以想到的一种方法是将切除作为大小相等的垃圾箱的边界。一种方法是:

# decide bin width (I decided to have two bins in the middle)
binwidth <- (cutoff_2 - cutoff_1)/2 
# create a possible bins (stating from the cut off and make sure that it covers the domain
bins <- -21 + (-15:15) * binwidth 
# limit the range of possible bins based on the range of the data
bins <- bins[between(bins, min(df$rand) - binwidth, max(df$rand) + binwidth)]

df %>% 
  ggplot(aes(x=rand, fill=colors)) +
  geom_histogram(breaks=bins) +
  geom_vline(xintercept=c(cutoff_1, cutoff_2), colour="black") + theme_minimal()

enter image description here

注意

但是我可以说做这样的事情看起来是一种更自然的数据展示方式。

Fill different colors for each quantile in geom_density() of ggplot