1
votes

I want to visualize the interaction between two factors in a barplot using either color or texture.

I first plotted the difference between two levels of one factor (score), and now I want to split these levels based on a second factor (group). My original plot showed the difference between score type 1 and 2 by coloring the bars different (black vs white). In my second plot, I'd still like to emphasize this coloring scheme, but I'm not sure how to do this.

Here's some example code:

## Example data, data from two groups: patients and controls
    data_ex <- data.frame( pnum = c(1,2,3,4,5,6,7,8,9,10),
               group = c("patient", "patient","patient","patient","patient",
"control", "control", "control", "control", "control"),
               score1 = c(26, 15, 17, 15, 20, 21, 18, 19, 16, 20),
               score2 = c(25, 19, 14, 18, 20, 22, 17, 19, 18, 19))

## Reshape the data
    data_ex_long <- data_ex %>% gather(key = type_score, value = score, score1, score2)

My first plot looks like this:

 ggplot(data=data_ex_long,aes(x = type_score, y = score)) +  
          geom_bar( width=.3, stat = "summary",  colour="black", size=1.5, fill =c("#252525","#f0f0f0"), position = position_dodge(width = 0.05),lwd=0.2) +
          geom_point( size = 3.5, position=position_jitter(width=0.05), alpha = 0.8, fill="#bdbdbd", colour = "black", shape = 21) +
          stat_boxplot(geom = "errorbar", width = 0.0) +
          theme(text = element_text(size = 18),
                axis.text.x  = element_text(size=15, color="#000000"),     
                axis.text.y  = element_text(size=20, color="#000000"),       
                axis.title.x = element_blank(),  
                axis.title.y = element_text(size=18, color="#000000"),
                axis.line.x  = element_line(colour = "black", size = 1.3), 
                axis.line.y  = element_line(colour = "black", size = 1.3),
                panel.border = element_blank(),
                panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(),
                panel.background = element_blank(),
                axis.ticks.length = unit(.25, "cm"),
                axis.line = element_line())

bar plot

Now I would like to split the factors based on the group variable, like so:

        ggplot(data=data_ex_long,aes(x = type_score, y = score, fill = group)) +  
          geom_bar(stat = "summary",fun.y = "mean", colour="black", size=1.5, position = position_dodge(width = 1)) +
          geom_point(aes(type_score, fill = group), colour="black", size = 3.5, shape = 21, position = 
          position_jitterdodge(jitter.width = 0.2, jitter.height=0.4, dodge.width=0.9), alpha = 0.8) +
          geom_errorbar(aes(size=2),stat = 'summary', position = 'dodge', color = "black", width = 1, size = 1) +
          theme(text = element_text(size = 18),
                axis.text.x  = element_text(size=15, color="#000000"),     
                axis.text.y  = element_text(size=20, color="#000000"),       
                axis.title.x = element_blank(),  
                axis.title.y = element_text(size=18, color="#000000"),
                axis.line.x  = element_line(colour = "black", size = 1.3), 
                axis.line.y  = element_line(colour = "black", size = 1.3),
                panel.border = element_blank(),
                panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(),
                panel.background = element_blank(),
                axis.ticks.length = unit(.25, "cm"),
                axis.line = element_line())

interaction plot

I'm not happy with the coloring this way. I would like the first two bars related to "score1" to be black (or at least dark gray) and the second two bars related to "score2" to be white. Then, I would like to highlight the first and third bar, as being related to the "control" group, either by changing the texture within the bar (stripes?) or in some other way (thicker lines?). Any tips on how I can adjust the looks of the bars individually?

EDIT: Here's a very ugly example made in paint of something I imagined: example plot

1

1 Answers

1
votes

This should help with the color part of your question (I removed all for the question irrelevant parts of the code):

ggplot(data=data_ex_long,aes(x = type_score, y = score, fill = type_score, group = group )) +  
  geom_bar(aes(col = group),stat = "summary",fun.y = "mean", size=1.5, position = position_dodge(width = 1)) +
  geom_point(aes(type_score, fill = type_score), colour="black", size = 3.5, shape = 21, position = 
               position_jitterdodge(jitter.width = 0.2, jitter.height=0.4, dodge.width=0.9), alpha = 0.8)  +
  scale_fill_manual(values = c("grey", "white", "grey", "white")) + 
  scale_color_manual(values = c("darkgreen", "steelblue"))

enter image description here

Regarding the textures I found a package called patternplot (available on cran) which might help with this. It produces ggplot2 objects, however, the syntax seems strange. You could also check the github repository clauswilke/ggtextures .