在R中绘制双坐标轴(双y轴)的方法

为了方便演示,先虚构一套数据,如下所示:

1
2
3
4
d <- data.frame(name=c("zhao","qian","sun","li"),weight=c(62,58,79,60),height=c(178,169,180,173))
x <- d$name
y1 <- d$weight
y2 <- d$height
  1. 使用plotrix包

    • 程序代码
    1
    2
    3
    4
    5
    
    #png("2y_1.png")
    library(plotrix)
    xpos <- 1:4
    twoord.plot(xpos,y1,xpos,y2,xlim=c(0,5),lylim=c(0,100),rylim=c(100,190), lcol=4,rcol=2,xlab="name",ylab="Weight (kg)",rylab="Height (cm)",type=c("bar","b"),xticklab=x,halfwidth=0.2)
    #dev.off()
    • 输出图片

    2y_1

  2. 使用TeachingDemos包

    • 程序代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    #png("2y_2.png")
    library(TeachingDemos)
    par(mar=c(5,5,4,5)+0.1)
    bar <- barplot(y1,ylim=c(0,100),ylab="Weight (kg)",col="blue",col.axis="blue",col.lab="blue")
    updateusr(1:2,range(0,100),1:2,range(100,190))
    lines(bar,y2,type="b",col="red")
    axis(4,col="red",col.ticks="red",col.axis="red")
    mtext("Heigth (cm)",side=4,line=3,col="red")
    mtext(x,side=1,line=1,at=bar,col="black")
    #dev.off()
    • 输出图片

    2y_2

  3. 不使用包

    • 程序代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    #png("2y_3.png")
    par(mar=c(5,5,4,5)+0.1)
    bar <- barplot(y1,xlim=c(0,5),ylim=c(0,100),ylab="Weight (kg)",col="blue",col.axis="blue",col.lab="blue")
    mtext(x,side=1,line=1,at=bar,col="black")
    mtext("Name",side=1,line=3,col="black")
    par(new=T)
    plot(bar,y2,axes=F,xlim=c(0,5),ylim=c(100,190),xlab="",ylab="",col="red",type="b")
    axis(4,col="red",col.ticks="red",col.axis="red")
    mtext("Heigth (cm)",side=4,line=3,col="red")
    #dev.off()
    • 输出图片

    2y_3

  4. 参考资料