简体   繁体   English

ggplot中的自定义条形距离

[英]custom bar distances in ggplot

Using ggplot, you can change the width of a bar of a bar graph by modifying width :使用 ggplot,您可以通过修改width来更改条形图的条形宽度

geom_bar(stat="identity",position=position_dodge(), width = .9) geom_bar(stat="identity",position=position_dodge(), width = .9)

You can uniformly change the distance of the bars using position_dodge() :您可以使用position_dodge()统一更改条形的距离:

geom_bar(stat="identity", position=position_dodge(1) ,width = .9) geom_bar(stat="identity", position=position_dodge(1) ,width = .9)

How do I customize the distance between bars so they are varied in a ununiform manner?如何自定义条形之间的距离,使它们以不均匀的方式变化?

It's not clear exactly what you mean.不清楚你的意思。 I'm assuming you mean you have a discrete x axis variable and you wish to specify custom spacing between each bar.我假设您的意思是您有一个离散的 x 轴变量,并且您希望在每个条形之间指定自定义间距。 It's possible to use position_jitter to get random spacing, though this also affects bar width and I'm guessing is not what you want.可以使用position_jitter来获得随机间距,尽管这也会影响条宽,我猜这不是你想要的。

I would probably handle this by using a numeric x scale and relabelling the axis with my factor levels:我可能会通过使用数字 x 比例并用我的因子水平重新标记轴来处理这个问题:

library(ggplot2)

ggplot(data = data.frame(x = 1:10 + rep(c(0.1, -0.1), 5), y = sample(11:20))) + 
  geom_bar(aes(x, y, fill = factor(x)), color = "black", stat = "identity") +
  scale_x_continuous(breaks = 1:10 + rep(c(0.1, -0.1), 5), 
                     labels = LETTERS[1:10]) +
  guides(fill = guide_none())

在此处输入图片说明

Of course, we can only guess at what you really want since you didn't provide a motivating example.当然,我们只能猜测你真正想要什么,因为你没有提供一个激励性的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM