R ggplot2: change colour of font and background in facet strip?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







4















I am trying to customize a ggplot2 plot containing facets, and would like to change both the colour of the facet strip, as well as the colour of the font. I found some code to change the strip.background colour, but was not able to modify it to change also the font color... any idea?



What I got so far:





library(ggplot2)
library(grid)

p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
ggtitle("How to change coloour of font in facet strip?")

g <- ggplot_gtable(ggplot_build(p))
strip_both <- which(grepl('strip-', g$layout$name))
fills <- c("red","green","blue","yellow","red","green","blue","yellow")
k <- 1

for (i in strip_both) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)




Created on 2018-11-23 by the reprex package (v0.2.1)










share|improve this question































    4















    I am trying to customize a ggplot2 plot containing facets, and would like to change both the colour of the facet strip, as well as the colour of the font. I found some code to change the strip.background colour, but was not able to modify it to change also the font color... any idea?



    What I got so far:





    library(ggplot2)
    library(grid)

    p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
    ggtitle("How to change coloour of font in facet strip?")

    g <- ggplot_gtable(ggplot_build(p))
    strip_both <- which(grepl('strip-', g$layout$name))
    fills <- c("red","green","blue","yellow","red","green","blue","yellow")
    k <- 1

    for (i in strip_both) {
    j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
    g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
    k <- k+1
    }
    grid.draw(g)




    Created on 2018-11-23 by the reprex package (v0.2.1)










    share|improve this question



























      4












      4








      4


      2






      I am trying to customize a ggplot2 plot containing facets, and would like to change both the colour of the facet strip, as well as the colour of the font. I found some code to change the strip.background colour, but was not able to modify it to change also the font color... any idea?



      What I got so far:





      library(ggplot2)
      library(grid)

      p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
      ggtitle("How to change coloour of font in facet strip?")

      g <- ggplot_gtable(ggplot_build(p))
      strip_both <- which(grepl('strip-', g$layout$name))
      fills <- c("red","green","blue","yellow","red","green","blue","yellow")
      k <- 1

      for (i in strip_both) {
      j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
      g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
      k <- k+1
      }
      grid.draw(g)




      Created on 2018-11-23 by the reprex package (v0.2.1)










      share|improve this question
















      I am trying to customize a ggplot2 plot containing facets, and would like to change both the colour of the facet strip, as well as the colour of the font. I found some code to change the strip.background colour, but was not able to modify it to change also the font color... any idea?



      What I got so far:





      library(ggplot2)
      library(grid)

      p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
      ggtitle("How to change coloour of font in facet strip?")

      g <- ggplot_gtable(ggplot_build(p))
      strip_both <- which(grepl('strip-', g$layout$name))
      fills <- c("red","green","blue","yellow","red","green","blue","yellow")
      k <- 1

      for (i in strip_both) {
      j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
      g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
      k <- k+1
      }
      grid.draw(g)




      Created on 2018-11-23 by the reprex package (v0.2.1)







      r ggplot2 grob






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 1:22









      Valentin

      2,2421532




      2,2421532










      asked Nov 24 '18 at 4:14









      MatifouMatifou

      2,3721729




      2,3721729
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Another option is using grid's editing functions, provided that we build the gPath of each grob that we want to edit.



          Prepare the gPaths:



          library(ggplot2)
          library(grid)

          p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)

          # Generate the ggplot2 plot grob
          g <- grid.force(ggplotGrob(p))
          # Get the names of grobs and their gPaths into a data.frame structure
          grobs_df <- do.call(cbind.data.frame, grid.ls(g, print = FALSE))
          # Build optimal gPaths that will be later used to identify grobs and edit them
          grobs_df$gPath_full <- paste(grobs_df$gPath, grobs_df$name, sep = "::")
          grobs_df$gPath_full <- gsub(pattern = "layout::",
          replacement = "",
          x = grobs_df$gPath_full,
          fixed = TRUE)


          Check out the table grobs_df and get familiar with the naming and paths. For example all strips contain the key word "strip". Their background is identified by the key word "background" and their title text by "titleGrob" & "text". We can then use regular expression to catch them:



          # Get the gPaths of the strip background grobs
          strip_bg_gpath <- grobs_df$gPath_full[grepl(pattern = ".*strip\.background.*",
          x = grobs_df$gPath_full)]
          strip_bg_gpath[1] # example of a gPath for strip background
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::strip.background.x..rect.5374"

          # Get the gPaths of the strip titles
          strip_txt_gpath <- grobs_df$gPath_full[grepl(pattern = "strip.*titleGrob.*text.*",
          x = grobs_df$gPath_full)]
          strip_txt_gpath[1] # example of a gPath for strip title
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::GRID.titleGrob.5368::GRID.text.5364"


          Now we can edit the grobs:



          # Generate some color
          n_cols <- length(strip_bg_gpath)
          fills <- rainbow(n_cols)
          txt_colors <- gray(0:n_cols/n_cols)

          # Edit the grobs
          for (i in 1:length(strip_bg_gpath)){
          g <- editGrob(grob = g, gPath = strip_bg_gpath[i], gp = gpar(fill = fills[i]))
          g <- editGrob(grob = g, gPath = strip_txt_gpath[i], gp = gpar(col = txt_colors[i]))
          }

          # Draw the edited plot
          grid.newpage(); grid.draw(g)
          # Save the edited plot
          ggsave("edit_strips_bg_txt.png", g)


          enter image description here






          share|improve this answer
























          • great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

            – Matifou
            Nov 24 '18 at 17:57



















          2














          Sure someone could find better solution, but I was only able to do this so far:



          library(ggplot2)
          library(grid)
          library(RColorBrewer)

          p <- ggplot(mpg, aes(x = displ, y = cty)) +
          geom_point() +
          facet_grid(drv ~ cyl) +
          ggtitle("How to change coloour of font in facet strip?") +
          ggthemes::theme_few()

          g <- ggplot_gtable(ggplot_build(p))

          strips <- which(grepl('strip-', g$layout$name))

          pal <- brewer.pal(8, "Paired")


          for (i in seq_along(strips)) {
          k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
          g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i + 1]
          }

          plot(g)


          change text and strips colors






          share|improve this answer



















          • 1





            great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

            – Matifou
            Nov 24 '18 at 17:57











          • Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

            – utubun
            Nov 24 '18 at 18:15














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455092%2fr-ggplot2-change-colour-of-font-and-background-in-facet-strip%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Another option is using grid's editing functions, provided that we build the gPath of each grob that we want to edit.



          Prepare the gPaths:



          library(ggplot2)
          library(grid)

          p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)

          # Generate the ggplot2 plot grob
          g <- grid.force(ggplotGrob(p))
          # Get the names of grobs and their gPaths into a data.frame structure
          grobs_df <- do.call(cbind.data.frame, grid.ls(g, print = FALSE))
          # Build optimal gPaths that will be later used to identify grobs and edit them
          grobs_df$gPath_full <- paste(grobs_df$gPath, grobs_df$name, sep = "::")
          grobs_df$gPath_full <- gsub(pattern = "layout::",
          replacement = "",
          x = grobs_df$gPath_full,
          fixed = TRUE)


          Check out the table grobs_df and get familiar with the naming and paths. For example all strips contain the key word "strip". Their background is identified by the key word "background" and their title text by "titleGrob" & "text". We can then use regular expression to catch them:



          # Get the gPaths of the strip background grobs
          strip_bg_gpath <- grobs_df$gPath_full[grepl(pattern = ".*strip\.background.*",
          x = grobs_df$gPath_full)]
          strip_bg_gpath[1] # example of a gPath for strip background
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::strip.background.x..rect.5374"

          # Get the gPaths of the strip titles
          strip_txt_gpath <- grobs_df$gPath_full[grepl(pattern = "strip.*titleGrob.*text.*",
          x = grobs_df$gPath_full)]
          strip_txt_gpath[1] # example of a gPath for strip title
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::GRID.titleGrob.5368::GRID.text.5364"


          Now we can edit the grobs:



          # Generate some color
          n_cols <- length(strip_bg_gpath)
          fills <- rainbow(n_cols)
          txt_colors <- gray(0:n_cols/n_cols)

          # Edit the grobs
          for (i in 1:length(strip_bg_gpath)){
          g <- editGrob(grob = g, gPath = strip_bg_gpath[i], gp = gpar(fill = fills[i]))
          g <- editGrob(grob = g, gPath = strip_txt_gpath[i], gp = gpar(col = txt_colors[i]))
          }

          # Draw the edited plot
          grid.newpage(); grid.draw(g)
          # Save the edited plot
          ggsave("edit_strips_bg_txt.png", g)


          enter image description here






          share|improve this answer
























          • great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

            – Matifou
            Nov 24 '18 at 17:57
















          2














          Another option is using grid's editing functions, provided that we build the gPath of each grob that we want to edit.



          Prepare the gPaths:



          library(ggplot2)
          library(grid)

          p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)

          # Generate the ggplot2 plot grob
          g <- grid.force(ggplotGrob(p))
          # Get the names of grobs and their gPaths into a data.frame structure
          grobs_df <- do.call(cbind.data.frame, grid.ls(g, print = FALSE))
          # Build optimal gPaths that will be later used to identify grobs and edit them
          grobs_df$gPath_full <- paste(grobs_df$gPath, grobs_df$name, sep = "::")
          grobs_df$gPath_full <- gsub(pattern = "layout::",
          replacement = "",
          x = grobs_df$gPath_full,
          fixed = TRUE)


          Check out the table grobs_df and get familiar with the naming and paths. For example all strips contain the key word "strip". Their background is identified by the key word "background" and their title text by "titleGrob" & "text". We can then use regular expression to catch them:



          # Get the gPaths of the strip background grobs
          strip_bg_gpath <- grobs_df$gPath_full[grepl(pattern = ".*strip\.background.*",
          x = grobs_df$gPath_full)]
          strip_bg_gpath[1] # example of a gPath for strip background
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::strip.background.x..rect.5374"

          # Get the gPaths of the strip titles
          strip_txt_gpath <- grobs_df$gPath_full[grepl(pattern = "strip.*titleGrob.*text.*",
          x = grobs_df$gPath_full)]
          strip_txt_gpath[1] # example of a gPath for strip title
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::GRID.titleGrob.5368::GRID.text.5364"


          Now we can edit the grobs:



          # Generate some color
          n_cols <- length(strip_bg_gpath)
          fills <- rainbow(n_cols)
          txt_colors <- gray(0:n_cols/n_cols)

          # Edit the grobs
          for (i in 1:length(strip_bg_gpath)){
          g <- editGrob(grob = g, gPath = strip_bg_gpath[i], gp = gpar(fill = fills[i]))
          g <- editGrob(grob = g, gPath = strip_txt_gpath[i], gp = gpar(col = txt_colors[i]))
          }

          # Draw the edited plot
          grid.newpage(); grid.draw(g)
          # Save the edited plot
          ggsave("edit_strips_bg_txt.png", g)


          enter image description here






          share|improve this answer
























          • great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

            – Matifou
            Nov 24 '18 at 17:57














          2












          2








          2







          Another option is using grid's editing functions, provided that we build the gPath of each grob that we want to edit.



          Prepare the gPaths:



          library(ggplot2)
          library(grid)

          p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)

          # Generate the ggplot2 plot grob
          g <- grid.force(ggplotGrob(p))
          # Get the names of grobs and their gPaths into a data.frame structure
          grobs_df <- do.call(cbind.data.frame, grid.ls(g, print = FALSE))
          # Build optimal gPaths that will be later used to identify grobs and edit them
          grobs_df$gPath_full <- paste(grobs_df$gPath, grobs_df$name, sep = "::")
          grobs_df$gPath_full <- gsub(pattern = "layout::",
          replacement = "",
          x = grobs_df$gPath_full,
          fixed = TRUE)


          Check out the table grobs_df and get familiar with the naming and paths. For example all strips contain the key word "strip". Their background is identified by the key word "background" and their title text by "titleGrob" & "text". We can then use regular expression to catch them:



          # Get the gPaths of the strip background grobs
          strip_bg_gpath <- grobs_df$gPath_full[grepl(pattern = ".*strip\.background.*",
          x = grobs_df$gPath_full)]
          strip_bg_gpath[1] # example of a gPath for strip background
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::strip.background.x..rect.5374"

          # Get the gPaths of the strip titles
          strip_txt_gpath <- grobs_df$gPath_full[grepl(pattern = "strip.*titleGrob.*text.*",
          x = grobs_df$gPath_full)]
          strip_txt_gpath[1] # example of a gPath for strip title
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::GRID.titleGrob.5368::GRID.text.5364"


          Now we can edit the grobs:



          # Generate some color
          n_cols <- length(strip_bg_gpath)
          fills <- rainbow(n_cols)
          txt_colors <- gray(0:n_cols/n_cols)

          # Edit the grobs
          for (i in 1:length(strip_bg_gpath)){
          g <- editGrob(grob = g, gPath = strip_bg_gpath[i], gp = gpar(fill = fills[i]))
          g <- editGrob(grob = g, gPath = strip_txt_gpath[i], gp = gpar(col = txt_colors[i]))
          }

          # Draw the edited plot
          grid.newpage(); grid.draw(g)
          # Save the edited plot
          ggsave("edit_strips_bg_txt.png", g)


          enter image description here






          share|improve this answer













          Another option is using grid's editing functions, provided that we build the gPath of each grob that we want to edit.



          Prepare the gPaths:



          library(ggplot2)
          library(grid)

          p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)

          # Generate the ggplot2 plot grob
          g <- grid.force(ggplotGrob(p))
          # Get the names of grobs and their gPaths into a data.frame structure
          grobs_df <- do.call(cbind.data.frame, grid.ls(g, print = FALSE))
          # Build optimal gPaths that will be later used to identify grobs and edit them
          grobs_df$gPath_full <- paste(grobs_df$gPath, grobs_df$name, sep = "::")
          grobs_df$gPath_full <- gsub(pattern = "layout::",
          replacement = "",
          x = grobs_df$gPath_full,
          fixed = TRUE)


          Check out the table grobs_df and get familiar with the naming and paths. For example all strips contain the key word "strip". Their background is identified by the key word "background" and their title text by "titleGrob" & "text". We can then use regular expression to catch them:



          # Get the gPaths of the strip background grobs
          strip_bg_gpath <- grobs_df$gPath_full[grepl(pattern = ".*strip\.background.*",
          x = grobs_df$gPath_full)]
          strip_bg_gpath[1] # example of a gPath for strip background
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::strip.background.x..rect.5374"

          # Get the gPaths of the strip titles
          strip_txt_gpath <- grobs_df$gPath_full[grepl(pattern = "strip.*titleGrob.*text.*",
          x = grobs_df$gPath_full)]
          strip_txt_gpath[1] # example of a gPath for strip title
          ## [1] "strip-t-1.7-5-7-5::strip.1-1-1-1::GRID.titleGrob.5368::GRID.text.5364"


          Now we can edit the grobs:



          # Generate some color
          n_cols <- length(strip_bg_gpath)
          fills <- rainbow(n_cols)
          txt_colors <- gray(0:n_cols/n_cols)

          # Edit the grobs
          for (i in 1:length(strip_bg_gpath)){
          g <- editGrob(grob = g, gPath = strip_bg_gpath[i], gp = gpar(fill = fills[i]))
          g <- editGrob(grob = g, gPath = strip_txt_gpath[i], gp = gpar(col = txt_colors[i]))
          }

          # Draw the edited plot
          grid.newpage(); grid.draw(g)
          # Save the edited plot
          ggsave("edit_strips_bg_txt.png", g)


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 11:19









          ValentinValentin

          2,2421532




          2,2421532













          • great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

            – Matifou
            Nov 24 '18 at 17:57



















          • great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

            – Matifou
            Nov 24 '18 at 17:57

















          great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

          – Matifou
          Nov 24 '18 at 17:57





          great, very detailed and instructive answer, thanks! (might maybe just want to comment the ggsave line? someone doing copy/paste might end up with unwanted save file?). Thanks!

          – Matifou
          Nov 24 '18 at 17:57













          2














          Sure someone could find better solution, but I was only able to do this so far:



          library(ggplot2)
          library(grid)
          library(RColorBrewer)

          p <- ggplot(mpg, aes(x = displ, y = cty)) +
          geom_point() +
          facet_grid(drv ~ cyl) +
          ggtitle("How to change coloour of font in facet strip?") +
          ggthemes::theme_few()

          g <- ggplot_gtable(ggplot_build(p))

          strips <- which(grepl('strip-', g$layout$name))

          pal <- brewer.pal(8, "Paired")


          for (i in seq_along(strips)) {
          k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
          g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i + 1]
          }

          plot(g)


          change text and strips colors






          share|improve this answer



















          • 1





            great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

            – Matifou
            Nov 24 '18 at 17:57











          • Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

            – utubun
            Nov 24 '18 at 18:15


















          2














          Sure someone could find better solution, but I was only able to do this so far:



          library(ggplot2)
          library(grid)
          library(RColorBrewer)

          p <- ggplot(mpg, aes(x = displ, y = cty)) +
          geom_point() +
          facet_grid(drv ~ cyl) +
          ggtitle("How to change coloour of font in facet strip?") +
          ggthemes::theme_few()

          g <- ggplot_gtable(ggplot_build(p))

          strips <- which(grepl('strip-', g$layout$name))

          pal <- brewer.pal(8, "Paired")


          for (i in seq_along(strips)) {
          k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
          g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i + 1]
          }

          plot(g)


          change text and strips colors






          share|improve this answer



















          • 1





            great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

            – Matifou
            Nov 24 '18 at 17:57











          • Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

            – utubun
            Nov 24 '18 at 18:15
















          2












          2








          2







          Sure someone could find better solution, but I was only able to do this so far:



          library(ggplot2)
          library(grid)
          library(RColorBrewer)

          p <- ggplot(mpg, aes(x = displ, y = cty)) +
          geom_point() +
          facet_grid(drv ~ cyl) +
          ggtitle("How to change coloour of font in facet strip?") +
          ggthemes::theme_few()

          g <- ggplot_gtable(ggplot_build(p))

          strips <- which(grepl('strip-', g$layout$name))

          pal <- brewer.pal(8, "Paired")


          for (i in seq_along(strips)) {
          k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
          g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i + 1]
          }

          plot(g)


          change text and strips colors






          share|improve this answer













          Sure someone could find better solution, but I was only able to do this so far:



          library(ggplot2)
          library(grid)
          library(RColorBrewer)

          p <- ggplot(mpg, aes(x = displ, y = cty)) +
          geom_point() +
          facet_grid(drv ~ cyl) +
          ggtitle("How to change coloour of font in facet strip?") +
          ggthemes::theme_few()

          g <- ggplot_gtable(ggplot_build(p))

          strips <- which(grepl('strip-', g$layout$name))

          pal <- brewer.pal(8, "Paired")


          for (i in seq_along(strips)) {
          k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
          g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
          g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i + 1]
          }

          plot(g)


          change text and strips colors







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 9:25









          utubunutubun

          1,8501914




          1,8501914








          • 1





            great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

            – Matifou
            Nov 24 '18 at 17:57











          • Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

            – utubun
            Nov 24 '18 at 18:15
















          • 1





            great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

            – Matifou
            Nov 24 '18 at 17:57











          • Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

            – utubun
            Nov 24 '18 at 18:15










          1




          1





          great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

          – Matifou
          Nov 24 '18 at 17:57





          great, and nice choice of colours, thanks! (I think misses a call to library(ggthemes) ?)

          – Matifou
          Nov 24 '18 at 17:57













          Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

          – utubun
          Nov 24 '18 at 18:15







          Thanks. Sorry, I forget to uncomment ggthemes::theme_few(), I usually trying to remember that. I wish Hadley wouldn't hear it, but I hate default ggplot2 theme dearly, right from the bottom of my heart.

          – utubun
          Nov 24 '18 at 18:15




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455092%2fr-ggplot2-change-colour-of-font-and-background-in-facet-strip%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini