Skip to content

x-axis expand changes after plot_grid #212

@arslan9732

Description

@arslan9732

I am using plot_grid() to combine three plots. but after applying plot_grid(), it seems like it change the expand() in scale_x_discrete().

library(ggplot2)
library(cowplot)
library(gggenomes)

data1 <- data.frame(
  Window = as.factor(seq(0,3800, by = 100)),
  Para1 = runif(39, min = 40, max = 50))

data2 <- data.frame(
  Window = as.factor(seq(0,7400, by = 100)),
  Para2 = runif(75, min = 40, max = 50))

p1 <- ggplot(data1, aes(x = as.factor(Window), y = Para1, group = 1)) +
  geom_line(color = "grey") +
  geom_ribbon(aes(ymin = 0, ymax = Para1), fill = "grey", alpha = 1) +
  scale_x_discrete(expand = c(0.01,0.01),
                   breaks = c(0, 3800)  # Show only first and last
  ) +
  scale_y_continuous(
    expand = c(0, 0),
    limits = c(0, 101),
    breaks = seq(0, 100, by = 50)
  ) +
  theme_classic() +
  theme(
    panel.background = element_rect(fill = "transparent", color = NA),
    plot.background = element_rect(fill = "transparent", color = NA),
    ) +
  labs(x = "")
p1


p2 <- ggplot(data2, aes(x = as.factor(Window), y = Para2, group = 1)) +
  geom_line(color = "grey") +
  geom_ribbon(aes(ymin = 0, ymax = Para2), fill = "grey", alpha = 1) +
  scale_x_discrete(expand = c(0.01,0.01),
                   breaks = c(0,7400)  # Show only first and last
  ) +
  scale_y_continuous(
    expand = c(0, 0),
    limits = c(0, 101),
    breaks = seq(0, 100, by = 50)
  ) +
  theme_classic() +
  theme(
    panel.background = element_rect(fill = "transparent", color = NA),
    plot.background = element_rect(fill = "transparent", color = NA),
  ) +
  labs(x = "")
p2

df <- data.frame(
  seq_id = c("a", "b"),
  length = c(3800,7400)
)

p3 <- gggenomes(seqs=df) + 
  geom_seq() +
  scale_x_continuous(expand = c(0.01,0.01), breaks = seq(0, max(df$length)+100 , by = 1000)) +
  scale_y_continuous(expand = c(0,0.4)) +
  theme_classic()

narrow <- plot_grid(p1,
                    NULL, nrow = 1, rel_widths = c(0.5, 0.42))
plot_grid(narrow, p3, p2,
          ncol =  1)

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions