Kou
Kou

Reputation: 57

ggplot2, patchwork - part of axis title blocked by margin of neighbouring plot

I have two line charts, stacked one below the other using 'patchwork'. I want a common y-axis title for the stack, so I added Y-axis label to upper of the two line charts.After stacking I found that, lower part of the y-axis label is being blocked by margin of the lower line chart (as seen by green and yellow plot backgrounds in the graphics attached below).Here is the code and the result

# 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

So I tried solving the problem by reducing the left margin of lower chart from 0 to -1, but it didn't change the margin and axis title remained blocked. If left margin of both the charts is reduced to -1, y-axis title, ticks are no more visible as seen in the graphics below. Green and yellow filled rectangles are plot backgrounds. Left plot margin set to -1

Can anybody please help find solution? Any ideas, what else can I try? Thank you!

Upvotes: 2

Views: 2610

Answers (1)

Muon
Muon

Reputation: 1346

You can simply just set the title for the combined plots (and not for lc1 or lc2). You'll need to set blank y-labels for lc1 and lc2 as you've done already for lc2 (e.g. ylab(label = "").

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

patchwork combined y-label

Upvotes: 3

Related Questions