ggplot2,拼凑而成-轴标题的一部分被相邻图的边距阻塞

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

我有两个折线图,使用“拼凑而成”在一个折线图的下方堆叠。我想要一个通用的y轴标题,所以我在两个折线图的上方添加了y轴标签。堆叠后,我发现y轴标签的下部被下部线条的边距阻塞了图表(如下面附图中的绿色和黄色绘图背景所示)。此处是代码和结果

# data
x<- 1:256
x
y<- runif(256, 0, 10)
y

data <- data.frame(x,y)
head(data)

# lc1
# Geom properties
lc1<- ggplot(data, aes(x=x, y=y)) + 
  geom_line()

# Scale
lc1<- lc1 +
  expand_limits(x = 0, y = 0) +
  scale_x_continuous(
    expand = c(0, 0),
    breaks = c(0, 32, 64, 96, 128, 160, 192, 224, 256),
    label = NULL
  ) +
  scale_y_continuous(expand = c(0, 0),
                     breaks = c(0, 5, 10))

# Aspect ratio
lc1 <- lc1 + theme(aspect.ratio = 0.15)

# Panel properties
lc1 <- lc1 +
  theme(panel.background = element_blank()) +
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    legend.position = "none"
  )

# Axes lines, ticks, axis text
lc1 <- lc1 +
  theme(
    axis.line.x = element_line (colour = "grey", size = 0.5),
    axis.line.y = element_line(colour = "black", size = 0.5),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(colour = "black", size = 0.5),
    axis.ticks.length = unit(.15, "cm")
  ) +
  theme(
    axis.text.y = element_text(
      color = "black",
      face = "plain",
      size = 6,
      margin = margin(
        t = 0,
        r = 2,
        b = 0,
        l = 0
      ),
      angle = 0,
      vjust = 0,
      hjust = 0
    )
  )

# Title, caption, axes labels 
lc1<- lc1 + 
  ylab(label = "Y-axis label (unit)") +
  theme(axis.title.x = element_text(
    color = "black",
    size = 10,
    face = "bold",
    hjust = 0
  ))

# Plot margins
lc1 <- lc1 +
  theme(plot.margin = unit(c(-0.55,0,-0.53,0), "cm"))

# plot background
lc1<- lc1 + theme(plot.background = element_rect(fill = "green"))

lc1




# lc2
# Geom properties
lc2<- ggplot(data, aes(x=x, y=y)) + 
  geom_line()

# Scale
lc2<- lc2 +
  expand_limits(x = 0, y = 0) +
  scale_x_continuous(
    expand = c(0, 0),
    breaks = c(0, 32, 64, 96, 128, 160, 192, 224, 256),
    label = NULL
  ) +
  scale_y_continuous(expand = c(0, 0),
                     breaks = c(0, 5, 10))

# Aspect ratio
lc2 <- lc2 + theme(aspect.ratio = 0.15)

# Panel properties
lc2 <- lc2 +
  theme(panel.background = element_blank()) +
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    legend.position = "none"
  )

# Axes lines, ticks, axis text
lc2 <- lc2 +
  theme(
    axis.line.x = element_line (colour = "grey", size = 0.5),
    axis.line.y = element_line(colour = "black", size = 0.5),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(colour = "black", size = 0.5),
    axis.ticks.length = unit(.15, "cm")
  ) +
  theme(
    axis.text.y = element_text(
      color = "black",
      face = "plain",
      size = 6,
      margin = margin(
        t = 0,
        r = 2,
        b = 0,
        l = 0
      ),
      angle = 0,
      vjust = 0,
      hjust = 0
    )
  )

# Title, caption, axes labels 
lc2<- lc2 + 
  ylab(label = "") +
  theme(axis.title.x = element_text(
    color = "black",
    size = 10,
    face = "bold",
    hjust = 0.5
  ))

# Plot margins
lc2 <- lc2 +
  theme(plot.margin = unit(c(-0.55,0,-0.53,0), "cm"))

# plot background
lc2<- lc2 + theme(plot.background = element_rect(fill = "yellow"))

lc2

# stacking
library(patchwork)

p<- (lc1/lc2)
p

Left plot margin set to 0

因此,我尝试通过将下部图表的左边界从0减小到-1来解决问题,但是它没有改变边界,并且轴标题仍然被阻止。如果两个图表的左边距都减小为-1,y轴标题,则刻度不再可见,如下图所示。绿色和黄色填充矩形是绘图背景。Left plot margin set to -1

有人可以帮忙找到解决方法吗?有什么想法,我还能尝试什么?谢谢!

r ggplot2 plot axis patchwork
1个回答
0
投票

您可以只为组合图设置标题(而不能为lc1lc2设置标题)。您需要为lc1lc2设置空白的y标签,就像您已经为lc2设置的一样(例如ylab(label = "")

p <- (lc1/lc2) + ylab(label = "Y-axis label (unit)")

enter image description here

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