How to change color of facet borders when using facet_grid

后端 未结 3 479
一向
一向 2020-12-16 03:24

When using facet_grid, ggplot2 divides the major categories that make up the facet variables with a broader-than-usual white line. This serves mos

相关标签:
3条回答
  • 2020-12-16 03:38

    Try this--you can control it with strip.background, formated with an element_rect call.

    qplot(hwy, cty, data = mpg) +
      facet_grid(. ~ manufacturer) +   
      theme(strip.text.x = element_text(size = 8,
                                        colour = "red", 
                                        angle = 90),
            strip.background = element_rect(fill = "darkblue",
                                            colour = NA)
      )
    
    0 讨论(0)
  • 2020-12-16 03:41

    You might need to use ggplot's layout table and gtable functions.

    library(ggplot2)
    library(gtable)
    library(grid)
    
    p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + 
         facet_grid(am ~ cyl)
    ## Get the plot grob
    gt <- ggplotGrob(p)
    
    ## Check the layout
    gtable_show_layout(gt)   # Vertical gaps are in columns 5 and 7
                             # and span rows 4 to 9
                             # Horizontal gap is in row 8
                             # and spans columns 4 to 9
    
    
    ## To automate the selection of the relevant rows and columns:
    ## Find the panels in the layout
    ## (t, l, b, r refer to top, left, bottom, right);
    ## The gaps' indices are one to the right of the panels' r index (except the right most panel);
    ## and one below the panels' b index (except the bottom most panel);
    ## Rmin and Rmax give the span of the horizontal gap;
    ## Bmin and Bmax give the span of the vertical gap;
    ## cols and rows are the columns and row indices of the gaps.
    
    panels = subset(gt$layout, grepl("panel", gt$layout$name), t:r)
    
    # The span of the horizontal gap
    Rmin = min(panels$r)
    Rmax = max(panels$r) + 1
    
    #The span of the vertical gap
    Bmin = min(panels$t) - 1
    Bmax = max(panels$t)
    
    # The columns and rows of the gaps
    cols = unique(panels$r)[-length(unique(panels$r))] + 1
    rows = unique(panels$t)[-length(unique(panels$t))] + 1
    
    # The grob - orange rectangle
    g = rectGrob(gp = gpar(col = NA, fill = "orange"))
    
    ## Add orange rectangles into the vertical and horizontal gaps
    gt <- gtable_add_grob(gt, 
          rep(list(g), length(cols)),
          t=Bmin, l=cols, b=Bmax)
    
    gt <- gtable_add_grob(gt, 
          rep(list(g), length(rows)),
          t=rows, l=Rmin, r=Rmax)
    
    ## Draw it
    grid.newpage()
    grid.draw(gt)
    

    enter image description here

    0 讨论(0)
  • 2020-12-16 03:58

    Although a year late, I found this to be an easy fix:

    ggplot(mpg, aes(cty, hwy, color = factor(year)))+ 
      geom_point()+
      facet_grid(cyl ~ drv) +
      theme(panel.margin=unit(.05, "lines"),
            panel.border = element_rect(color = "black", fill = NA, size = 1), 
            strip.background = element_rect(color = "black", size = 1))
    

    0 讨论(0)
提交回复
热议问题