如何用R绘制双y轴柱状图

2020-08-05 09:42:56 浏览数 (1)

绘制上面这个漂亮的图,要注释以下3点:

1. 双y轴

2. 颜色渐变

3. 横坐标的label对齐

代码及解释如下:

代码语言:javascript复制
library(RColorBrewer)
# 载入颜色包
par(mar=c(5,4,2,6))
#控制图形四周的边距
data<-t(as.matrix((1:26)*10))

x<-c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R", "S","T","U","V","W","X","Y","Z")

colnames(data)<-x

data1<-data[,order(data,decreasing=T)]

x1<-x[order(data,decreasing=T)]

barplot(data1,0.8,col= colorRampPalette(brewer.pal(9,"Greens"))(26),las=1, ylab="Percent of Genes",axes=F,width=0.5)

#绘制无坐标轴的条形图,实现颜色的渐变

axis(2,las=1)

#添加左侧Y轴

par(new=T)

axis(4,col="black",las=1)

#添加右侧Y轴

title("KOGs Functional Categories")

#添加标题

mtext(side=4,padj=5,"Percent Genes")

#添加右侧Y轴标记

很简单吧?把data换成你自己的数据试试吧~

0 人点赞