更改R中图形上的刻度线数量

Naz*_*zer 3 r

我创建了一个两个图(两年)气候数据(温度和降水)的图形,看起来与我想要的完全一样,除了我的一个轴有太多刻度线.对于我在这个图上所做的一切,我找不到一种方法来指定更少的刻度线,而不会弄乱其他部分.我还想指定刻度线的位置.这是图:气候

您可以看到顶轴的刻度线只是模糊在一起,所选的数字对我来说不是很有意义.我怎么能告诉R我真正想要的是什么?

以下是我使用的数据集: cobs10cobs11.

这是我的代码:

par(mfrow=c(2,1))
par(mar = c(5,4,4,4) + 0.3)
plot(cobs10$day, cobs10$temp, type="l", col="red", yaxt="n", xlab="", ylab="",     
ylim=c(-25, 30))

axis(side=3, col="black", at=cobs10$day, labels=cobs10$gdd) 
at = axTicks(3)
mtext("Thermal Units", side=3, las=0, line = 3)

axis(side=2, col='red', labels=FALSE)
at= axTicks(2)
mtext(side=2, text= at, at = at, col = "red", line = 1, las=0)
mtext("Temperature (C)", side=2, las=0, line=3)

par(new=TRUE)
plot(cobs10$gdd, cobs10$precip, type="h", col="blue", yaxt="n", xaxt="n", ylab="",     
xlab="")
axis(side=4, col='blue', labels=FALSE)
at = axTicks(4)
mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0)
mtext("Precipitation (cm)", side=4, las=0, line = 3)


par(mar = c(5,4,4,4) + 0.3)
plot(cobs11$day, cobs11$temp, type="l", col="red", yaxt="n", xlab="Day of Year",     
ylab="", ylim=c(-25, 30))

axis(side=3, col="black", at=cobs11$day, labels=cobs11$gdd) 
at = axTicks(3)
mtext("", side=3, las=0, line = 3)

axis(side=2, col='red', labels=FALSE)
at= axTicks(2)
mtext(side=2, text= at, at = at, col = "red", line = 1, las=0)
mtext("Temperature (C)", side=2, las=0, line=3)

par(new=TRUE)
plot(cobs11$gdd, cobs11$precip, type="h", col="blue", yaxt="n", xaxt="n", ylab="",     
xlab="", ylim=c(0,12))
axis(side=4, col='blue', labels=FALSE)
at = axTicks(4)
mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0)
mtext("Precipitation (cm)", side=4, las=0, line = 3)
Run Code Online (Sandbox Code Playgroud)

谢谢你的思考.

MrG*_*ble 7

你已经得到了解决方案:

axis(side=3, col="black", at=cobs10$day, labels=cobs10$gdd) 
Run Code Online (Sandbox Code Playgroud)

除此之外,您要求在每个条目上都有刻度和标签.看看这个功能pretty:

at <- pretty(cobs10$day)
at
# [1]   0 100 200 300 400
Run Code Online (Sandbox Code Playgroud)

这些是刻度线应放在x轴上的位置.现在您需要找到相应的标签.这不是直截了当的,但我们会得到:

