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

标签 r ggplot2 histogram

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

我的结果:
enter image description here

最佳答案

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/

相关文章:

r - 年份变量应该是 R 面板数据中的因子还是数字?

r - 如何在R中反转ggmap栅格图像的颜色?

r - 如何修复ggplot2中的 "Discrete value supplied to continuous scale"错误

c++ - 点云的描述符

python - Matplotlib:指定直方图刻度标签中 bin 值的格式

从 R 中的 foreach 循环返回列表的列表

r - 计算一天中每小时发生的持续时间的一部分

R 幂回归中 stat_smooth 和 lm (使用对数)之间的差异?

r - 如何在 Seurat (ggplot2) 中的 DoHeatmap 图中重新排序单元格

python - Matplotlib hexbin log scale colorbar 刻度标签作为指数