rggplot2colorsggpattern

Patterns in filled contours and striped colors in R


Let me introduce you my big problem: I have to create two filled contour graphs and then overlap them in order to show where intersections happen. In order to be clear, I'd need to use "stripes" instead of colors (or colored stripes) and add a transparency coefficient. The problem is not with the transparency of course, but with the stripes. Neither the basic graph nor ggplot2 give this possibility, and unfortunately ggpattern did not think about the "geom_contour_filled()" function, so that a function to stripe the areas in filled contour does not exist. So I was wondering: is there anything that allows to create a color in stripes? Or a striped color? Or maybe, something already existing? Unfortunately, I am not a developer and I do not know how to address this problem.

On the basic R graphing, I tried if something like density and angle parameters (of the function "polygon()") existed for the filled.contour() function and eventually how to apply them. About ggplot2, instead, I tried with the ggpattern package but there was not a way to use the relative scale manual functions to i, because a pattern function for the geom_contour_filled() does not exist. Of course, I already checked all the questions that have been made in Stack overflow and virtually in any community.

EDIT: following the advice of @jpsmith I add an example of (not working) code using ggplot:

library(ggplot2)
df<-data.frame(pi1=a$Var1, pi2=a$Var2, val=c(uncmom0303))
df<-data.frame(pi1=a$Var1, pi2=a$Var2, val=c(uncmom0303))
v<-ggplot(df, aes(pi1,pi2,z=val))
v+ 
  geom_contour_filled(bins = 2)+
  scale_color_manual(labels = c("Agreement", "No agreement"), values = c("grey32", "grey68"), aesthetics = "fill")+
  scale_pattern_fill_manual(values = "stripes")

the code is not working in the sense it does not create a pattern, but the area is normally shown. Even deleting "scale_colour_manual()" and leaving only "scale_pattern_fill_manual()" does not work, since the latter does not accept values to define colors and labels.

In the image you can see an example of filled contour (with ggplot2): here is an example of filled contour with ggplot2 in greyscale:

My question is: is there a way to show the filled areas as a striped area (even with the stripes of the same colour of the filled area of the graph)? Or, also, is there a way to create coloured stripes in R and use it as it was a colour?


Solution

  • It is possible to use ggpattern here. The secret is to use stat_contour_filled, with geom = 'polygon_pattern'.

    We don't have your data to demonstrate, but we can create a dummy data frame with the same names as your data set which should hopefully do the trick:

    set.seed(10)
    dens <- MASS::kde2d(runif(4), runif(4), n = 100)
    df <- cbind(expand.grid(pi1 = dens$x, pi2 = dens$y), val = c(dens$z))
    

    Now, if we plot this using standard geom_contour_filled, we get the standard viridis fills:

    library(ggplot2)
    library(ggpattern)
    
    ggplot(df, aes(pi1, pi2, z = val)) +
      geom_contour_filled(bins = 2)
    

    enter image description here

    But if we want this in black-and-white with patterned fills instead of solid color fills, we can do:

    ggplot(df, aes(pi1, pi2, z = val)) +
      stat_contour_filled(geom = 'polygon_pattern', bins = 2,
                          aes(pattern = after_stat(level),
                              fill = after_stat(level)), 
                          pattern_spacing = 0.03,
                          pattern_size = 0, pattern_angle = 45,
                          pattern_fill = 'black', pattern_color = 'black') +
      scale_pattern_manual(NULL, values = c('circle', 'stripe'),
                           labels = c('Disagree', 'Agree')) +
      scale_fill_manual(NULL, values = c('white', 'gray85'),
                        labels = c('Disagree', 'Agree')) +
      theme_classic() +
      coord_cartesian(expand = FALSE)
    

    enter image description here

    And you can change both the background color and the pattern color however you see fit:

    ggplot(df, aes(pi1, pi2, z = val)) +
      stat_contour_filled(geom = 'polygon_pattern', bins = 2,
                          aes(pattern = after_stat(level),
                              fill = after_stat(level),
                              pattern_fill = after_stat(level)), 
                          pattern_spacing = 0.03, pattern_angle = 45,
                          pattern_size = 0) +
      scale_pattern_manual(NULL, values = c('circle', 'stripe'),
                           labels = c('Disagree', 'Agree')) +
      scale_fill_manual(NULL, values = c('steelblue', 'pink'),
                        labels = c('Disagree', 'Agree')) +
      scale_pattern_fill_manual(NULL, values = c('blue4', 'purple3'),
                                labels = c('Disagree', 'Agree')) +
      theme_classic() +
      coord_cartesian(expand = FALSE)
    

    enter image description here