如何在R 2x2条形图中以X轴45度显示所有标签 [英] How to show all the labels in X-axis 45 degree in R 2x2 bar plot
本文介绍了如何在R 2x2条形图中以X轴45度显示所有标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有以下数据:
Method Metric E0 E1 E2 E4
Method-XXX Precision 0.9661017 0.9622642 1 0.9655172
Method-YYY Precision 0.533 0.535 0.378 0.214
Method-ZZZ Precision 0.595 0.843 0.77 0.689
Method-XZZZ Precision 0.573 0.698 0.53 0.708
Method-XZZZY Precision 0.008 0.011 0.004 0.002
Method-XXX Recall 0.9736842 0.9736842 0.9473684 0.9473684
Method-YYY Recall 1 1 1 0.667
Method-ZZZ Recall 0.833 1 1 1
Method-XZZZ Recall 1 1 1 1
Method-XZZZY Recall 0.167 0.75 1 1
我可以创建该图:
但是,正如您所看到的,x轴并没有全部分配标签. 我该如何实现? 如果将x轴旋转45度也可以.但是然后我不确定该怎么做:
However as you can see, the x-axis are not all assigned with labels. How can I achieve that? It's also ok if we rotate the x-axis 45 degree. But then I'm not sure how to do that:
这是我的代码(由 thelatemail 提供):
This is my code (Courtesy of thelatemail):
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
barcols <- c("red","blue")
sapply(3:6,
function(x) {
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols)
title(main=names(dat[x]))
axis(1,at=colMeans(bp),c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY"),lwd=0,lwd.tick=1)
abline(h=0)
}
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.6,c("Precision","Recall"),fill=barcols,cex=1.5)
更新
我尝试了以下方法来生成45度.但也没有用:
I tried the following to generate the 45 degree. But didn't work either:
sapply(3:6,
function(x) {
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),xaxt="n",beside=TRUE,col=barcols)
title(main=names(dat[x]))
xaxislab <- c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY")
text(cex=1, x=colMeans(bp)-.25, y=-1.25, xaxislab, xpd=TRUE, srt=45)
#axis(1,at=colMeans(bp),xaxislab,lwd=0,lwd.tick=1)
#abline(h=0)
}
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.1,c("Precision","Recall"),fill=barcols,cex=1.5)
推荐答案
基本上遵循 gridBase 小插图(pdf)),您可以使用grid.text()
注释 base 图形输出.
Following basically the same strategy used in this answer (and demo'd in the first example in the gridBase vignette (pdf)) you could use grid.text()
to annotate the base graphics output.
library(gridBase)
## Function that plots barplots with x-axes annotated with slanted
ff <- function(x) {
barcols <- c("red","blue")
## Plot, suppressing the labels
bp <- barplot(matrix(dat[,x], nrow = 2, byrow = TRUE), xaxt = "n",
beside = TRUE, col = barcols)
title(main=names(dat[x]))
xaxislab <- c("Method-XXX", "Method-YYY", " Method-ZZZ",
"Method-XZZZ", " Method-XZZZY")
## Compute x-axis coordinate at center of each group
bp <- colMeans(bp)
## Use gridBase to compute viewport coordinates and
## grid to push/pop viewports and add the labels
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.text(xaxislab,
x = unit(bp, "native"), y = unit(-0.5, "lines"),
just = "right", rot = 45, gp=gpar(cex=0.7))
popViewport(3)
}
## Apply it to your data
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
sapply(3:6, ff)
这篇关于如何在R 2x2条形图中以X轴45度显示所有标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文