堆叠条形图的颜色
Colors for stacked barplot
我是新手,对堆叠条形图的颜色有疑问。让我先说一下,在问这个问题之前我已经搜索过答案,但我对 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
是否可以在堆叠条形图中使用不同于负值的条形图颜色正值?如果是这样,能否请您提供如何操作的建议?
颜色可以在 "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()
我是新手,对堆叠条形图的颜色有疑问。让我先说一下,在问这个问题之前我已经搜索过答案,但我对 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
是否可以在堆叠条形图中使用不同于负值的条形图颜色正值?如果是这样,能否请您提供如何操作的建议?
颜色可以在 "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()