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

前端 未结 4 1569
独厮守ぢ
独厮守ぢ 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 05:49

    Following basically the same strategy used in this answer (and demo'd in the first example in the gridBase vignette (pdf)) you could use grid.text() to annotate the base graphics output.

    library(gridBase)
    
    ## Function that plots barplots with x-axes annotated with slanted
    ff <- function(x) {
        barcols <- c("red","blue")
    
        ## Plot, suppressing the labels
        bp <- barplot(matrix(dat[,x], nrow = 2, byrow = TRUE), xaxt = "n",
                      beside = TRUE, col = barcols)
        title(main=names(dat[x]))
        xaxislab <- c("Method-XXX", "Method-YYY", " Method-ZZZ",
                      "Method-XZZZ", " Method-XZZZY")
    
        ## Compute x-axis coordinate at center of each group
        bp <- colMeans(bp) 
    
        ## Use gridBase to compute viewport coordinates and
        ## grid to push/pop viewports and add the labels
        vps <- baseViewports()
        pushViewport(vps$inner, vps$figure, vps$plot)
        grid.text(xaxislab,
            x = unit(bp, "native"), y = unit(-0.5, "lines"),
            just = "right", rot = 45, gp=gpar(cex=0.7))
        popViewport(3)
    }
    
    ## Apply it to your data 
    dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
    layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
    sapply(3:6, ff)
    

    enter image description here

提交回复
热议问题