基于另一个变量的geom_col中的颜色/填充条? [英] Color/fill bars in geom_col based on another variable?
问题描述
我有一个未着色的geom_col
,希望它通过在条形图中显示不同的阴影来显示有关另一个(连续)变量的信息.
I have an uncolored geom_col
and would like it to display information about another (continuous) variable by displaying different shades of color in the bars.
从geom_col开始
Starting with a geom_col
library(dplyr)
library(ggplot2)
set.seed(124)
iris[sample(1:150, 50), ] %>%
group_by(Species) %>%
summarise(n=n()) %>%
ggplot(aes(Species, n)) +
geom_col()
假设我们要根据每个分组中mean(Sepal.Width)
的高低来给条形上色
Suppose we want to color the bars according to how low/high mean(Sepal.Width)
in each grouping
(注意:我不知道是否有办法为ggplot提供连续"颜色,但如果没有,可以使用以下颜色)
(note: I don't know if there's a way to provide 'continuous' colors to a ggplot, but, if not, the following colors would be fine to use)
library(RColorBrewer)
display.brewer.pal(n = 3, name= "PuBu")
brewer.pal(n = 3, name = "PuBu")
[1] "#ECE7F2" "#A6BDDB" "#2B8CBE"
最终结果应与上面的geom_col相同,但条形图根据mean(Sepal.Width)
的高低来着色.
The end result should be the same geom_col as above but with the bars colored according to how low/high mean(Sepal.Width)
is.
- 此答案显示了类似的内容,但非常手动,适用于3条线,但对于许多地块而言却不可持续条数很高(因为需要手动设置太多
case_when
条件) - 这相似,但是颜色基于图中已显示的变量,而不是另一个变量
- 还要注意,在我上面提供的示例中,有3条,我提供3种颜色,这有点手工,如果有更好的(即更少手工)指定颜色的方法,我们将很高兴学习
- This answer shows something similar but is highly manual, and is okay for 3 bars, but not sustainable for many plots with a high number of bars (since would require too many
case_when
conditions to be manually set) - This is similar but the coloring is based on a variable already displayed in the plot, rather than another variable
- Note also, in the example I provide above, there are 3 bars and I provide 3 colors, this is somewhat manual and if there's a better (i.e. less manual) way to designate colors would be glad to learn it
我以为这可以用,但似乎忽略了我提供的颜色
I thought this would work, but it seems to ignore the colors I provide
library(RColorBrewer)
# fill info from: https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r
set.seed(124)
iris[sample(1:150, 50), ] %>%
group_by(Species) %>%
summarise(n=n(), sep_mean = mean(Sepal.Width)) %>%
arrange(desc(n)) %>%
mutate(colors = brewer.pal(n = 3, name = "PuBu")) %>%
mutate(Species=factor(Species, levels=Species)) %>%
ggplot(aes(Species, n, fill = colors)) +
geom_col()
推荐答案
执行以下操作
- 将
fill = sep_mean
添加到aes()
- 添加
+ scale_fill_gradient()
- 删除
mutate(colors = brewer.pal(n = 3, name = "PuBu"))
,因为上一步会为您处理颜色
- add
fill = sep_mean
toaes()
- add
+ scale_fill_gradient()
- remove
mutate(colors = brewer.pal(n = 3, name = "PuBu"))
since the previous step takes care of colors for you
set.seed(124)
iris[sample(1:150, 50), ] %>%
group_by(Species) %>%
summarise(n=n(), sep_mean = mean(Sepal.Width)) %>%
arrange(desc(n)) %>%
mutate(Species=factor(Species, levels=Species)) %>%
ggplot(aes(Species, n, fill = sep_mean, label=sprintf("%.2f", sep_mean))) +
geom_col() +
scale_fill_gradient() +
labs(fill="Sepal Width\n(mean cm)") +
geom_text()
这篇关于基于另一个变量的geom_col中的颜色/填充条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!