繁体   English   中英

所有x轴标签都不以45度显示

[英]all x axis labels are not displaying in 45 degree

在此处输入图片说明 我有如下代码。 但是我没有得到所有的x轴标签,当我尝试在pdf文件中显示时,它没有以45度显示。 由于我是新手,请帮助我更正此选项。

    pdf(file="figure.pdf", height=3.5, width=5, onefile=TRUE)
    Runtime <- c(579,0,581,610,830,828,592,651,596,596,591,581,587,594,604,606,447,434,445)
    Runtime
    g_range <- range(0,Runtime)
    g_range
    plot(Runtime, type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE)
    lab=c('2011-07-20','2011-08-03','2011-08-10','2011-08-17','2011-08-24','2011-08-25','2011-08-27','2011-08-31','2011-09-07','2011-09-10','2011-09-14','2011-09-21','2011-09-28','2011-10-05','2011-10-06','2011-10-07','2011-10-13','2011-10-19','2011-10-31')
    box()
    lab
    axis(1, at=1:19, lab=F)
    text(axTicks(1), par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8)
    axis(2, las=1, at=500*0:g_range[2])
    title(main="Runtime", col.main="red", font.main=4)
    title(xlab="Build", col.lab=rgb(0,0.5,0))
    title(ylab="MS", col.lab=rgb(0,0.5,0))
    legend(1, g_range[2], c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1);
    dev.off()

当我运行您的代码时, 没有得到您显示的图像。 问题是这一行:

text(axTicks(1), par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8)

作为axTicks(1)返回:

> axTicks(1)
[1]  5 10 15

因此,正在发生的情况是在这3个位置绘制了19个标签。

如果要在刻度线的位置( 1:19 )进行绘图,则:

text(1:19, par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8)

将工作。

这是一个基于您的代码的完整示例。

Runtime <- c(579,0,581,610,830,828,592,651,596,596,591,581,587,
             594,604,606,447,434,445)
g_range <- range(0,Runtime)
lab <- c('2011-07-20','2011-08-03','2011-08-10','2011-08-17','2011-08-24',
         '2011-08-25','2011-08-27','2011-08-31','2011-09-07','2011-09-10',
         '2011-09-14','2011-09-21','2011-09-28','2011-10-05','2011-10-06',
         '2011-10-07','2011-10-13','2011-10-19','2011-10-31')
## plot
op <- par(mar = c(6,4,4,2) + 0.1) ## bigger bottom margin
plot(Runtime, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE)
box()
axis(1, at=1:19, lab=FALSE)
text(1:19, par("usr")[3] - 40, srt=45, adj=1.2, labels=lab, xpd=T, cex=0.7)
axis(2, las=1, at=500*0:g_range[2])
title(main="Runtime", col.main="red", font.main=4)
title(xlab="Build", col.lab=rgb(0,0.5,0), line = 4.5)
title(ylab="MS", col.lab=rgb(0,0.5,0))
legend("topright", c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1)
## reset par
par(op)

不过,使用gridBase包中的函数可能会更好地处理此问题,该功能允许网格图形和基础图形混合在一起。 我说这可能更好的原因是,您可以指定以行数为单位设置y坐标,而不是尝试根据绘制的数据得出适合y值。

这是一个例子:

## load gridBase
require(gridBase)

## do the base plot parts
op <- par(mar = c(6,4,4,2) + 0.1) ## bigger bottom margin
plot(1:19, Runtime, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE)
box()
axis(1, at=1:19, lab=FALSE)
axis(2, las=1, at=500*0:g_range[2])
title(main="Runtime", col.main="red", font.main=4)
title(xlab="Build", col.lab=rgb(0,0.5,0), line = 4.5)
title(ylab="MS", col.lab=rgb(0,0.5,0))
legend("topright", c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1)
## at this point, DO NOT alter the dimensions of the plotting window

## now do the grid business
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
## this adds the text
grid.text(lab, x = unit(1:19, "native"), y = unit(-1, "lines"),
          just = "right", rot = 60, gp = gpar(cex = 0.7))
## this finishes off the viewport - you have to do this or things will go wrong:
popViewport(3)
## reset par
par(op)

请注意,这在屏幕上可能有点挑剔,在我的R 2.13.2安装中重新运行gridBase示例不会产生任何标签。 不过,关闭设备然后重新运行代码是可行的。 如果您要使用pdf()设备,我认为这应该不是问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM