如何在ggplot2中增加bar图中各条之间的空间?

时间:2023-02-09 16:28:12

How can I increase the space between the bars in a bar plot in ggplot2 ?

在ggplot2中,如何增加条形图中条形之间的空间?

4 个解决方案

#1


47  

You can always play with the width parameter, as shown below:

您可以一直使用宽度参数,如下所示:

df <- data.frame(x=factor(LETTERS[1:4]), y=sample(1:100, 4))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.5)) + 
  geom_bar(stat="identity", position="identity") +
  opts(title="width = .5") + labs(x="", y="") +
  theme_bw()

Compare with the following other settings for width:

与以下其他宽度设置进行比较:

如何在ggplot2中增加bar图中各条之间的空间?

So far, so good. Now, suppose we have two factors. In case you would like to play with evenly spaced juxtaposed bars (like when using space together with beside=TRUE in barplot()), it's not so easy using geom_bar(position="dodge"): you can change bar width, but not add space in between adjacent bars (and I didn't find a convenient solution on Google). I ended up with something like that:

到目前为止还好。现在,假设有两个因子。如果您想要使用均匀间隔的并列排列(例如在barplot()中使用空格和旁边=TRUE),那么使用ge_bar (position=“dodge”)就不那么容易了:您可以改变bar的宽度,但是不要在相邻的栏之间添加空格(我在谷歌上没有找到一个方便的解决方案)。我最后得出了这样的结论:

df <- data.frame(g=gl(2, 1, labels=letters[1:2]), y=sample(1:100, 4))
x.seq <- c(1,2,4,5)
ggplot(data=transform(df, x=x.seq), aes(x=x, y=y, width=.85)) +
  geom_bar(stat="identity", aes(fill=g)) + labs(x="", y="") + 
  scale_x_discrete(breaks = NA) + 
  geom_text(aes(x=c(sum(x.seq[1:2])/2, sum(x.seq[3:4])/2), y=0, 
                label=c("X","Y")), vjust=1.2, size=8)

The vector used for the $x$-axis is "injected" in the data.frame, so that so you change the outer spacing if you want, while width allows to control for inner spacing. Labels for the $x$-axis might be enhanced by using scale_x_discrete().

$x$-axis所使用的向量是在data.frame中“注入”的,因此您可以根据需要更改外部空间,而宽度允许控制内部空间。可以使用scale_x_discrete()来增强$x$-axis的标签。

如何在ggplot2中增加bar图中各条之间的空间?

#2


33  

For space between factor bars use

用于因子条之间的空间使用

ggplot(data = d, aes(x=X, y=Y, fill=F)) 
 + geom_bar(width = 0.8, position = position_dodge(width = 0.9))

The width in geom_bar controls the bar width in relation to the x-axis while the width in position_dodge control the width of the space given to both bars also in relation to the x-axis. Play around with the width to find one that you like.

geom_bar中的宽度控制与x轴相关的bar宽度,而position_dodge中的宽度控制与x轴相关的两个bar的空间宽度。用宽度来找你喜欢的。

#3


1  

Thank you very much chl.! I had the same problem and you helped me solving it. Instead of using geom_text to add the X-labels I used scale_x_continuous (see below)

非常感谢你,chl。我遇到了同样的问题,你帮我解决了这个问题。我使用了scale_x_continuous(参见下面的),而不是使用geo_text添加x标签。

geom_text(aes(x=c(sum(x.seq[1:2])/2, sum(x.seq[3:4])/2), y=0, 
            label=c("X","Y")), vjust=1.2, size=8)

replaced by

取而代之的是

scale_x_continuous(breaks=c(mean(x.seq[1:2]), mean(x.seq[3:4])), labels=c("X", "Y")) 

#4


0  

For space between POSIXlt bars you need adjust the width from the number of seconds in a day

对于POSIXlt条之间的空间,您需要从一天的秒数调整宽度

# POSIXlt example: full & half width
d <- data.frame(dates = strptime(paste(2016, "01", 1:10, sep = "-"), "%Y-%m-%d"),
            values = 1:10)

ggplot(d, aes(dates, values)) +
  geom_bar(stat = "identity", width = 60*60*24) 

