Question: How to add colors to bar chart?
0
gravatar for Wox
2.1 years ago by
Wox340
HUJI
Wox340 wrote:

I'm interested in plotting faceted bar plots WT vs Mut gene. How can I improve my scripts to add colors? All attempts failed. WT -> Green bars and Mut -> orange bars. Also, I want to change the order of my faceting; Left WT, Right mut (3 raws * 2 col)

df <- structure(list(Genotype = c("mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT"), Case = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), Time = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Protein_Abundance = c(0.8, 0.8, 0.8, 0.8, 0.8, 1, 0.8, 0.6, 0.5, 0.3, 0.2, 0.4, 0.5, 0.6, 0.8, 1, 0.8, 0.7, 0.5, 0.4, 0.4, 0.6, 0.8, 0.9, 1, 0.9, 0.9, 0.9, 0.9, 0.9)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = list(Genotype = structure(list(), class = c("collector_character", "collector")), Case = structure(list(), class = c("collector_integer", "collector")), Time = structure(list(), class = c("collector_integer", "collector")), Protein_Abundance = structure(list(), class = c("collector_double", "collector"))), default = structure(list(), class = c("collector_guess", "collector"))), class = "col_spec"))

Scripts

library(reshape2)
library(plotly) 
library(readr) 
library(ggplot2)
df$Genotype <- factor(df$Genotype, levels = c("WT","mut")) 
colours <- c('#336600','#ff9900') 

ggplot(df,aes(x=Time, y = Protein_Abundance, col=Genotype)) + geom_bar(stat='identity', position='dodge') +scale_fill_manual(values=colours)+ facet_wrap(Genotype ~ Case)
R • 952 views
ADD COMMENTlink modified 2.1 years ago by Prakash1.9k • written 2.1 years ago by Wox340
5
gravatar for Prakash
2.1 years ago by
Prakash1.9k
India
Prakash1.9k wrote:

you have to factor case as well and fill color based on Genotype.. see below code

df$Genotype <- factor(df$Genotype, levels = c("WT","mut"))
df$Case <- factor(df$Case, levels = c("1","2","3"))
colours <- c('#336600','#ff9900') 

ggplot(df,aes(x=Time, y = Protein_Abundance,fill=Genotype)) + 
  geom_bar(stat='identity', position='dodge') +
  scale_fill_manual(values=colours)+ 
  facet_wrap(Case ~ Genotype,ncol = 2)

Rplot

ADD COMMENTlink modified 2.1 years ago by genomax90k • written 2.1 years ago by Prakash1.9k
1

does it need to be factor? could you run this code without making genotype a factor?

df <- structure(list(Genotype = c("mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "mut", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT"), Case = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), Time = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Protein_Abundance = c(0.8, 0.8, 0.8, 0.8, 0.8, 1, 0.8, 0.6, 0.5, 0.3, 0.2, 0.4, 0.5, 0.6, 0.8, 1, 0.8, 0.7, 0.5, 0.4, 0.4, 0.6, 0.8, 0.9, 1, 0.9, 0.9, 0.9, 0.9, 0.9)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = list(Genotype = structure(list(), class = c("collector_character", "collector")), Case = structure(list(), class = c("collector_integer", "collector")), Time = structure(list(), class = c("collector_integer", "collector")), Protein_Abundance = structure(list(), class = c("collector_double", "collector"))), default = structure(list(), class = c("collector_guess", "collector"))), class = "col_spec"))
library(ggplot2)
colours <- c('#336600','#ff9900') 
ggplot(df,aes(x=Time, y = Protein_Abundance, fill=Genotype)) + 
    geom_bar(stat='identity', position='dodge') +
    scale_fill_manual(values=colours)+ 
    facet_wrap(Genotype ~ Case)

Rplot

ADD REPLYlink modified 2.0 years ago • written 2.1 years ago by cpad011214k
2

yes I think it need to be a factor... I think BIOAWY wants to compare WT and mut in three different case. So to arrange each facet it need to be factor..

ADD REPLYlink written 2.1 years ago by Prakash1.9k

Thank you cpad012 :) Appreciate as always. I just needed to compare WT vs mut in 3 dif cases.

ADD REPLYlink written 2.1 years ago by Wox340

Thank you very much Prakash. I really do appreciate your kind help. Thanks again!

ADD REPLYlink written 2.1 years ago by Wox340
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: 1503 users visited in the last hour