对齐直方图的条形,以标签为中心 [英] Align bars of histogram centered on labels

查看:83
本文介绍了对齐直方图的条形,以标签为中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于布局原因,我想将直方图条放置在标签的中心,以使条的中间位于标签的顶部.

For layouting reasons I would like to position the histgram bars centered on the labels, such that the middle of a bar is on top of the label.

library(ggplot2)

df <- data.frame(x = c(0,0,1,2,2,2))

ggplot(df,aes(x)) + 
    geom_histogram(binwidth=1) + 
    scale_x_continuous(breaks=0:2)

这是目前为止的样子-条形图的左侧在标签的顶部:

This is what it looks so far - the left side of a bar is on top of the label:

是否可以通过这种方式调整给定的代码段? (不使用geom_bar而不是f.x.)

Is it possible to adjust the given snippet in such a way? (without using geom_bar instead f.x.)

推荐答案

这不需要分类的x轴,但是如果您要设置的bin宽度不同于1,则需要稍微玩一下.

This doesn't require a categorical x axis, but you'll want to play a little if you have different bin widths than 1.

library(ggplot2)

df <- data.frame(x = c(0,0,1,2,2,2))

ggplot(df,aes(x)) + 
geom_histogram(binwidth=1,boundary=-0.5) + 
scale_x_continuous(breaks=0:2)

对于较早的ggplot2(<2.1.0),请使用geom_histogram(binwidth=1, origin=-0.5).

这篇关于对齐直方图的条形,以标签为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