所有x轴标签均未以45度显示

Tam*_*lan 5 r

在此输入图像描述我的代码如下所示.但是我没有得到所有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()
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 6

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

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

作为axTicks(1)回报:

> axTicks(1)
[1]  5 10 15
Run Code Online (Sandbox Code Playgroud)

所以正在发生的事情是你的19个标签正在这3个地点被绘制.

如果你想在ticks(1:19)的位置绘图,那么:

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

将工作.

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

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

这可能会更好地使用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)
Run Code Online (Sandbox Code Playgroud)

注意这可能在屏幕上有点挑剔,重新运行我的R 2.13.2安装上的gridBase示例不会产生任何标签.关闭设备,然后重新运行代码工作.如果您正在绘制pdf()设备,我不认为这应该是一个问题.