简体   繁体   English

控制条形图的总宽度

[英]Controlling the total width of a barplot

How to get rid of all this space where the blue lines are? 如何摆脱蓝线所在的所有空间?

Data: 数据:

data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
                  value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))

data$is_repeat = factor(data$is_repeat, levels = c(0,1),
                                        labels = c("One-time", "Repeat"))

Plot: 情节:

ggplot(data, aes(is_repeat, value)) +
      geom_bar(stat = "identity", width = 0.3) + 
      ggtitle("Title") + 
      xlab("Type of event") +
      ylab("Total Value") +
      ylim(0, 150000) +
      theme_minimal()

在此输入图像描述

edit: I looked at that question and it did NOT solve my problem. 编辑:我看了那个问题,并没有解决我的问题。 My guess is that in the other question's plot, there are 4 bars, so it looks filled. 我的猜测是,在另一个问题的情节中,有4个条形,所以看起来很充实。 I want to reduce the total width of the X axis. 我想减少X轴的总宽度。

another edit: Added data. 另一个编辑:添加数据。

If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with: 如果您想要完全去除条形之间的空间,并且您不介意条形图的宽度,您可以使用:

 geom_bar(stat="identity", position="stack", width=1)

or theme(aspect.ratio=1) theme(aspect.ratio=1)

And to remove the space from the end of the plot to the bars you need 并从绘图的末尾删除空间到您需要的栏

scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) 

So your code looks like this: 所以你的代码看起来像这样:

  ggplot(data, aes(is_repeat, value)) +
  geom_bar(stat="identity", position="stack", width=1) + 
  ggtitle("Title") + 
  xlab("Type of event") +
  ylab("Total Value") +
  ylim(0, 150000) + 
  scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
  theme_minimal()

And the output: 并输出:

在此输入图像描述

You can add space between bars with changing the width=1 您可以在更改width=1条形之间添加空格

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

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