ggplot(d, aes(dates, values)) +
  geom_bar(stat = "identity", width = 60*60*24*0.5) 

#1


47  

You can always play with the width parameter, as shown below:

您可以一直使用宽度参数,如下所示:

df <- data.frame(x=factor(LETTERS[1:4]), y=sample(1:100, 4))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.5)) + 
  geom_bar(stat="identity", position="identity") +
  opts(title="width = .5") + labs(x="", y="") +
  theme_bw()

Compare with the following other settings for width:

与以下其他宽度设置进行比较:

如何在ggplot2中增加bar图中各条之间的空间?

So far, so good. Now, suppose we have two factors. In case you would like to play with evenly spaced juxtaposed bars (like when using space together with beside=TRUE in barplot()), it's not so easy using geom_bar(position="dodge"): you can change bar width, but not add space in between adjacent bars (and I didn't find a convenient solution on Google). I ended up with something like that:

到目前为止还好。现在,假设有两个因子。如果您想要使用均匀间隔的并列排列(例如在barplot()中使用空格和旁边=TRUE),那么使用ge_bar (position=“dodge”)就不那么容易了:您可以改变bar的宽度,但是不要在相邻的栏之间添加空格(我在谷歌上没有找到一个方便的解决方案)。我最后得出了这样的结论:

df <- data.frame(g=gl(2, 1, labels=letters[1:2]), y=sample(1:100, 4))
x.seq <- c(1,2,4,5)
ggplot(data=transform(df, x=x.seq), aes(x=x, y=y, width=.85)) +
  geom_bar(stat="identity", aes(fill=g)) + labs(x="", y="") + 
  scale_x_discrete(breaks = NA) + 
  geom_text(aes(x=c(sum(x.seq[1:2])/2, sum(x.seq[3:4])/2), y=0, 
                label=c("X","Y")), vjust=1.2, size=8)

The vector used for the $x$-axis is "injected" in the data.frame, so that so you change the outer spacing if you want, while width allows to control for inner spacing. Labels for the $x$-axis might be enhanced by using scale_x_discrete().

$x$-axis所使用的向量是在data.frame中“注入”的,因此您可以根据需要更改外部空间,而宽度允许控制内部空间。可以使用scale_x_discrete()来增强$x$-axis的标签。

如何在ggplot2中增加bar图中各条之间的空间?

#2


33  

For space between factor bars use

用于因子条之间的空间使用

ggplot(data = d, aes(x=X, y=Y, fill=F)) 
 + geom_bar(width = 0.8, position = position_dodge(width = 0.9))

The width in geom_bar controls the bar width in relation to the x-axis while the width in position_dodge control the width of the space given to both bars also in relation to the x-axis. Play around with the width to find one that you like.

geom_bar中的宽度控制与x轴相关的bar宽度,而position_dodge中的宽度控制与x轴相关的两个bar的空间宽度。用宽度来找你喜欢的。

#3


1  

Thank you very much chl.! I had the same problem and you helped me solving it. Instead of using geom_text to add the X-labels I used scale_x_continuous (see below)

非常感谢你,chl。我遇到了同样的问题,你帮我解决了这个问题。我使用了scale_x_continuous(参见下面的),而不是使用geo_text添加x标签。

geom_text(aes(x=c(sum(x.seq[1:2])/2, sum(x.seq[3:4])/2), y=0, 
            label=c("X","Y")), vjust=1.2, size=8)

replaced by

取而代之的是

scale_x_continuous(breaks=c(mean(x.seq[1:2]), mean(x.seq[3:4])), labels=c("X", "Y")) 

#4


0  

For space between POSIXlt bars you need adjust the width from the number of seconds in a day

对于POSIXlt条之间的空间,您需要从一天的秒数调整宽度

# POSIXlt example: full & half width
d <- data.frame(dates = strptime(paste(2016, "01", 1:10, sep = "-"), "%Y-%m-%d"),
            values = 1:10)

ggplot(d, aes(dates, values)) +
  geom_bar(stat = "identity", width = 60*60*24) 

ggplot(d, aes(dates, values)) +
  geom_bar(stat = "identity", width = 60*60*24*0.5)