简体   繁体   中英

How to scale a bar plot using ggplot2?

I want my bar plot to take up less space, currently there is a lot of white space which is not neccessary. I would like the bins to be a lot closer than they currently are. And since there are only two categories in the X axis I do not see why there is so much space between them. - increasing the bin width would make white space go away, but then the bins become unnaturally large.

Code:

# 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 之间较大差距的图表。

You can change the aspect.ratio in your theme using the following code:

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 :

在此处输入图像描述

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