我想获得这样的情节(https://www.dropbox.com/s/ed7f02jbjtxij6s/1.png),其中y标签应位于y轴的左侧,但我无法修改相应的x轴,因为x标签是日期类型(不是数字)比如1,2,3,我可以在这里设置像这样的文字(x = -1,y = ...),就像这样。)
在这里,我只想使用text
或某些others functions
来实现这样的功能,我可以在其中修改与y轴的所需距离,而不是axis(2, ...)
可以在https://www.dropbox.com/s/87bwvhyo6i4f68u/test.csv
下载csv文件我使用的代码。
tmp <- read.csv("~/desktop/test.csv", stringsAsFactors = FALSE,
strip.white = TRUE, header = FALSE)
tmp$V1 <- paste0(tmp$V1, '-01')
tmp <- within(tmp, {
V1 <- as.Date(V1)
date <- format(as.Date(tmp$V1, '%Y-%m-%d'), '%Y-%m')
stuff <- V2
})
par(tcl = -.1, xpd = FALSE)
with(tmp,
plot(V1, log(stuff), type = 'n', ylim = c(0,6),
col = 'royalblue1', lwd = 3, bty = 'l',
axes = FALSE, xlab = '', ylab = ''))
abline(h = 0:6, lwd = .5)
with(tmp, points(x = V1, y = log10(stuff), type = 'l',
col = 'royalblue1', lwd = 3))
par(xpd = TRUE)
axis(2, at = 0:6, cex.axis = .6,
labels = NA, las = 2)
x <- with(tmp, seq(min(V1), max(V1), length = 12))
text(x = x, y = -.5, cex = .8, labels = format(x, '%Y-%m'), srt = 45)
# for y axis: here x = x[1] - 1 does not work, since x is not numeric class
text(x = x[1] - 1, y = 0:6, cex = .8, labels = format(10 ** (0:6), scientific = FALSE, big.mark = ','))
感谢。
编辑:
来自hadj
函数中的MrFlick [line
和mtext
的答案:
axis(2, at = 0:6, hadj = 1.5,
labels = format(10 ** (0:6), scientific = FALSE, big.mark = ','),
las = 2)
mtext(side = 2, at = 0:6, , las = 2, line = 3,
text = format(10 ** (0:6), scientific = FALSE, big.mark = ','))'
答案 0 :(得分:1)
我不确定我理解你为什么不想使用标准轴标签。有一个hadj=
可以让您控制标签的绘制位置
axis(2, at = 0:6,hadj=.6,
labels =format(10 ** (0:6), scientific = FALSE, big.mark = ','), las = 2)
否则,您可以使用text()
代替mtext()
代替保证金文字
mtext(side=2, at=0:6, , las=2,
text = format(10 ** (0:6), scientific = FALSE, big.mark = ','))
或者,如果您真的想要,可以as.numeric(x)[1]
将日期转换为可以进行数字操作的日期。在这种情况下,-1
将调整一天,因为您的范围为range(as.numeric(x)) = c(12996,16191)