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

左侧绘图边距设置为 0

所以我尝试通过将下图的左边距从 0 减少到 -1 来解决这个问题,但它并没有改变边距并且轴标题仍然被阻塞。如果两个图表的左边距减少到 -1,y 轴标题,刻度将不再可见,如下图所示。绿色和黄色填充的矩形是绘图背景。 左侧绘图边距设置为 -1

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

4

1 回答 1

2

您可以简单地设置组合图的标题(而不是lc1or lc2)。您需要为 和 设置空白 y 标签lc1lc2就像您已经为lc2(例如ylab(label = "").

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

拼凑组合 y 标签

于 2020-04-05T01:07:37.767 回答