cmirian
cmirian

Reputation: 2253

Histogram does not align/correspond to x-axis in ggplot/R

I have a histogram illustrating a specific cell count on the x-axis (1 = 1 cell counted, 2 = 2 cells counted etc), and how many patients that cumulative have been diagnosed with this specific cell count on the y-axis.

However, the "base" of the histogram does not align with the specific value on the x-axis. Preferably, I want the x-axis-cell-count to be be centered in the base of the histogram.

I have attached a photo of how it looks ATM: here

My data

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)

I have used the follow script:

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

Thanks, C.

Upvotes: 0

Views: 109

Answers (1)

Nautica
Nautica

Reputation: 2018

Going off of my comment, replacing geom_hist with geom_bar and removing the bin argument gets you:

enter image description here

Upvotes: 1

Related Questions