This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Changing barplot color on enrichment-based data

Hello,

I ran a enrichment analysis using clusterprofiler, like this :

upregulated_enrich <- enrichGO(
  gene         = rownames(upregulated_genes),
  OrgDb        = org.Hs.eg.db,
  keyType      = "SYMBOL",
  ont          = "BP",
  pAdjustMethod = "BH",
  pvalueCutoff = 0.05,
  qvalueCutoff = 0.05
)

Then I made a bar plot out of the result:

barplot(upregulated_enrich, x = "Count", color = "p.adjust", showCategory = 15, font.size = 10, cex.names = 1.5)

The problem is, the color is a gradient going from red to blue, and I would like it to be in grey.

I tried several things, as I understood barplot made a ggplot object I tried to add scale_color_gradient but it doesn't work. I also tried to add the argument col = c("black", "grey") to the barplot function but it does not change the color.

Can anybody help ? Thank you in advance

ggplot2

As an update, I tried to do the plot directly through ggplot, like this :

ggplot(upregulated_enrich_df, aes(x=Description, y=Count, fill = p.adjust)) + 
    geom_bar(stat = "identity") +
    coord_flip()

But I get the following error :

Continuous values supplied to discrete scale. Example values: 2.06704461473967e-28, 8.9145806694221e-27, 2.01985496772942e-26, 9.43537110094994e-21, and 1.45468202900121e-20

I'm trying to color the bars of the plot according to the adjusted p-value, but it's a continuous coloring and not a discrete one, any way to fix this ?

Many thanks

1 answer

Did you try scale_fill_gradient?

barplot_object <- barplot(upregulated_enrich, x = "Count", color = "p.adjust", showCategory = 15, font.size = 10, cex.names = 1.5)

# Adjusting the color to be in grey scale
barplot_object + scale_fill_gradient(low = "black", high = "grey")
## scale_fill_gradient worked for me, though the gradient is limited, for me it only showed 3 colors

fit <- plot(barplot(GO_results_BP,showCategory = 10)+ scale_fill_gradient(low = "black", high = "lightgrey")) 

## Another related option is scale_fill_gradientn, then you can set a specific palette

fit <- plot(barplot(GO_results_BP,showCategory = 10)+ scale_fill_gradientn(colours = gray.colors(10))) 

Log in to answer this question.