How to add colors to bar chart?
1
0
Entering edit mode
6.2 years ago
WUSCHEL ▴ 810

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 • 2.2k views
ADD COMMENT
5
Entering edit mode
6.2 years ago
Prakash ★ 2.2k

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 COMMENT
1
Entering edit mode

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 REPLY
2
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1896 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