0
votes

I am a newbie with a question on color for stacked barplot. Let me preface this by saying I have searched for answers before asking this question, but I'm so new to R plotting that I may not be using the correct search query.

Now, I have a matrix that I would like to plot with a stacked barplot using positive and negative values. I've been able to do this (with some help), but I can't seem to get the bars to accept different colors for positive and negative values. Below is what I've written:

dataset<-as.matrix(read.csv("skin_2hr.csv", header=T, row.names=1))

colors <-c("139", "orange", "132","purple","navy","forestgreen")
barcenter<-barplot(t(dataset[,3:2]), density=c(10,40,20,40,20,40),
main="Skin 2hr Post Exposure", 
xlab=expression(paste("kJ/m"^"2",4100K FL")), ylab="number of genes", 
    names.arg=rownames(dataset), 
    ylim=c(-150,500), col=colors)
lines(barcenter,dataset[,1])
box()
legend("topleft", legend=rownames(dataset), col=colors,
   pch=15, bty="n") 

For some reason, the bars are all colored orange.barplot

The data matrix is below:

    Total   UP  DOWN
1   113    92   -21
2   216   130   -86
4   406   266   -140
8   183   136   -47
16  150   119   -31
32  178   144   -34

Is it possible to have barplot color positive values different from negative values in a stacked barplot? If so, could you please offer suggestions as how to do so?

1
Could you show us a dput(dataset) of your data? Or if very large a dput() of head(dataset, 20) - P1storius
Yes, here you go: - user10331445
structure(c(113L, 216L, 406L, 183L, 150L, 178L, 92L, 130L, 266L, 136L, 119L, 144L, -21L, -86L, -140L, -47L, -31L, -34L), .Dim = c(6L, 3L), .Dimnames = list(c("1", "2", "4", "8", "16", "32"), c("Total", "UP", "DOWN"))) - user10331445

1 Answers

0
votes

Colors can be manipulated one by one in the "scale_fill_manual" with a guide like this one

dataset<-structure(c(113L, 216L, 406L, 183L, 150L, 178L, 92L, 130L, 266L, 136L, 119L, 144L, -21L, -86L, -140L, -47L, -31L, -34L), .Dim = c(6L, 3L), .Dimnames = list(c("1", "2", "4", "8", "16", "32"), c("Total", "UP", "DOWN")))
dataset<-as.data.frame(dataset)
dataset$id<-rownames(dataset)
dataset<-cbind(melt(dataset)[7:18,],dataset$Total)
names(dataset)[names(dataset)=="dataset$Total"]<-"Total"
dataset$variable<-letters[1:nrow(dataset)]
dataset2<-dataset[1:6,c(1,4)]

You'll need ggplot2, which is very useful for graphs.

#install.packages("ggplot2")
library(ggplot2)
ggplot(dataset2, aes(factor(id,levels=dataset2$id), Total, group=1, colour=1)) + 
  geom_line(show.legend=F) + 
  geom_point(size=1, shape=16,show.legend=F) + 
  geom_bar(data=dataset, aes(x=id,y=value,fill=variable), stat="identity",position = "identity",show.legend=F)+
  ylab(paste("number of genes")) + xlab(expression("kJ / m"^"2"))+ 
  ggtitle("Skin 2hr Post Exposure")+
  guides(fill=FALSE)+#turns off color legend for above/below 0
  theme(legend.position="none")+
  #  scale_fill_brewer(palette = "Set3")+
  scale_fill_manual(values=c("orange", "black","green","yellow","white","red","pink","blue","brown","magenta","lightgray","lightblue"))+
  theme_bw()