Geom_Bar: fill bars with a color gradient
3
0
Entering edit mode
6.3 years ago

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 • 8.2k views
ADD COMMENT
2
Entering edit mode
6.3 years ago

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.

ADD COMMENT
2
Entering edit mode
6.3 years ago
russhh 5.7k

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]

ADD COMMENT
0
Entering edit mode

It works perfectly. Thank you.

ADD REPLY
1
Entering edit mode

Good stuff - I was guessing

ADD REPLY
2
Entering edit mode
6.3 years ago
Russ ▴ 520

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.

ADD COMMENT

Login before adding your answer.

Traffic: 1424 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6