Question: Geom_Bar: fill bars with a color gradient
0
gravatar for dianalves17
21 months ago by
Germany/Dresden/MPI-CBG
dianalves170 wrote:

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 • 4.4k views
ADD COMMENTlink modified 21 months ago by Russ460 • written 21 months ago by dianalves170
2
gravatar for Devon Ryan
21 months ago by
Devon Ryan95k
Freiburg, Germany
Devon Ryan95k wrote:

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 COMMENTlink written 21 months ago by Devon Ryan95k
2
gravatar for russhh
21 months ago by
russhh5.4k
UK, U. Glasgow
russhh5.4k wrote:

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 COMMENTlink modified 21 months ago • written 21 months ago by russhh5.4k

It works perfectly. Thank you.

ADD REPLYlink written 21 months ago by dianalves170
1

Good stuff - I was guessing

ADD REPLYlink written 21 months ago by russhh5.4k
2
gravatar for Russ
21 months ago by
Russ460
Ontario Veterinary College, University of Guelph, Guelph, Ontario, Canada
Russ460 wrote:

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 COMMENTlink written 21 months ago by Russ460
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1805 users visited in the last hour