将直方图区间对齐5

时间:2017-05-02 20:20:11

标签: r ggplot2

Current Histogram View

您好,我有一些数据排列在大小为5的箱子中。但是,轴不与箱子对齐,因此很难看到它们的起点和终点。

我要么重新定义轴,以便标签以5为增量与最小值对齐(轴将有标签204,209,214等)

或者箱子以5的间隔开始,取决于系列中的最小值。例如,您可以看到我提供的数据集中的最小值是204.截至目前,箱子在视觉上看起来是204-208,209-213等。一旦进行了更改,箱子就会变为200 -204,205-209等

data <- data.frame(c(218, 213, 234, 223, 262, 251, 240, 235, 236, 237, 245, 229, 261, 269, 256, 239, 255, 258, 221, 231, 246, 248, 206, 204, 211))

ggplot(data=data, aes(data)) + 
  geom_histogram(breaks=seq(min(data), max(data), by =5),
             col="black",
             fill="grey") +
  scale_x_continuous(breaks = pretty_breaks(n=20)) +
  labs(x="Data Value", y="Count", title = "Histogram of Data Values") +
  theme(plot.title = element_text(hjust = 0.5, size=20))

2 个答案:

答案 0 :(得分:3)

您的第二个选项是更简单的选项:

data <- data.frame(x = c(218, 213, 234, 223, 262, 251, 240, 235, 236, 237, 245, 229, 261, 269, 256, 239, 255, 258, 221, 231, 246, 248, 206, 204, 211))

ggplot(data, aes(x)) + 
  geom_histogram(binwidth = 5, boundary = 5, fill = "white", colour = "black") + 
  scale_x_continuous(breaks = seq(0, 1000, by = 5))

enter image description here

bindwidth =将分档设置为5的倍数。boundary =设置其中一个分档的边缘(即使它超出范围,它们也都会以boundary +结束 n * binwidth,任意 n )。

第一个涉及更多,因为您必须在geomscale中指定数据的最小值和最大值。

ggplot(data, aes(x)) + 
  geom_histogram(binwidth = 5, boundary = min(data$x), fill = "white", colour = "black") + 
  scale_x_continuous(breaks = seq(min(data$x), max(data$x) + 4, by = 5))

enter image description here

答案 1 :(得分:2)

对于第一个选项,我认为您可以使用scale_x_continuous代替breaks强制执行pretty_breaks中的中断:

ggplot(data=data, aes(data)) + 
  geom_histogram(breaks=seq(min(data), max(data), by =5),
                 col="black",
                 fill="grey") +
  scale_x_continuous(breaks = seq(min(data), max(data), by = 5)) +
  labs(x="Data Value", y="Count", title = "Histogram of Data Values") +
  theme(plot.title = element_text(hjust = 0.5, size=20))

enter image description here