旋转X轴标签45度在分组的柱状图R。

时间:2023-02-05 15:02:49

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

在R中,如何将X轴标签旋转45度?

I have tried the solution suggested here but got something very messy, the labels seem to have been added multiple times (only showing the axis part to protect data privacy): 旋转X轴标签45度在分组的柱状图R。

我已经尝试了这里建议的解决方案,但是有一些非常混乱的东西,标签似乎已经被添加了很多次(仅显示了保护数据隐私的axis部分):

This solution (gridBase) was also unsuccessful for me, for some reason I get the following error:

这个解决方案(gridBase)也不成功,因为某些原因,我得到了以下错误:

"Cannot pop the top-level viewport (grid and graphics output mixed?)"

“不能弹出*视图(网格和图形输出混合?)”

PS. Most people seem to recommend this solution in R base but I am stuck with that too because I don't understand what data they are referring to (I need some kind of example data set to understand new command lines...).

大多数人似乎在R基础上推荐这个解决方案,但是我也坚持这样做,因为我不理解他们指的是什么数据(我需要一些数据集来理解新的命令行…)。

Are these solutions not working because my barplot is a grouped barplot? Or should it work nevertheless? Any suggestions are welcome, I have been stuck for quite some time. Thank you.

这些解决方案不奏效吗?因为我的barplot是一个分组的barplot?还是应该如此?任何建议都是受欢迎的,我已经被困了很长一段时间了。谢谢你!

[edit] On request I am adding the code that I used to generate the picture above (based on one of the text() solutions):

在请求时,我添加了我用来生成上面图片的代码(基于一个文本()解决方案):

data <- #this is a matrix with 4 columns and 20 rows;
        #colnames and rownames are specified.
        #the barplot data is grouped by rows

lablist <- as.vector(colnames(data))

barplot(data, beside=TRUE, col=c("darkred","red","grey20","grey40"))
text(1:100, par("usr")[1], labels=lablist, srt=45, pos=1, xpd=TRUE)

3 个解决方案

#1


6  

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

我不是一个熟练的基础情节,所以也许我的解决方法不是很简单。我认为在这里使用ggplot2更好。

旋转X轴标签45度在分组的柱状图R。

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

#2


5  

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:

但是把cex=1换成cex=。8或.6在文本()函数中:

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.

在你发布的图片中,我觉得标签太大了。cex设置这些标签的大小。

#3


3  

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:

我也有同样的问题。我假设您只希望每个组下面有一个标签。我可能是错的,因为您没有明确地声明它,但是这似乎是这样的,因为您的标签在图像中是重复的。在这种情况下,你可以使用Stu提出的解决方案,尽管你必须将colMeans应用到x变量上,当你将它提供给文本函数时:

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)

#1


6  

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

我不是一个熟练的基础情节,所以也许我的解决方法不是很简单。我认为在这里使用ggplot2更好。

旋转X轴标签45度在分组的柱状图R。

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

#2


5  

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:

但是把cex=1换成cex=。8或.6在文本()函数中:

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.

在你发布的图片中,我觉得标签太大了。cex设置这些标签的大小。

#3


3  

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:

我也有同样的问题。我假设您只希望每个组下面有一个标签。我可能是错的,因为您没有明确地声明它,但是这似乎是这样的,因为您的标签在图像中是重复的。在这种情况下,你可以使用Stu提出的解决方案,尽管你必须将colMeans应用到x变量上,当你将它提供给文本函数时:

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)