ggplot2中的圆形直方图,条形间距均匀,没有多余的线 [英] Circular histogram in ggplot2 with even spacing of bars and no extra lines

查看:39
本文介绍了ggplot2中的圆形直方图,条形间距均匀,没有多余的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ggplot2中制作一个圆形直方图,该直方图显示了24小时内通话次数的变化.我的数据集从0开始到23,每小时的呼叫次数:

I'm working on making a circular histogram in ggplot2 that shows how the number of calls varies over 24 hours. My dataset starts at 0 and goes to 23, with the number of calls per hour:

df = data.frame(xvar = 0:23, 
                y = c(468,520,459,256,397,241,117,120,45,100,231,398,340,276,151,134,157,203,308,493,537,462,448,383))

我正在使用以下代码创建圆形直方图:

I'm using the following code to create the circular histogram:

ggplot(df, aes(xvar, y)) +
    coord_polar(theta = "x", start = -.13, direction = 1) +
    geom_bar(stat = "identity", fill = "maroon4", width = .9) +
    geom_hline(yintercept = seq(0, 500, by = 100), color = "grey80", size = 0.3) +
    scale_x_continuous(breaks = seq(0, 24), labels = seq(0, 24)) +
    xlab("Hour") +
    ylab("Number of Calls") +
    ggtitle("Number of Calls per Hour") +
    theme_bw()

我真的很喜欢这样的剧情:

I really like the resulting plot:

但是我不知道如何在23条和0条之间获得与其他条相同的间距.现在,这两根杆彼此齐平,到目前为止,我尝试过的任何方法都无法将它们分开.我也有兴趣删除不同时间之间的界限(例如21和22之间的界限),因为这会分散您的注意力,并且无法传达任何信息.任何建议,将不胜感激,尤其是在将23和0条隔开时!

but I can't figure out how to get the same spacing between the 23 and 0 bars as is present for the other bars. Right now, those two bars are flush against one another and nothing I've tried so far will separate them. I'm also interested in removing the lines between the different hours (ex. the line between 21 and 22) since it's somewhat distracting and doesn't convey any information. Any advice would be much appreciated, particularly on spacing the 23 and 0 bars!

推荐答案

您可以使用 scale_x_continuous expand 参数进行调整.简化了一点,

You can use the expand parameter of scale_x_continuous to adjust. Simplified a little,

ggplot(df, aes(x = xvar, y = y)) +
    coord_polar(theta = "x", start = -.13) +
    geom_bar(stat = "identity", fill = "maroon4", width = .9) +
    geom_hline(yintercept = seq(0, 500, by = 100), color = "grey80", size = 0.3) +
    scale_x_continuous(breaks = 0:24, expand = c(.002,0)) +
    labs(x = "Hour", y = "Number of Calls", title = "Number of Calls per Hour") +
    theme_bw()

这篇关于ggplot2中的圆形直方图,条形间距均匀,没有多余的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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