115

私は運が悪い棒グラフで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)
4

8 に答える 8

306

オプションのパラメーター las=2 を使用します。

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

ここに画像の説明を入力

于 2015-01-11T13:39:23.830 に答える
64

DAVIDの応答ごとに編集された回答:

これは一種のハックな方法です。もっと簡単な方法があると思います。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)
于 2012-04-23T19:08:21.947 に答える
31

ベース グラフィックスを使用して、x 軸ラベルを 90 度以下の角度で回転させます。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)

ここに画像の説明を入力

于 2014-02-24T03:54:08.677 に答える
5

棒グラフのドキュメントでは...、関数呼び出しに渡すことができる追加のパラメーター ( )について読むことができます。

...    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.

45° ではありませんlas=2が、 pass によってラベルが垂直になるのはそのためです。

于 2019-12-18T22:14:25.460 に答える
1

アンドレ・シルバの答えは、「バープロット」行に1つの警告がありますが、私にとってはうまくいきます。

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

「xaxt」引数に注意してください。これがないと、ラベルは 2 回描画されます。1 回目は 60 度回転しません。

于 2016-05-03T22:14:04.760 に答える