Question: Geom_Bar: fill bars with a color gradient
0
gravatar for dianalves17
2.5 years 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 • 5.9k views
ADD COMMENTlink modified 2.5 years ago by Russ470 • written 2.5 years ago by dianalves170
2
gravatar for Devon Ryan
2.5 years ago by
Devon Ryan98k
Freiburg, Germany
Devon Ryan98k 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 2.5 years ago by Devon Ryan98k
2
gravatar for russhh
2.5 years ago by
russhh5.5k
UK, U. Glasgow
russhh5.5k 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 2.5 years ago • written 2.5 years ago by russhh5.5k

It works perfectly. Thank you.

ADD REPLYlink written 2.5 years ago by dianalves170
1

Good stuff - I was guessing

ADD REPLYlink written 2.5 years ago by russhh5.5k
2
gravatar for Russ
2.5 years ago by
Russ470
Ontario Veterinary College, University of Guelph, Guelph, Ontario, Canada
Russ470 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 2.5 years ago by Russ470
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: 1822 users visited in the last hour
_