首页 > 解决方案 > 直方图未对齐/对应于 ggplot/R 中的 x 轴

问题描述

我有一个直方图,说明 x 轴上的特定细胞计数(1 = 1 个细胞计数,2 = 2 个细胞计数等),以及在 y 轴上累计有多少患者被诊断出具有这种特定细胞计数。

但是,直方图的“底”与 x 轴上的特定值不一致。最好,我希望 x 轴细胞计数位于直方图底部的中心。

我附上了一张 ATM 外观的照片:这里

我的数据

p <– structure(list(WHO.Grade = c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), ki67pro = c(4L, 3L, 4L, 4L, 7L, 6L, 4L, 1L, 4L, 12L, 4L, 3L, 1L, 2L, 20L, 10L, 3L, 3L, 3L, 5L, 3L, 3L, 4L, 5L, 2L, 4L, 5L, 3L, 15L, 4L, 4L, 4L, 4L, 2L, 4L, 2L, 2L, 3L, 7L, 5L, 4L, 2L, 4L, 6L, 4L, 3L, 5L, 4L, 2L, 3L, 3L, 5L, 8L, 0L, 3L, 2L, 20L, 4L, 4L, 4L, 3L, 5L, 5L, 12L, 3L, 2L, 2L, 3L, 3L, NA, 4L, 3L, 3L, 12L, 4L, 1L, 3L, 8L, 7L, 4L, 5L, 3L, 3L, 3L, 3L, 1L, 4L, 5L, 2L, 3L, 3L, 5L, 7L, NA, 2L, 12L, 4L, 0L, 4L, 3L, 10L, 5L, 4L, 3L, 20L, 10L, 10L, 3L, 2L, 10L, 4L, 5L, 3L, 2L, 4L, 2L, 5L, 2L, 4L, 25L, 3L, 5L, 3L, 4L, 7L, 0L, 5L, 7L, 1L, 1L, 1L, 4L, 4L, 6L, 5L, 7L, 3L, 3L, NA, 3L, 4L, 3L, 5L, 10L, 1L, 2L, 3L, 2L, 4L, 5L, 4L, 3L, 4L, 3L, 3L, 7L, 3L, 4L, 3L, 4L, 5L, 6L, 3L, 2L, 3L, 4L, 5L, 3L, 4L, 2L, 4L, 5L, 5L, 12L, 12L, 7L)), .Names = c("WHO.Grade", "ki67pro"), class = "data.frame", row.names = c(NA, -176L))

p1 <- subset(p, p$WHO.Grade==1)
p2 <- subset(p, p$WHO.Grade==2)
p3 <- subset(p, p$WHO.Grade==3)

我使用了以下脚本:

q <-      ggplot()  + theme_grey() +
  scale_x_continuous(name="The Ki-67/MIB-1 LI percetage", limits=c(-1, 26), seq(-1,26,by=1)) +
  scale_y_continuous(name="Patients diagnosed", limits=c(0, 44), seq(0,44,by=2)) +

  geom_histogram(aes(x=p1$ki67pro), colour="#222a37", fill="#222a37", bins=40, alpha=0.30)  + 
  geom_histogram(aes(x=p2$ki67pro), colour="dodgerblue2", fill="dodgerblue2", bins=40, alpha=0.35) +
  geom_histogram(aes(x=p3$ki67pro), colour="darkred", fill="darkred", bins=40, alpha=0.35) +

  geom_density(aes(x=p$ki67pro, y=..count..), colour="orange", fill="white", alpha=0)  + 
  geom_hline(yintercept=0, colour="white", size=0.9)

q

谢谢,C。

标签: rggplot2

解决方案


离开我的评论,替换geom_histgeom_bar删除 bin 参数可以让你:

在此处输入图像描述


推荐阅读