【问题标题】:Rotating x axis labels in R for barplot在R中为条形图旋转x轴标签
【发布时间】:2012-05-04 09:57:00
【问题描述】:

我试图让 x 轴标签在条形图上旋转 45 度,但没有运气。这是我下面的代码:

barplot(((data1[,1] - average)/average) * 100,
        srt       = 45,
        adj       = 1,
        xpd       = TRUE,
        names.arg = data1[,2],
        col       = c("#3CA0D0"),
        main      = "Best Lift Time to Vertical Drop Ratios of North American Resorts",
        ylab      = "Normalized Difference",
        yaxt      = 'n',
        cex.names = 0.65,
        cex.lab   = 0.65)

【问题讨论】:

    标签: r graph plot bar-chart axis-labels


    【解决方案1】:

    使用可选参数 las=2 。

    barplot(mytable,main="Car makes",ylab="Freqency",xlab="make",las=2)
    

    【讨论】:

    • 我相信这应该是公认的答案。使用问题中使用的基本 barplot 函数的参数完美工作。
    • 同意,这应该是公认的答案。更简洁的解决方案
    • 使用 par(mar=c(15,4,4,2)) 调整边距,使垂直标签不会从图中裁剪。
    • 我个人更喜欢这种方法,但它没有回答 OP 的原始问题:我正在尝试让 x 轴标签在条形图上旋转 45 度跨度>
    • 你可以看到'make'被标签覆盖。如何解决?
    【解决方案2】:

    根据大卫的回复编辑答案:

    这是一种骇人听闻的方式。我猜有一个更简单的方法。但是您可以通过从barplot 保存条形位置来抑制条形标签和标签的绘图文本,并进行一些上下调整。以下是 mtcars 数据集的示例:

    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)
    

    【讨论】:

    • 警告:如果您使用beside = TRUE,如果您希望每组只有一个标签,您可能希望使用colMeans(x) 而不是仅使用x .
    【解决方案3】:

    使用基本图形以等于或小于 90 度的角度旋转 x 轴标签。代码改编自R FAQ

    par(mar = c(7, 4, 2, 2) + 0.2) #add room for the rotated labels
    
    #use mtcars dataset to produce a barplot with qsec colum information
    mtcars = mtcars[with(mtcars, order(-qsec)), ] #order mtcars data set by column "qsec"
    
    end_point = 0.5 + nrow(mtcars) + nrow(mtcars) - 1 #this is the line which does the trick (together with barplot "space = 1" parameter)
    
    barplot(mtcars$qsec, col = "grey50", 
            main = "",
            ylab = "mtcars - qsec", ylim = c(0,5 + max(mtcars$qsec)),
            xlab = "",
            space = 1)
    #rotate 60 degrees (srt = 60)
    text(seq(1.5, end_point, by = 2), par("usr")[3]-0.25, 
         srt = 60, adj = 1, xpd = TRUE,
         labels = paste(rownames(mtcars)), cex = 0.65)
    

    【讨论】:

    • text() 有什么用? axis() 不起作用?
    【解决方案4】:

    您可以简单地将数据框传递给以下函数

    rotate_x <- function(data, column_to_plot, labels_vec, rot_angle) {
        plt <- barplot(data[[column_to_plot]], col='steelblue', xaxt="n")
        text(plt, par("usr")[3], labels = labels_vec, srt = rot_angle, adj = c(1.1,1.1), xpd = TRUE, cex=0.6) 
    }
    

    用法:

    rotate_x(mtcars, 'mpg', row.names(mtcars), 45)
    

    您可以根据需要更改标签的旋转角度

    【讨论】:

      【解决方案5】:

      你可以使用

      par(las=2) # make label text perpendicular to axis
      

      写在这里:http://www.statmethods.net/graphs/bar.html

      【讨论】:

        【解决方案6】:

        您可以使用 ggplot2 旋转 x 轴标签添加一个附加层

        theme(axis.text.x = element_text(angle = 90, hjust = 1))
        

        【讨论】:

          【解决方案7】:

          在 Bar Plots 的文档中,我们可以了解到可以传递给函数调用的附加参数 (...):

          ...    arguments to be passed to/from other methods. For the default method these can 
                 include further arguments (such as axes, asp and main) and graphical 
                 parameters (see par) which are passed to plot.window(), title() and axis.
          

          在图形参数的文档(par的文档)中我们可以看到:

          las
              numeric in {0,1,2,3}; the style of axis labels.
          
              0:
                always parallel to the axis [default],
          
              1:
                always horizontal,
          
              2:
                always perpendicular to the axis,
          
              3:
                always vertical.
          
              Also supported by mtext. Note that string/character rotation via argument srt to par does not affect the axis labels.
          

          这就是为什么传递 las=2 会使标签垂直,尽管不是 45°

          【讨论】:

          • 垂直不是45度!
          • @Tomas 很公平,我编辑了最后一句话。
          • 重点是这不是问题的答案。
          【解决方案8】:

          安德烈席尔瓦的回答对我很有用,在“条形图”行中有一个警告:

          barplot(mtcars$qsec, col="grey50", 
              main="",
              ylab="mtcars - qsec", ylim=c(0,5+max(mtcars$qsec)),
              xlab = "",
              xaxt = "n", 
              space=1)
          

          注意“xaxt”参数。没有它,标签会被绘制两次,第一次没有旋转 60 度。

          【讨论】:

          • 45度旋转在哪里?
          猜你喜欢
          • 2017-08-30
          • 2013-12-13
          • 2018-12-31
          • 1970-01-01
          • 2018-11-12
          • 2017-07-01
          • 2020-12-04
          相关资源
          最近更新 更多