rotate X axis labels 45 degrees on grouped bar plot R

前端 未结 3 627
心在旅途
心在旅途 2020-12-17 22:43

How can I rotate the X axis labels 45 degrees on a grouped bar plot in R?

I have tried the solution suggested here but got something very messy, the labels seem to h

相关标签:
3条回答
  • 2020-12-17 22:52

    Try the first answer:

    x <- barplot(table(mtcars$cyl), xaxt="n")
    labs <- paste(names(table(mtcars$cyl)), "cylinders")
    text(cex=1, x=x-.25, y=-1.25, labs, xpd=TRUE, srt=45)
    

    But change cex=1 to cex=.8 or .6 in the text() function:

    text(cex=.6, x=x-.25, y=-1.25, labs, xpd=TRUE, srt=45)
    

    In the picture you posted, it appears to me that the labels are just too big. cex sets the size of these labels.

    0 讨论(0)
  • 2020-12-17 23:11

    I am not a base plot proficient, so maybe my solution is not very simple. I think that using ggplot2 is better here.

    enter image description here

    def.par <- par(no.readonly = TRUE)
    
    ## divide device into two rows and 1 column 
    ## allocate figure 1  for barplot
    ## allocate figure 2 for barplot labels
    ## respect relations between widths and heights
    
    nf <- layout(matrix(c(1,1,2,2),2,2,byrow = TRUE), c(1,3), c(3,1), TRUE)
    layout.show(nf)
    
    ## barplot 
    par(mar = c(0,1,1,1))
    set.seed(1)
    nKol <- 8  ## you can change here but more than 11 cols 
               ## the solution is not really readable
    data <- matrix(sample(1:4,nKol*4,rep=TRUE),ncol=nKol)
    xx <- barplot(data, beside=TRUE,
                  col=c("darkred","red","grey20","grey40"))
    
    ## labels , create d ummy plot for sacles
    par(mar = c(1,1,0,1))
    plot(seq_len(length(xx)),rep(1,length(xx)),type='n',axes=FALSE)
    ## Create some text labels 
    labels <- paste("Label", seq_len(ncol(xx)), sep = " ")
    ## Plot text labels with some rotation at the top of the current figure
    text(seq_len(length(xx)),rep(1.4,length(xx)), srt = 90, adj = 1,
         labels = labels, xpd = TRUE,cex=0.8,srt=60,
         col=c("darkred","red","grey20","grey40"))
    
    par(def.par)  #- reset to default
    
    0 讨论(0)
  • 2020-12-17 23:11

    I had the same problem with a grouped bar plot. I assume that you only want one label below each group. I may be wrong about this, since you don't state it explicitly, but this seems to be the case since your labels are repeated in image. In that case you can use the solution proposed by Stu although you have to apply colMeans to the x variable when you supply it to the text function:

    x <- barplot(table(mtcars$cyl), xaxt="n")
    labs <- paste(names(table(mtcars$cyl)), "cylinders")
    text(cex=1, x=colMeans(x)-.25, y=-1.25, labs, xpd=TRUE, srt=45)
    
    0 讨论(0)
提交回复
热议问题