Question: How to add colors to bar chart?
0
gravatar for Wuschel
10 months ago by
Wuschel130
HUJI
Wuschel130 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 • 507 views
ADD COMMENTlink modified 10 months ago by Prakash1.3k • written 10 months ago by Wuschel130
5
gravatar for Prakash
10 months ago by
Prakash1.3k
India
Prakash1.3k 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 10 months ago by genomax69k • written 10 months ago by Prakash1.3k
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 10 months ago • written 10 months ago by cpad011211k
1

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 10 months ago by Prakash1.3k

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

ADD REPLYlink written 10 months ago by Wuschel130

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

ADD REPLYlink written 10 months ago by Wuschel130
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: 1656 users visited in the last hour