通过垂直分割容器,通过精确的截止点为ggplot直方图着色 [英] Coloring ggplot histogram by precise cut off points by splitting the bins vertically
问题描述
我想通过不同的垂直截止点为ggplot直方图着色.我能够使用此答案,但是发现在我的数据中,垃圾箱被拆分并缩短了.最少的示例和下面的图表.
I would like to color a ggplot histogram by different vertical cutoff points. I was able to use this answer, but found that on my data the bins are split up and shortened. Minimal example and chart below.
如何在不将这些切碎的较短垃圾箱切碎的情况下垂直拆分垃圾箱?
How can I split up the bins vertically without getting these chopped up shorter bins?
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")
推荐答案
我可以想到的一种方法是将边界作为大小相等的垃圾箱的边界.一种方法是:
One way I could think of is to make cut off as a boundary of equal sized bins. One way to do so is:
# 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()
但是我可以说,做这样的事情看起来是一种更自然的呈现数据的方式.
But I may say that doing something like this looks a more natural way of presenting the data.
为每个分位数填充不同的颜色ggplot的geom_density()
这篇关于通过垂直分割容器,通过精确的截止点为ggplot直方图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!