R: ggplot2中geom_segment线条粗细不一致

7
我正在尝试在 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)

我的结果是: 输入图像描述
1个回答

13
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,因为我没有注意到对最终图形有实质影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接