我正在尝试使用 ggplot2 在直方图底部绘制一些线段,覆盖几条线段以指示分布的相关部分。我希望绿色段的厚度是灰色段的两倍,但绿色段似乎总是灰色段厚度的五倍。这是一些玩具数据和我的 ggplot2 代码:
library(ggplot2)
x<- as.data.frame(rnorm(1000, mean=50))
colnames(x) <- c("values")
ggplot(x, aes(x=values)) +
geom_histogram (binwidth=1,position="identity", col="black")+
theme_classic(base_size=18)+
theme(axis.line.x = element_line(colour = "black"),
axis.line.y = element_line(colour = "black"))+
geom_segment(aes(y=-10, yend=-10, col=I("darkgray"), size=.1,x=1, xend=100),show.legend=F)+
geom_segment(aes(y=-10, yend=-10, col=I("palegreen4"), size=.2,x=25,xend=75), show.legend=F)
我的结果:
最佳答案
ggplot(x, aes(x=values)) +
geom_histogram(binwidth=1, position="identity", col="black") +
theme_classic(base_size=18) +
geom_segment(aes(y=-10, yend=-10,x=1, xend=100), colour = "grey", size=1, show.legend=F) +
geom_segment(aes(y=-10, yend=-10, x=25,xend=75), colour = "green", size = 2, show.legend=F)
尝试搬家
size
在你之外 aes()
称呼。只有随数据变化的变量才需要在 aes()
内, 所以 colour
也可以出来我清理了你的 theme_axis adjustments
因为我没有注意到对最终情节的明显影响。
关于R:geom_segment ggplot2 中的线条粗细不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37845370/