user3576287
user3576287

Reputation: 1032

How to manually choose the colors of a box plot with significance in ggplot2?

I have a data frame like this:

        values    TF group_num group
1   0.22722942 FALSE         5     5
2  -0.36530296  TRUE         3     3
3   0.38795866 FALSE         4     4
4   0.48439459 FALSE         4     4
5   0.93879881 FALSE         4     4
6  -0.03598468  TRUE         4     4
7  -0.49993149 FALSE         4     4
8   1.26200426 FALSE         5     5
9  -0.02348821  TRUE         4     4
10 -0.78135675 FALSE         4     4
11 -0.00466411 FALSE         2     2
12 -0.28493506  TRUE         3     3
13 -1.41763529  TRUE         4     4
14  2.03987995 FALSE         1     1
15 -0.70235537 FALSE         3     3
16  0.36176799 FALSE         2     2
17  0.22171013 FALSE         5     5
18 -0.56860951 FALSE         4     4
19 -0.51199794  TRUE         4     4
20 -0.92628365 FALSE         4     4

group_num = numeric
group = factors

So, when I want to create a boxplot which shows the significance by using geom_signif I got an error when I use this:

ggplot(test, aes(x = TF, y = values, fill=group_num)) +
  geom_boxplot() + 
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T) +
  facet_grid(~group_num) +
  scale_fill_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48")) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"),
        strip.text.x = element_blank())

Error: Continuous value supplied to discrete scale

because I use group_num which is numeric

However, when I use the group (factors) I got the output like this:

ggplot(test, aes(x = TF, y = values, fill=group)) + 
  geom_boxplot() +  
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T)+
scale_fill_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48"))+ 
          facet_grid(~group) + 
      scale_color_manual(values=c("firebrick2","darkorchid4","dodgerblue4","deepskyblue4","gray48")) +
      theme(panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank(),
            panel.background = element_blank(), 
            axis.line = element_line(colour = "black"),
            strip.text.x = element_blank())

enter image description here

However, I want to get the significant points for all groups. To do that if I use group_num without choosing colors it works. and I get an output like this:

ggplot(test, aes(x = TF, y = values, fill=group_num)) + 
  geom_boxplot() + 
  stat_signif(comparisons=list(c('TRUE','FALSE')),map_signif_level = T) + 
  facet_grid(~group_num) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        strip.text.x = element_blank())

enter image description here

So, my question is how can I use a group (as.factor) to get an output like the first picture with significance on the top for all different groups which I use facet_grid?

Upvotes: 0

Views: 2991

Answers (1)

timfaber
timfaber

Reputation: 2070

Try adding group as fill (as factor) variable in the geom_boxplot call:

ggplot(test, aes(x = TF, y = values)) + 
  geom_boxplot(aes(fill=group)) 

Upvotes: 1

Related Questions