问题描述
考虑这个简单的例子
pre $ library $ g $ p
dat ggplot(dat,aes(x = number))+ geom_histogram()
看看酒吧是如何奇怪地与x轴对齐?为什么当 10.0
中的栏居中时, 5.0
左边的第一个栏?我怎样才能控制这个?例如,对于我来说,让标签右边的条开始更有意义。
谢谢!
这会将栏放在值的中心位置上。
data< ; data.frame(number = c(5,10,11,12,12,12,13,15,15))
ggplot(data,aes(x = number))+ geom_histogram(binwidth = 0.5 )
这是一个窍门,让tick标签可以在左边对齐。
但是,如果您添加其他数据,您还需要将它们移位
ggplot(data,aes(x = number)) +
geom_histogram(binwidth = 0.5)+
scale_x_continuous(
breaks = seq(0.75,15.75,1),#show x-ticks在条上对齐(0.25之前的值, bin width)
labels = 1:16#更改tick标签以获得栏x值
)
其他选项: binwidth = 1,breaks = seq(0.5,15.5,1)
(可能对整数更有意义)
Consider this simple example
library(ggplot2)
dat <- data.frame(number = c(5, 10, 11 ,12,12,12,13,15,15))
ggplot(dat, aes(x = number)) + geom_histogram()
See how the bars are weirdly aligned with the x axis? Why is the first bar on the left of 5.0
while the bar at 10.0
is centered? How can I get control over that? For instance, it would make more sense (to me) to have the bar starting on the right of the label.
Thanks!
This will center the bar on the value
data <- data.frame(number = c(5, 10, 11 ,12,12,12,13,15,15))
ggplot(data,aes(x = number)) + geom_histogram(binwidth = 0.5)
Here is a trick with the tick label to get the bar align on the left..But if you add other data, you need to shift them also
ggplot(data,aes(x = number)) +
geom_histogram(binwidth = 0.5) +
scale_x_continuous(
breaks=seq(0.75,15.75,1), #show x-ticks align on the bar (0.25 before the value, half of the binwidth)
labels = 1:16 #change tick label to get the bar x-value
)
other option: binwidth = 1, breaks=seq(0.5,15.5,1)
(might make more sense for integer)
这篇关于ggplot2:如何将直方图的条与x轴对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!