繁体   English   中英

如何使用 ggplot2 缩放条形图 plot?

[英]How to scale a bar plot using ggplot2?

我希望我的栏 plot 占用更少的空间,目前有很多不必要的空白。 我希望垃圾箱比现在更近。 由于 X 轴上只有两个类别,我不明白为什么它们之间有这么多空间。 - 增加 bin 宽度会使空白 go 消失,但随后 bin 变得异常大。

代码:

# Creates plot to show response-rate
hubspot_ordering %>% 
    ggplot(aes(x = Replied)) +
        geom_bar(color = "black", 
                 fill = "steelblue",
                 stat = "count",
                 width = 0.1,
                 position = position_dodge(0.9)) +
        xlab("Kommuners respons på e-post og oppringing") +
        scale_x_discrete(labels = c("Besvart",
                                    "Ingen respons")) +
        ylab("Antall kommuner") +
        theme(element_line(size = 0.5)) +
        theme_bw() -> plot_response_rate

Output: 显示 bin 之间较大差距的图表。

您可以使用以下代码更改theme中的aspect.ratio

df <- data.frame(x=factor(LETTERS[1:2]), y=sample(1:100, 2))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.1)) + 
  geom_bar(stat="identity", position="identity") +
  theme(aspect.ratio = 2/1) +
  labs(x="", y="") 

Output aspect.ratio=2 :

在此处输入图像描述

Output aspect.ratio=1 :

在此处输入图像描述

暂无
暂无

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

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