How to show all the labels in X-axis 45 degree in R 2x2 bar plot

前端 未结 4 1549
独厮守ぢ
独厮守ぢ 2020-12-20 05:20

With the following data:

Method  Metric  E0  E1  E2  E4
Method-XXX  Precision   0.9661017   0.9622642   1   0.9655172
Method-YYY  Precision   0.533   0.535           


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 06:06

    add to rotate 45 degrees

    dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
    layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
    barcols <- c("red","blue")
    
    sapply(3:6, 
           function(x) {
             #par(las = 2)
             bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols)
             title(main=names(dat[x]))
            #axis(1,at=colMeans(bp),lwd=0,lwd.tick=1,srt=45)
            text(colMeans(bp), par("usr")[3] , labels = c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY"), srt = 45, pos = 1, xpd = TRUE)
    
    
             abline(h=0)
           }
    )
    
    plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
    legend(0,0.6,c("Precision","Recall"),fill=barcols,cex=1.5)
    

    add this to rotate 180 degrees the labels par(las = 2)

    dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
        layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
        barcols <- c("red","blue")
    
    sapply(3:6, 
           function(x) {
    #add this to rotate the labels
             par(las = 2)
             bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols)
             title(main=names(dat[x]))
             axis(1,at=colMeans(bp),c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY"),lwd=0,lwd.tick=1)
             abline(h=0)
           }
    )
    
    plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
    legend(0,0.6,c("Precision","Recall"),fill=barcols,cex=1.5)
    

提交回复
热议问题