1

Good morning,

I am looking for a way to add patterns on my r boxplots. I have seen that some solutions exist already (here for exemple) but I have also find a more straitforward way to do this (here). However, I cannot manage to reproduce the exemple given hereby.

structure(list(day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("d0", 
"db"), class = "factor"), name = structure(1:20, .Label = c("x1", 
"x2", "x3", "x4", "x5", 
"x6", "x7", "x8", "x9", 
"x10", "x11", "x12", "x13", 
"x14", "x15", "x16", "x17", 
"x18", "x19", "x20", "x21", "x22", 
"x23", "x24", "x25", "x26", 
"x27", "x28", "x29", "x30", 
"x31", "x32", "x33", "x34", 
"x35", "x36", "x37", "x38", "x39"
), class = "factor"), mean = c(36.6, 36.2636363636364, 36.9285714285714, 
35.325, 37.0692307692308, 37.2357142857143, 36.85, 36.0153846153846, 
35.6384615384615, 36.76, 36.8538461538462, 35.4307692307692, 
35.3, 36.8153846153846, 36.1666666666667, 37.775, 37.3733333333333, 
36.4666666666667, 36.6071428571429, 36.6)), row.names = c(NA, 
-20L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "day", drop = TRUE, indices = list(
    0:19), group_sizes = 20L, biggest_group_size = 20L, labels = structure(list(
    day = structure(1L, .Label = c("d0", "db"), class = "factor")), row.names = c(NA, 
-1L), class = "data.frame", vars = "day", drop = TRUE))

What I have so far are the following lines:

patternboxplot(data=temp_mean, x=temp_mean$day, y = temp_mean$mean, group = NULL,
                  pattern.type = c('nwlines', 'blank'), pattern.line.size = c(6,1), 
                  pattern.color = c('black', 'white'),
                  background.color = c('white', 'white'),
                  frame.color = c('black', 'black'),
                  density=c(6,1))

When running it, I get the following error:

"Error in imagetodf2(readPNG(paste(location, "/", pattern.type[i], ".png", : object 'xmin' not found".

If anyone has a clue about this I'll be glad to hear about it!

Thank you in advance.

EDIT: I would like to plot the following figure (obtained here using the exact same dataset and gglpot2 package)

enter image description here

Blue
  • 47
  • 8

2 Answers2

2

Simulate something like your data:

temp_mean = data.frame(
  day = sample(c("d0","db"),100,replace=TRUE),
  mean = rnorm(100)
)

I tried but cannot get them to have different fills, with group per x-axis break. You can try something like below (maybe not ideal):

 patternboxplot(data=temp_mean, x=1, y = temp_mean$mean, group = temp_mean$day,
               pattern.type = c('nwlines', 'blank'), pattern.line.size = c(6,1), 
               pattern.color = c('black', 'white'),
               background.color = c('white', 'white'),
               frame.color = c('black', 'black'),
               density=c(6,1),
               legend.h=30,legend.w=0.05,legend.x.pos=1.075, 
               legend.y.pos=0.88, 
               legend.pixel=10)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • I'll have to work a bit around legend positionning but it is working! Thank you very much. Nevertheless, I wonder how putting "x=1" does the trick. Is there any explanation behind this? – Blue Feb 13 '20 at 12:48
  • In ggplot2, if you set for example aes(x=1,y=..,fill=group), it will create a boxplot with only 1 axis tick at 1, and two boxplots separated by groups. You are saying the x is a constant, hence this result. In patternboxplot, the group goes with the fill, so given your data, and what you need, I just tried it this way – StupidWolf Feb 13 '20 at 13:16
  • @Blue, how did you position the legend in the end? I tried a few times but it was not easy. You could add you final plotting command as an answer too, in case people find it usefull :) – StupidWolf Feb 13 '20 at 13:19
  • Thanks for the explanation (: As for the legend I'm stuck. When I change values for x and y position, only labels (texts "d0" and "db") are moving and not the "boxes" which stay at the top of the graph. – Blue Feb 13 '20 at 13:49
0

This will not crash, however you will have to experiement yourself with the pattern of your liking.

patternboxplot(data=temp_mean, x=factor(temp_mean$day), y = as.numeric(temp_mean$mean),group=NULL,
               pattern.type = c('nwlines'), pattern.line.size = c(6,1), 
               pattern.color = c('black'),
               background.color = c('white'),
               frame.color = c('black'),
               density=c(6,1))
  • When trying the code you gave me I have the exact same error message than the one before... Could the problem comes from the pre-treatment of my data? I computed the means by using ```dplyr``` package – Blue Feb 13 '20 at 10:33