基于值梯度填充boxplot颜色 [英] Fill boxplot color based on value gradient
问题描述
我想使用颜色渐变
来根据 y
轴上的值对boxplot着色。我添加了红色到蓝色渐变的图例,但我不知道如何将这些颜色添加到每个框中。
library (ggplot2)
ggplot(mpg,aes(class,hwy))+ geom_boxplot(aes(color = hwy))+
scale_fill_gradient2(low =red,high =blue ,
midpoint = 25,limit = c(0,50),space =Lab,
name =hwy)
我不认为你可以。 ggplot 文档只提到一个因素的着色,而且我只看到有因素已在x轴上指定。
另外,当您考虑它时,您的陈述是模糊的,盒形图中的内容决定 fill
?中位数,上限,下限?
更新:
这并不是你所要求的,
mpg%>%
group_by(class)%>%
mutate( mean.hwy = mean(hwy))%>%
ggplot(aes(class,hwy))+
geom_boxplot()+
stat_summary(fun.y =mean,
aes(y = mean.hwy,color = mean.hwy),
geom =point)+
scale_color_gradient2(low =red,
high =blue ,
midpoint = 25,
limit = c(0,50),
space =Lab,
name =hwy)
$ b出于某种原因,我无法使用中位数工作,我猜这里会更有意义,但希望这会让您更接近你的目标。
I want to color the boxplot based the value on
y
axis using acolor gradient
. I added the legend filled with red to blue gradient but I don't know how to add those colors into each box.library(ggplot2) ggplot(mpg, aes(class, hwy))+ geom_boxplot(aes(colour = hwy))+ scale_fill_gradient2(low = "red", high = "blue", midpoint = 25, limit = c(0,50), space = "Lab", name="hwy")
解决方案I don't think you can. ggplot documentation only mentions coloring by a factor, and I see only factor that has been specified on x axis.
Also when you think about it your statement is ambigious, what in the box plot determines
fill
? Median, Upper Boundary, Lower Boundary?Update:
This isn't exactly what you asked for but its close.
mpg %>% group_by(class) %>% mutate(mean.hwy= mean(hwy)) %>% ggplot( aes(class, hwy)) + geom_boxplot() + stat_summary(fun.y= "mean", aes(y=mean.hwy,color=mean.hwy), geom = "point") + scale_color_gradient2(low = "red", high = "blue", midpoint = 25, limit = c(0,50), space = "Lab", name="hwy")
For some reason I couldn't get this to work with median which I guess would make more sense here but hopefully this will get you closer to your goal.
这篇关于基于值梯度填充boxplot颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!