ggplot2:如何根据 bin 范围为直方图中的特定 bin 着色
ggplot2: how to color specific bins in histogram based on bin ranges
我正在使用 ggplot2 绘制直方图,并试图弄清楚如何用不同于其他颜色的另一种颜色为特定的分箱着色。我要着色的容器由它们的容器边缘/范围定义。
我发现的类似问题要求根据原始值而不是 bin 范围进行条件着色,either for a specific value or 。
示例:
dt <- data.table(x = runif(10000))
ggplot(dt, aes(x)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", fill = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
这给了我这个情节:
我将最左边的 bin 定义为 [0, 0.01),从那里计算其他 bin。
现在我想给以下 bin 上不同的颜色:[0, 0.01), [0.1, 0.11), [0.2, 0.21) ...,即从
开始的 bin
> seq(0, 1, 0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
我该怎么做?
编辑:这是我想要的情节:
如果您想沿变量 X 创建值范围并以不同方式为它们着色,您可以使用 cut
函数:
cut divides the range of x into intervals and codes the values in x according to which interval they fall. The leftmost interval corresponds to level one, the next leftmost to level two and so on.
因此,稍微调整一下您的代码,您将拥有:
#Grouping variable 'x' in dt according sequence 'seq(0, 1, 0.1)'
dt$breaks <- cut(dt$x, breaks = seq(0, 1, 0.1))
#Plotting
ggplot(dt, aes(x, col = breaks, fill = breaks)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
davidnortes 的回答很好地显示了颜色,如果您只想突出显示 some bins,这里有一个选项。我将从 cut
开始预先识别一些垃圾箱(这需要与您的 binwidth=
和 geom_histogram
的其他选项保持同步),然后是一个简单的逻辑来确定哪些要突出显示。
library(dplyr)
dt %>%
mutate(
grp = cut(x, seq(0, 1, by = 0.01), labels = FALSE, include.lowest = TRUE),
is6 = between(grp, 60, 69)
) %>%
ggplot(aes(x, fill = is6)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
选项:
- 删除
is6
图例,添加 + scale_fill_discrete(guide = "none")
如果你想要多个不同的频段,也许 case_when
可以提供帮助,注意 is6
不需要是合乎逻辑的:
dt %>%
mutate(
grp = cut(x, seq(0, 1, by = 0.01), labels = FALSE, include.lowest = TRUE),
highlight = case_when(
between(grp, 60, 69) ~ "A",
between(grp, 20, 25) ~ "B",
TRUE ~ "C")
) %>%
ggplot(aes(x, fill = highlight)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
scale_fill_discrete
也适用于此。
你可能想要每组 highlight
或类似的特定颜色,使用 scale_fill_manual
.
编辑:
这是你的图片,尽管有颜色:
dt %>%
mutate(
grp = (x %% 0.1 < 0.01)
) %>%
ggplot(aes(x, fill = grp)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
我正在使用 ggplot2 绘制直方图,并试图弄清楚如何用不同于其他颜色的另一种颜色为特定的分箱着色。我要着色的容器由它们的容器边缘/范围定义。
我发现的类似问题要求根据原始值而不是 bin 范围进行条件着色,either for a specific value or
示例:
dt <- data.table(x = runif(10000))
ggplot(dt, aes(x)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", fill = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
这给了我这个情节:
我将最左边的 bin 定义为 [0, 0.01),从那里计算其他 bin。
现在我想给以下 bin 上不同的颜色:[0, 0.01), [0.1, 0.11), [0.2, 0.21) ...,即从
开始的 bin> seq(0, 1, 0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
我该怎么做?
编辑:这是我想要的情节:
如果您想沿变量 X 创建值范围并以不同方式为它们着色,您可以使用 cut
函数:
cut divides the range of x into intervals and codes the values in x according to which interval they fall. The leftmost interval corresponds to level one, the next leftmost to level two and so on.
因此,稍微调整一下您的代码,您将拥有:
#Grouping variable 'x' in dt according sequence 'seq(0, 1, 0.1)'
dt$breaks <- cut(dt$x, breaks = seq(0, 1, 0.1))
#Plotting
ggplot(dt, aes(x, col = breaks, fill = breaks)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
davidnortes 的回答很好地显示了颜色,如果您只想突出显示 some bins,这里有一个选项。我将从 cut
开始预先识别一些垃圾箱(这需要与您的 binwidth=
和 geom_histogram
的其他选项保持同步),然后是一个简单的逻辑来确定哪些要突出显示。
library(dplyr)
dt %>%
mutate(
grp = cut(x, seq(0, 1, by = 0.01), labels = FALSE, include.lowest = TRUE),
is6 = between(grp, 60, 69)
) %>%
ggplot(aes(x, fill = is6)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))
选项:
- 删除
is6
图例,添加+ scale_fill_discrete(guide = "none")
如果你想要多个不同的频段,也许
case_when
可以提供帮助,注意is6
不需要是合乎逻辑的:dt %>% mutate( grp = cut(x, seq(0, 1, by = 0.01), labels = FALSE, include.lowest = TRUE), highlight = case_when( between(grp, 60, 69) ~ "A", between(grp, 20, 25) ~ "B", TRUE ~ "C") ) %>% ggplot(aes(x, fill = highlight)) + geom_histogram(binwidth = 0.01, boundary = 0, closed = "left", col = "darkgreen", alpha = 0.5, size = 0.1) + scale_x_continuous(breaks = seq(0, 1, 0.1))
scale_fill_discrete
也适用于此。你可能想要每组
highlight
或类似的特定颜色,使用scale_fill_manual
.
编辑:
这是你的图片,尽管有颜色:
dt %>%
mutate(
grp = (x %% 0.1 < 0.01)
) %>%
ggplot(aes(x, fill = grp)) +
geom_histogram(binwidth = 0.01, boundary = 0, closed = "left",
col = "darkgreen", alpha = 0.5, size = 0.1) +
scale_x_continuous(breaks = seq(0, 1, 0.1))