简体   繁体   中英

custom bar distances in ggplot

Using ggplot, you can change the width of a bar of a bar graph by modifying width :

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

You can uniformly change the distance of the bars using position_dodge() :

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. 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.

I would probably handle this by using a numeric x scale and relabelling the axis with my factor levels:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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