lbl <- which(cobs10$day %in% at)
lbl
# [1] 100 200 300
lbl <- c(0, cobs10$gdd[lbl]

axis(side=3, at=at[-5], labels=lbl)
Run Code Online (Sandbox Code Playgroud)

更新

我对你在一个剧情中使用三个不同系列感到有些恼火.造成这种麻烦的原因有很多.

  1. 有两个y值总是很麻烦,请参阅Stephen Few的这篇文章(转到第5页,我最喜欢的例子); 在你的情况下,由于图的性质和你使用颜色来指示值属于哪个y轴,因此它并不严重.但原则上仍然如此.
  2. 轴刻度应具有固定的功能,例如线性或对数.使用您的热量单位,它们看起来是"随机的"(我知道情况并非如此,但对于他们这样做的局外人).
  3. 我们必须对你的x轴刻度做一些事情,只是提到"一年中的一天".

首先,我们来看看您的数据,看看可以天真地做些什么.我们认识到您的"日期"变量是实际日期.让我们利用它,让R意识到它!

cobs10 <- read.table('cobs10.txt',as.is=TRUE)
cobs10$date <- as.Date(cobs10$date)
plot(temp ~ date, data=cobs10, type='l')
Run Code Online (Sandbox Code Playgroud)

R是内置的公式绘图

在这里,我非常喜欢X轴刻度并且在复制时遇到了一些麻烦.''漂亮''在日期坚持4蜱或12蜱.但我们稍后会再回过头来.

接下来,我们可以对叠加绘图做一些事情.在这里,我使用''par(mfrow = c(3,1))''来指示R在一个窗口中堆叠三个多个图; 通过这些多重图,我们可以区分边距和外边距.的""擦伤""和"" OMA""参数指的是的余量.

让我们把所有三个变量放在一起!

par(mfrow=c(3,1), mar=c(0.6, 5.1, 0, 0.6), oma=c(5.1, 0, 1, 0))
plot(temp ~ date, data=cobs10, type='l', ylab='Temperatur (C)')
plot(precip ~ date, data=cobs10, type='l', ylab='Precipitation (cm)')
plot(gdd ~ date, data=cobs10, type='l', ylab='Thermal units')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这看起来没问题,但是在图表顶部没有刻度.不好.当然,我们可以在前两个图中启用刻度(使用''plot(...,xaxt ='n')''),但这会扭曲底部图.因此,您需要对所有三个图表执行此操作,然后将轴添加到外部绘图区域.

par(mfrow=c(3,1), mar=c(0.6, 5.1, 0, 0.6), oma=c(5.1, 0, 1, 0))
plot(temp ~ date, data=cobs10, type='l', xaxt='n', ylab='Temperatur (C)')
plot(precip ~ date, data=cobs10, type='l', xaxt='n', ylab='Precipitation (cm)')
plot(gdd ~ date, data=cobs10, type='l', xaxt='n', ylab='Thermal units')

ticks <- seq(from=min(cobs10$date), by='2 months', length=7)
lbl <- strftime(ticks, '%b')
axis(side=1, outer=TRUE, at=ticks, labels=lbl)
mtext('2010', side=1, outer=TRUE, line=3, cex=0.67)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

由于''漂亮''不像我们想要的那样表现,我们使用''seq''来制作x轴刻度序列.然后我们将日期格式化为仅显示月份名称的缩写,但这是关于本地设置(我住在丹麦),请参阅''locale''.要将轴刻度和标签添加到外部区域,我们必须记住指定''outer = TRUE''; 否则它被添加到最后一个子图.另请注意,我指定''cex = 0.67''以匹配x轴的字体大小和y轴.

现在我同意在单个子图中显示热单位并不是最佳的,尽管它是显示它的正确方法.但是蜱虫存在问题.我们真正想要的是显示一些很好的值,清楚地表明它们不是线性的.但是你的数据不一定包含这些好的值,所以我们必须自己插入它们.为此,我使用''splinefun''

lbl <- c(0, 2, 200, 1000, 2000, 3000, 4000)
thermals <-  splinefun(cobs10$gdd, cobs10$date)  # thermals is a function that returns the date (as an integer) for a requested value
thermals(lbl)
## [1] 14649.00 14686.79 14709.55 14761.28 14806.04 14847.68 14908.45
ticks <- as.Date(thermals(lbl), origin='1970-01-01') # remember to specify an origin when converting an integer to a Date.
Run Code Online (Sandbox Code Playgroud)

现在热刻度已到位,让我们尝试一下.

par(mfrow=c(2,1), mar=c(0.6, 5.1, 0, 0.6), oma=c(5.1, 0, 4, 0))
plot(temp ~ date, data=cobs10, type='l', xaxt='n', ylab='Temperatur (C)')
plot(precip ~ date, data=cobs10, type='l', xaxt='n', ylab='Precipitation (cm)')

usr <- par('usr')
x.pos <- (usr[2]+usr[1])/2

ticks <- seq(from=min(cobs10$date), by='2 months', length=7)
lbl <- strftime(ticks, '%b')
axis(side=1, outer=TRUE, at=ticks, labels=lbl)
mtext('2010', side=1, at=x.pos, line=3)

lbl <- c(0, 2, 200, 1000, 2000, 3000, 4000)
thermals <-  splinefun(cobs10$gdd, cobs10$date)  # thermals is a function that returns the date (as an integer) for a requested value
ticks <- as.Date(thermals(lbl), origin='1970-01-01') # remember to specify an origin when converting an integer to a Date.
axis(side=3, outer=TRUE, at=ticks, labels=lbl)
mtext('Thermal units', side=3, line=15, at=x.pos)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新我更改了mtext最后一个代码块中的函数调用,以确保x轴文本以绘图区域为中心,而不是整个区域.您可能希望通过更改line-argument 来调整垂直位置.