R geom_区域中离散轴的ymin和ymax
为了对不同的群体和整个人口进行基准测试,我尝试将人口箱图中的人口框添加到该图中。当轴为数字时,可通过R geom_区域中离散轴的ymin和ymax,r,ggplot2,R,Ggplot2,为了对不同的群体和整个人口进行基准测试,我尝试将人口箱图中的人口框添加到该图中。当轴为数字时,可通过geom\u区域和geom\u hline轻松实现: m <- median(mtcars$mpg) Q1 <- quantile(as.numeric(mtcars$mpg), c(0.25)) Q3 <- quantile(as.numeric(mtcars$mpg), c(0.75)) ggplot(mtcars, aes(x=cyl, y=mpg))+ geom_rec
geom\u区域
和geom\u hline
轻松实现:
m <- median(mtcars$mpg)
Q1 <- quantile(as.numeric(mtcars$mpg), c(0.25))
Q3 <- quantile(as.numeric(mtcars$mpg), c(0.75))
ggplot(mtcars, aes(x=cyl, y=mpg))+
geom_rect(aes(xmin = -Inf , xmax = Inf , ymin = Q1 , ymax = Q3 ),fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + scale_x_discrete(breaks=(factor(mtcars$cyl))) + geom_boxplot(aes(group=cyl))+ coord_flip(xlim=c(3,9)) +
geom_point() + theme_classic()
这是可行的,但不是我想要的。我希望这个区域能覆盖整个地块。此外,该区域的颜色与第一个图上的颜色不同(可能与alpha
的行为有关)
解决方案/建议?正如Joran所说,只需将
geom_区域
数据置于aes
之外即可。合乎逻辑,因为您没有映射
m <- median(esoph$ncases)
Q1 <- quantile(as.numeric(esoph$ncases), c(0.25))
Q3 <- quantile(as.numeric(esoph$ncases), c(0.75))
ggplot(esoph, aes(x=agegp, y=ncases))+
geom_rect(xmin = -Inf , xmax = Inf , ymin = Q1 , ymax = Q3 ,fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + geom_boxplot(aes(group=agegp))+ coord_flip() + geom_point() +
theme_classic()
m在aes()之外移动xmin
和xmax
时会发生什么?因为你没有映射它们,所以它们不应该在那里。谢谢!工作起来很有魅力!没问题!你可以自己写下来作为答案并接受它,只是为了总结一下。你知道如何在使用alpha
时在不同的图上保持颜色恒定吗?如果你不能提前知道你将使用alpha
的形状会发生多少重叠,那么就不知道了。
ggplot(esoph, aes(x=agegp, y=ncases))+
geom_rect(aes(xmin = min(agegp) , xmax = max(agegp) , ymin = Q1 , ymax = Q3 ),fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + geom_boxplot(aes(group=agegp))+ coord_flip() + geom_point() +
theme_classic()
m <- median(esoph$ncases)
Q1 <- quantile(as.numeric(esoph$ncases), c(0.25))
Q3 <- quantile(as.numeric(esoph$ncases), c(0.75))
ggplot(esoph, aes(x=agegp, y=ncases))+
geom_rect(xmin = -Inf , xmax = Inf , ymin = Q1 , ymax = Q3 ,fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + geom_boxplot(aes(group=agegp))+ coord_flip() + geom_point() +
theme_classic()