How to add texture to fill colors in ggplot2

前端 未结 8 1199
误落风尘
误落风尘 2020-11-22 08:30

I\'m currently using scale_brewer() for fill and these look beautiful in color (on screen and via color printer) but print relatively uniformly as greys when us

相关标签:
8条回答
  • 2020-11-22 09:05

    I think Docconcoct work is great but now I've suddenly googled a special package --- Patternplot. Didn't see internal code but vignette seems useful.

    0 讨论(0)
  • 2020-11-22 09:05

    It might be useful to create a dummy data frame whose contours correspond to "textures" and then use geom_contour. Here is my example:

    library(ggplot2)
    
    eg = expand.grid(R1 = seq(0,1,by=0.01), R2 = seq(0,1,by=0.01))
         eg$importance = (eg$R1+eg$R2)/2
    
      ggplot(eg , aes(x = R1, y = R2)) +
      geom_raster(aes(fill = importance), interpolate=TRUE) +
      scale_fill_gradient2(low="white", high="gray20", limits=c(0,1)) +
      theme_classic()+
      geom_contour(bins=5,aes(z=importance), color="black", size=0.6)+
      coord_fixed(ratio = 1, xlim=c(0,1),ylim=c(0,1))
    

    And here is the result: shaded plot with lines

    (the lines should be smoothed)

    0 讨论(0)
  • 2020-11-22 09:14

    I've just discovered a package called ggpattern (https://github.com/coolbutuseless/ggpattern) that seems to be nice solution for this problem and integrates nicely with the ggplot2 workflow. While solutions using textures might work fine for diagonal bars, they will not produce vector graphics and are therefore not optimal.

    Here's an example taken straight from ggpattern's github repository:

    install.packages("remotes")
    remotes::install_github("coolbutuseless/ggpattern")
    
    library(ggplot2)
    library(ggpattern)
    
    df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
    
    ggplot(df) +
      geom_col_pattern(
        aes(level, outcome, pattern_fill = level), 
        pattern = 'stripe',
        fill    = 'white',
        colour  = 'black'
      ) +
      theme_bw(18) +
      theme(legend.position = 'none') + 
      labs(
        title    = "ggpattern::geom_pattern_col()",
        subtitle = "pattern = 'stripe'"
      ) +
      coord_fixed(ratio = 1/2)
    
    

    which results in this plot:

    If only some bars should be striped, geom_col_pattern() has a pattern_alpha argument that could be used to make certain unwanted stripes completely transparent.

    0 讨论(0)
  • 2020-11-22 09:17

    You can use ggtextures package by @claus wilke to draw textured rectangles and bars with ggplot2.

    # Image/pattern randomly selected from README
    path_image <- "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg"
    
    library(ggplot2)
    # devtools::install_github("clauswilke/ggtextures")
    ggplot(mtcars, aes(cyl, mpg)) + 
      ggtextures::geom_textured_bar(stat = "identity", image = path_image)
    

    You can also combine it with other geoms:

    data_raw <- data.frame(x = round(rbinom(1000, 50, 0.1)))
    ggplot(data_raw, aes(x)) +
      geom_textured_bar(
        aes(y = ..prop..), image = path_image
      ) +
      geom_density()
    

    0 讨论(0)
  • 2020-11-22 09:17

    ggrough might be of interest: https://xvrdm.github.io/ggrough/

    0 讨论(0)
  • 2020-11-22 09:20

    It's not currently possible because grid (the graphics system that ggplot2 uses to do the actual drawing) doesn't support textures. Sorry!

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