2

I am trying to change the size of the strips. I want them smaller than what i am getting as they are taking a lot of space. I tried the code in this thread: edit strip size ggplot2. The recommendation is to use theme(strip.text.x = element_text(margin = margin(2,0,2,0, "cm"))). It did not work because i cannot merge it with theme(strip.text = element_blank()). I include the latter line of code because i do not want the name of the countries to show in the strips, i add them manually using geom_text. Is there a way to keep the name of the countries hidden in the strips but make the size of the strips smaller in height?

enter image description here

Here is the code below (the code is long because i add manually a legend to the graph):

    library(gtable)
    library(gridExtra)
    library(ggplot2)
    

p1= df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_text(aes(label = cntry, x =  Inf, y = Inf), vjust = 1.75, hjust =1)+
  geom_point(mapping=aes(x=estimate, y=term)) +
  facet_wrap(vars(cntry,continent)) 


#Plot it
dummy= df %>%
  ggplot(aes(x = estimate, y = term))+ 
  facet_wrap(vars(cntry,continent)) +
  geom_rect(aes(fill=continent), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  theme_minimal() +
  theme(strip.text = element_blank())


g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(dummy)



gtable_select <- function (x, ...) 
{
  matches <- c(...)
  x$layout <- x$layout[matches, , drop = FALSE]
  x$grobs <- x$grobs[matches]
  x
}

panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1

new_strips <- gtable_select(g2, panels | strips)
grid.newpage()
grid.draw(new_strips)

gtable_stack <- function(g1, g2){
  g1$grobs <- c(g1$grobs, g2$grobs)
  g1$layout <- transform(g1$layout, z= z-max(z), name="g2")
  g1$layout <- rbind(g1$layout, g2$layout)
  g1
}

new_plot <- gtable_stack(g1, new_strips)
grid.newpage()
grid.draw(new_plot)

# Extract only the legend from "dummy" plot
g_legend <- function(dummy){ 
  tmp <- ggplot_gtable(ggplot_build(dummy)) 
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
  legend <- tmp$grobs[[leg]] 
  return(legend)} 
# Assign the legend to a separate object
facet.legend <- g_legend(dummy)


jpeg("legend_graph.jpg", width = 8, height = 6, units = "in", res = 300)
print(grid.arrange(new_plot, facet.legend, nrow = 2, widths = c(7, 1), heights = c(6, 0.01)))
dev.off()

Here is the data:

structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("China", 
"USA", "Germany", "Canada"), class = "factor"), term = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L), .Label = c("2008", "2009", "2010", "2011", "2012", 
"2013", "2014", "2015", "2016", "2017"), class = "factor"), estimate = c(-0.02, 
-0.02, -0.05, -0.01, 0, 0.02, -0.01, 0.03, 0.02, 0, 0.09, 0.1, 
0.04, 0.13, 0.1, 0.07, 0.08, 0.01, -0.1, -0.09, -0.1, -0.1, -0.13, 
-0.12, -0.18, -0.07, -0.04, 0.01, 0, -0.02, -0.02, 0.03, 0.01, 
0.01, 0.03, 0.03), continent = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Asia", 
"America", "Europe"), class = "factor")), row.names = c(NA, -36L
), class = "data.frame")
Jack
  • 813
  • 4
  • 17

0 Answers0