如何使用 ggplot2 增加条形图中轴线和条形之间的间距

问题描述 投票:0回答:1

如何增加 y 轴线和以下条形图中第一个条形之间的间距?

# Load ggplot
library(ggplot2)

# Load the Titanic dataset
data("Titanic")
Titanic <- as.data.frame(Titanic)

# Bar plot
ggplot(Titanic, aes(x = Class, y = Freq, fill = Survived)) + 
  geom_col(position = "fill") +
  coord_cartesian(ylim = c(0, 1), expand = FALSE) +
  theme_classic() +
  theme(legend.position = "none",
        axis.line.x = element_blank(),
        axis.ticks.x = element_blank())

有没有办法只将

coord_cartesian(expand = FALSE)
应用于 x 轴?这可以解决问题。

r ggplot2 bar-chart axis spacing
1个回答
0
投票

我认为您无法通过

coord_cartesion
获得那种程度的控制。你可以用体重秤代替

ggplot(Titanic, aes(x = Class, y = Freq, fill = Survived)) + 
  geom_col(position = "fill") +
  scale_x_discrete(expand=expansion(add=c(.8, 0))) + 
  scale_y_continuous(expand=expansion(add=c(0, 0))) + 
  theme_classic() +
  theme(legend.position = "none",
        axis.line.x = element_blank(),
        axis.ticks.x = element_blank())

将 0.8 的值更改为您喜欢的任何值。

© www.soinside.com 2019 - 2024. All rights reserved.