在分组条形图 R 上将 X 轴标签旋转 45 度 [英] rotate X axis labels 45 degrees on grouped bar plot R

查看:36
本文介绍了在分组条形图 R 上将 X 轴标签旋转 45 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 R 中的分组条形图上将 X 轴标签旋转 45 度?

我已尝试(gridBase)也是对我来说不成功,由于某种原因,我收到以下错误:

<块引用>

无法弹出顶层视口(网格和图形输出混合?)"

PS.大多数人似乎推荐 这个解决方案 在 R 基础中,但我也坚持这一点,因为我不明白他们指的是什么数据(我需要某种示例数据集来理解新的命令行......).

这些解决方案是否不起作用,因为我的条形图是分组条形图?还是应该仍然有效?欢迎任何建议,我已经卡了很长时间了.谢谢.

根据要求,我添加了用于生成上述图片的代码(基于 text() 解决方案之一):

data <- #这是一个4列20行的矩阵;#colnames 和 rownames 已指定.#barplot数据按行分组lablist <- as.vector(colnames(data))条形图(数据,旁边=真,col=c(深红色",红色",灰色20",灰色40"))文本(1:100,par(usr")[1],标签=lablist,srt=45,pos=1,xpd=TRUE)

解决方案

我不是基础剧情精通,所以可能我的解决方案不是很简单.我认为在这里使用 ggplot2 更好.

def.par <- par(no.readonly = TRUE)## 将设备分为两行一列## 为 barplot 分配图 1## 为条形图标签分配图 2##尊重宽度和高度之间的关系nf <- 布局(矩阵(c(1,1,2,2),2,2,byrow = TRUE), c(1,3), c(3,1), TRUE)布局.show(nf)## 条形图par(mar = c(0,1,1,1))set.seed(1)nKol <- 8 ##你可以在这里改变但超过11列## 解决方案不是真的可读数据 <- 矩阵(样本(1:4,nKol*4,rep=TRUE),ncol=nKol)xx <- 条形图(数据,旁边=真,col=c("darkred","re​​d","grey20","grey40"))## 标签,为 sacles 创建虚拟图par(mar = c(1,1,0,1))情节(seq_len(长度(xx)),代表(1,长度(xx)),类型='n',轴=假)## 创建一些文本标签标签 <- paste("标签", seq_len(ncol(xx)), sep = " ")## 在当前图形的顶部绘制带有一些旋转的文本标签文本(seq_len(长度(xx)),代表(1.4,长度(xx)),srt = 90,adj = 1,标签 = 标签,xpd = TRUE,cex=0.8,srt=60,col=c("darkred","re​​d","grey20","grey40"))par(def.par) #- 重置为默认值

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

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):

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

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

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.

[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)

解决方案

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

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

这篇关于在分组条形图 R 上将 X 轴标签旋转 45 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