堆积的barplot的颜色

问题描述 投票:0回答:1

我是一个有关堆积条形图颜色问题的新手。让我先说一下我在问这个问题之前已经搜索了答案,但是我对R绘图很新,我可能没有使用正确的搜索查询。

现在,我有一个矩阵,我想使用正负值绘制堆积条形图。我已经能够做到这一点(有一些帮助),但我似乎无法让酒吧接受不同颜色的正面和负面价值。以下是我写的:

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") 

由于某种原因,酒吧都是橙色的.barplot

数据矩阵如下:

    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

在堆积的条形图中是否可以使条形图颜色正值与负值不同?如果是这样,请您提供建议,如何这样做?

r colors bar-chart stacked
1个回答
0
投票

颜色可以在“scale_fill_manual”中逐个操作,像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)]

你需要ggplot2,这对图表非常有用。

#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()
© www.soinside.com 2019 - 2024. All rights reserved.