簡體   English   中英

在 R 中為條形圖旋轉 x 軸標簽

[英]Rotating x axis labels in R for barplot

我試圖讓 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)

使用可選參數 las=2 。

barplot(mytable,main="Car makes",ylab="Freqency",xlab="make",las=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)

使用基本圖形以等於或小於 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)

在此處輸入圖片說明

您可以簡單地將數據框傳遞到以下函數中

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)

在此處輸入圖片說明

您可以根據需要更改標簽的旋轉角度

您可以使用

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

它寫在這里: http : //www.statmethods.net/graphs/bar.html

您可以使用 ggplot2 旋轉 x 軸標簽添加一個附加層

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

在 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是正確的答案。

安德烈席爾瓦的回答對我很有用,在“barplot”行中有一個警告:

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

注意“xaxt”參數。 沒有它,標簽被繪制兩次,第一次沒有旋轉 60 度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM