Home > OS >  remove x axis and the associated space on gig-lot
remove x axis and the associated space on gig-lot

Time:03-21

I am trying to use grid.arrange() to combine two graphs (where the first is just a simple y axis).

But there is a lot of space associated with the first simple y axis. Is there a way to remove this?

Please see attached code below. There is also the data in put format. Please see attached code below. There is also the data in put format. Please see attached code below. There is also the data in put format. Please see attached code below. There is also the data in put format.

left_axis <- ggplot(fig2_data, aes(y = tox_outcome))  
  scale_y_discrete(limits = rev)  
  theme_manuscript(base_size = 12)
 
right_panel <- ggplot(fig2_data)  
  geom_bar(aes(x = n, y = cohort, fill = tox_outcome_bool), stat = 'identity', position = position_fill(reverse = TRUE))  
  facet_wrap( ~ tox_outcome, ncol = 1, strip.position = 'top')  
  theme_manuscript(base_size = 12)  
  scale_fill_jco()  
  geom_text(aes(x = n, y = cohort, label = label_white), position = position_fill(reverse = TRUE, vjust = 0.5), size = 4, color = 'gray90')  
  geom_text(aes(x = n, y = cohort, label = label_black), position = position_fill(reverse = TRUE, vjust = 0.5), size = 4)  
  theme(strip.text.x = element_blank())  
  scale_y_discrete(name = '', labels = c("Q3W", "Q6W"))  
  scale_x_continuous(expand = c(0, 0))  
  theme(axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_blank(),
        legend.position = "none",
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank()
  )

grid.arrange(left_axis, right_panel, nrow = 1)

Here is the data:

> dput(fig2_data)
structure(list(tox_outcome = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L), .Label = c("severe_tox_g2plus", "severe_tox_g3plus", 
"hosp_in_18w_bool", "tx_stopped_for_tox", "GI_tox_bool", "Hepatobiliary_tox_bool", 
"Endocrine_tox_bool", "Skin_tox_bool", "General_tox_bool", "Renal_tox_bool"
), class = "factor"), cohort = c("q6w_post", "q6w_post", "q3w_post", 
"q3w_post", "q6w_post", "q6w_post", "q3w_post", "q3w_post", "q6w_post", 
"q6w_post", "q3w_post", "q3w_post", "q6w_post", "q6w_post", "q3w_post", 
"q3w_post", "q6w_post", "q6w_post", "q3w_post", "q3w_post", "q6w_post", 
"q6w_post", "q3w_post", "q3w_post", "q6w_post", "q6w_post", "q3w_post", 
"q3w_post", "q6w_post", "q6w_post", "q3w_post", "q3w_post", "q6w_post", 
"q6w_post", "q3w_post", "q3w_post", "q6w_post", "q6w_post", "q3w_post", 
"q3w_post"), tox_outcome_bool = structure(c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L), .Label = c("Yes", "No"), class = "factor"), n = c(82L, 
38L, 20L, 38L, 45L, 24L, 96L, 96L, 52L, 42L, 22L, 94L, 13L, 52L, 
59L, 43L, 15L, 1L, 66L, 78L, 50L, 55L, 27L, 73L, 95L, 98L, 65L, 
93L, 67L, 13L, 40L, 98L, 69L, 61L, 36L, 77L, 54L, 48L, 91L, 0L
), label_white = c(82L, NA, 20L, NA, 45L, NA, 96L, NA, 52L, NA, 
22L, NA, 13L, NA, 59L, NA, 15L, NA, 66L, NA, 50L, NA, 27L, NA, 
95L, NA, 65L, NA, 67L, NA, 40L, NA, 69L, NA, 36L, NA, 54L, NA, 
91L, NA), label_black = c(NA, 38L, NA, 38L, NA, 24L, NA, 96L, 
NA, 42L, NA, 94L, NA, 52L, NA, 43L, NA, 1L, NA, 78L, NA, 55L, 
NA, 73L, NA, 98L, NA, 93L, NA, 13L, NA, 98L, NA, 61L, NA, 77L, 
NA, 48L, NA, 0L)), row.names = c(NA, -40L), class = "data.frame")

large gap between two ggplots

CodePudding user response:

You can change the margin of your scale plot. You mean something like this:

library(ggsci)
library(gridExtra)
    left_axis <- ggplot(fig2_data, aes(y = tox_outcome))  
    scale_y_discrete(limits = rev)  
    theme(plot.margin=unit(c(0,-6,0,3), "cm"),
          element_blank())
    # theme_manuscript(base_size = 12)
  
  right_panel <- ggplot(fig2_data)  
    geom_bar(aes(x = n, y = cohort, fill = tox_outcome_bool), stat = 'identity', position = position_fill(reverse = TRUE))  
    facet_wrap( ~ tox_outcome, ncol = 1, strip.position = 'top')  
   # theme_manuscript(base_size = 12)  
    scale_fill_jco()  
    geom_text(aes(x = n, y = cohort, label = label_white), position = position_fill(reverse = TRUE, vjust = 0.5), size = 4, color = 'gray90')  
    geom_text(aes(x = n, y = cohort, label = label_black), position = position_fill(reverse = TRUE, vjust = 0.5), size = 4)  
    theme(strip.text.x = element_blank())  
    scale_y_discrete(name = '', labels = c("Q3W", "Q6W"))  
    scale_x_continuous(expand = c(0, 0))  
    theme(axis.text.x = element_blank(),
          axis.title.x = element_blank(),
          axis.ticks.x = element_blank(),
          axis.line.x = element_blank(),
          axis.line.y = element_blank(),
          legend.position = "none",
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank()
    )
  
  grid.arrange(left_axis, right_panel, nrow = 1)

enter image description here

CodePudding user response:

grid.arrange() by default gives each cell an equal amount of space. If you want a close fit around a specific grob, you should query its size and explicitly pass it.

Sample code:

grid.arrange(left_axis, right_panel, nrow = 1, widths=c(1,4))

Plot:

enter image description here

or even better grid.arrange(left_axis, right_panel, nrow = 1, widths=c(1,4.2))

enter image description here

  • Related