在R中旋转x轴标签以显示条形图

Dav*_*vid 84 plot r graph bar-chart axis-labels

我试图让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)
Run Code Online (Sandbox Code Playgroud)

Mic*_*l Z 242

使用可选参数las = 2.

barplot(mytable,main="Car makes",ylab="Freqency",xlab="make",las=2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 我个人更喜欢这种方法,但它没有回答OP的原始问题:_我试图让x轴标签在barplot上旋转45度_ (22认同)
  • 使用par(mar = c(15,4,4,2))来调整边距,以便不从图中裁剪垂直标签. (15认同)
  • 我相信这应该是公认的答案。使用问题中使用的基本 barplot 函数的参数可以完美地工作。 (4认同)

Tyl*_*ker 56

大卫回应的编辑答案:

这是一种hackish方式.我猜这是一种更简单的方法.但您可以通过保存条形位置来抑制条形标签和标签的绘图文本,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)
Run Code Online (Sandbox Code Playgroud)

  • _caveat_:如果您使用的是`beside = TRUE',那么如果每个组只需要一个标签,则可能要使用`colMeans(x)`而不是`x`。 (2认同)

And*_*lva 28

如果要以等于或小于90的角度旋转x轴标签,请尝试以下方法:

它使用barplot的参数space=1使列的宽度等于列的间隔空间.

通过这种方式,可以根据Tyler Rinker的答案调整由@BenBarnes精确定位的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" (source: http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r)

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Cyb*_*tic 7

您只需将数据框传递到以下函数:

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) 
}
Run Code Online (Sandbox Code Playgroud)

用法:

rotate_x(mtcars, 'mpg', row.names(mtcars), 45)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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


小智 6

你可以用

par(las=2) # make label text perpendicular to axis
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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

theme(axis.text.x = element_text(angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)


ban*_*'14 5

在 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.
Run Code Online (Sandbox Code Playgroud)

在图形参数的文档(文档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.
Run Code Online (Sandbox Code Playgroud)

这就是为什么通过las=2是正确的答案。