This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Geom_Bar: fill bars with a color gradient

Hi all,

I have the following graph bar: genes vs. normalised gene counts (link bellow). I have the results appearing in an descending order. However the color gradient that I used to fill the bars doesn't appear in that descending order. Anyone can help me to fix it?

here is the code that I used:

ggplot(de_results_midline_highest_lowest [1:20,], aes(x = reorder (external_gene_name, norm_count_1), y = norm_count_1, fill= external_gene_name)) +
geom_bar(position="dodge", stat="identity") +
xlab("Gene") +
ylab("Normalized Counts") +
coord_flip()+
labs(color = "Gene") +
guides(fill=guide_legend(title= "Gene")) +
scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Spectral"))(20))

https://www.dropbox.com/s/b9pm706u1kvlzyx/Top_20_midline.png?dl=0

r

3 answers

Relevel external_gene_name to match the order you want or instead fill by norm_count. As an aside, you should get rid of the legend. The colors don't actually add anything, though I suppose people like colorful charts.

Your code:

ggplot(
    de_results_midline_highest_lowest [1:20,],
    aes(x = reorder (external_gene_name, norm_count_1), y = norm_count_1, fill= external_gene_name)) +
geom_bar(position="dodge", stat="identity") +
xlab("Gene") +
ylab("Normalized Counts") +
coord_flip()+
labs(color = "Gene") +
guides(fill=guide_legend(title= "Gene")) +
scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Spectral"))(20))

Have a look at fct_reorder from forcats

I think you can do the following:

library(forcats)
library(magrittr)
library(ggplot2)
library(dplyr)

de_results_midline_highest_lowest [1:20,] %>%
  mutate(
    external_gene_name = fct_reorder(as.factor(external_gene_name), norm_count_1)
  ) %>%
  ggplot(
    aes(x = external_gene_name, y = norm_count_1, fill= external_gene_name)
  ) +
  geom_bar(position="dodge", stat="identity") +
  xlab("Gene") +
  ylab("Normalized Counts") +
  coord_flip()+
  labs(color = "Gene") +
  guides(fill=guide_legend(title= "Gene")) +
  scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Spectral"))(20))

[You might not need the as.factor]

Good stuff - I was guessing

The order of the colouring is decided by the order (or, in R speak, the levels) of your factor variable "external_gene_name". By default, factors are ordered in alphabetical order (as shown in the legend). You need to re-level the factor to reflect that the order is based on "norm_count_1".

Check the "factor" function in R for more info - or if you post some of your dataframe, we can probably help with the code.

This may work:

new_df <- de_results_midline_highest_lowest[1:20]
new_df$external_gene_name_2 <- factor(new_df$external_gene_name, levels = new_df[order(new_df$norm_count_1, decreasing = T),])

Then use ggplot2 to plot the new_df data frame, and use the "external_gene_name_2" as your fill variable.

Log in to answer this question.