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

标签 r

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

你可以看到顶轴的刻度线只是模糊在一起,选择的数字对我来说意义不大。我怎样才能告诉 R 我真正想要什么?

以下是我正在使用的数据集:cobs10
cobs11 .

这是我的代码:

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)

感谢您考虑。

最佳答案

您几乎已经有了解决方案:

axis(side=3, col="black", at=cobs10$day, labels=cobs10$gdd) 

除了,您要求在每个条目上都有刻度和标签。
看看函数pretty :
at <- pretty(cobs10$day)
at
# [1]   0 100 200 300 400

这些是刻度线应放置在 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)

更新

我对你在一个情节中使用三个不同的系列有点恼火。这很麻烦的原因有很多。
  • 有两个 y 值总是很麻烦,请参阅 Stephen Few 中的这篇文章(我最喜欢的例子转到第 5 页);在您的情况下,由于绘图的性质以及您使用颜色来指示值属于哪个 y 轴,所以它并不那么严重。但是,原则上。
  • 轴刻度应该有一个固定的功能,例如线性或对数。使用您的热量单位,它们会“随机”出现(我知道情况并非如此,但对于局外人来说,它们确实如此)。
  • 我们必须对仅指“一年中的一天”的 x 轴刻度做一些事情。

  • 首先,我们查看您的数据,看看可以天真地做些什么。我们认识到您的“日期”变量是实际日期。让我们利用它并让 R 意识到它!
    cobs10 <- read.table('cobs10.txt',as.is=TRUE)
    cobs10$date <- as.Date(cobs10$date)
    plot(temp ~ date, data=cobs10, type='l')
    

    R's builtin plotting of a formula

    在这里,我真的很喜欢 x 轴刻度,并且在复制它时遇到了一些麻烦。日期上的“漂亮”坚持 4 个刻度或 12 个刻度。但是我们稍后会回到这个问题。

    接下来,我们可以对叠加绘图做一些事情。在这里,我使用 ''par(mfrow=c(3,1))'' 来指示 R 将三个多个图堆叠在一个窗口中;通过这些多个图,我们可以区分内边距和外边距。 ''mar'' 和 ''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')
    

    enter image description here

    这看起来不错,但在图的顶部没有刻度。不好。自然,我们可以在前两个图中启用刻度(使用 ''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)
    

    enter image description here

    由于 ''pretty'' 的行为不像我们想要的那样,我们使用 ''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.
    

    现在热刻度已经到位,让我们尝试一下。
    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)
    

    enter image description here

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

    关于r - 更改R中图形上的刻度线数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16778094/

    相关文章:

    R正则表达式: how to extract elements that contains two character in a certain order?

    r - 在 R 中不连续的范围内选择一个随机数

    r - 获取新值开始的行索引

    r - ggplot构面网格y轴某些值不可见如何调整网格

    R markdown 输出页面宽度

    r - 当超过 6 个因子水平时循环通过点形状

    r - 如何根据新列复制每一行?

    r - 如何使用 dplyr 使用非标准评估来评估构造的字符串?

    css - 如何在 RMarkdown 中使用自定义 css 更改内联代码外观

    r - 如何获得阶乘(100)的确切值