【问题标题】:Changing ylabels on matrix plot更改矩阵图上的标签
【发布时间】:2020-01-20 12:18:33
【问题描述】:

我想绘制随时间变化的风速和高度。我已经能够使用矩阵图来做到这一点,但是现在我正试图使我的 yaxis 标签更加标准化(0,100,200,300 米......等),但不知道目前 yaxis (0:200) 线的索引如何与实际高度一致。

欢迎任何更改此代码或其他绘图类型的想法。

数据可以在这里下载:https://docs.google.com/spreadsheets/d/e/2PACX-1vSdYIBHyvv0ue1YMqXVUqVSeUyYbZH4fl-XhXe_tVa42iJsTGejeGX1re06-jL-kYiuIJCUohdvEL7k/pub?output=csv

编辑:数据文件说明:

数据的维度是 715 列和 200 行,不包括标题。标题显示时间,而高度向量(长度 200)对应于行。

当前输出:

检查了代码以确保其正常工作。为转发道歉:

代码:

wind_speeds <- data

altitudes <- c(1,29,58,87,116,145,174,203,232,261,290,319,348,377,406,435,464,493,522,551,580,609,638,667,696,725,
               754,783,812,841,870,899,928,957,986,1015,1044,1073,1102,1131,1160,1189,1218,1247,1276,1305,1333,1362,
               1391,1420,1449,1478,1507,1536,1565,1594,1623,1652,1681,1710,1739,1768,1797,1826,1855,1884,1913,1942,
               1971,2000,2029,2058,2087,2116,2145,2174,2203,2232,2261,2290,2319,2348,2377,2406,2435,2464,2493,2522,
               2551,2580,2609,2637,2666,2695,2724,2753,2782,2811,2840,2869,2898,2927,2956,2985,3014,3043,3072,3101,
               3130,3159,3188,3217,3246,3275,3304,3333,3362,3391,3420,3449,3478,3507,3536,3565,3594,3623,3652,3681,
               3710,3739,3768,3797,3826,3855,3884,3913,3941,3970,3999,4028,4057,4086,4115,4144,4173,4202,4231,4260,
               4289,4318,4347,4376,4405,4434,4463,4492,4521,4550,4579,4608,4637,4666,4695,4724,4753,4782,4811,4840,
               4869,4898,4927,4956,4985,5014,5043,5072,5101,5130,5159,5188,5217,5245,5274,5303,5332,5361,5390,5419,
               5448,5477,5506,5535,5564,5593,5622,5651,5680,5709,5738,5767)


unique_hrs <- c("01", "02", "03", "04", "05", "06" ,"07", "08", "09" ,"10", "11", "12", "13", "14" ,"15" ,"16", "17" ,"18", "19", "20" ,"21" ,"22", "23")

unique_hrs_index <- c(16, 46, 76, 106, 135, 165, 195, 225, 255, 285, 314, 344, 374, 404, 433, 463, 493, 523, 553, 583, 612, 642, 672)

min <- 0
max <- 12


library(viridis)
ColorRamp <- viridis(n = (max*2))
ColorLevels <- seq(min, max, length=length(ColorRamp))

# Set layout.  We are going to include a colorbar next to plot.
layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1),
       heights=c(1,1))

#plotting margins.  These seem to work well for me.
par(mar = c(4,5,4,2), font = 2)

# Plot it up!
image(1:ncol(wind_speeds), 1:nrow(wind_speeds), t(wind_speeds),
      col=ColorRamp, xlab="Time (UTC)", ylab="Altitude (m)",
      axes=FALSE, zlim=c(min,max), ylim = c(0,55), 
      main= NA)

#annotate the plot
box()
axis(side = 1, at=unique_hrs_index, labels=unique_hrs,
     cex.axis=1.0)
axis(side = 2, at=seq(1,length(altitudes),1), labels=round(altitudes), las= 1,
     cex.axis=1)

# Add colorbar to second plot region
par(mar = c(3,4,4,2))
image(1, ColorLevels,
      matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
      col=ColorRamp,xlab="",ylab="Wind Speed m/s",xaxt="n", las = 1)

【问题讨论】:

  • 你能解释一下数据wind_speed的维度是多少吗?您的 Google 文档包含的数据多于 altitude。您能否提供一个 wind_speed 数据集的小示例,以便我们了解您是如何绘制它的?
  • 您的axis(2, ...) 命令正在绘制实际的高度值。试试axis(side=2, at=seq(0,1600, by=200), labels=seq(0, 1600, by=200), las-1, cex.axis=1) 之类的东西。这假设 y 轴刻度是高度。如果只是连续值,则需要插值。
  • @dc37 希望我的编辑澄清。
  • @dcarlson 我试过了,现在没有显示任何 ylabels。
  • par("usr") 显示 0.5、715.5、0.0、55.0,因此用户坐标在水平 (x) 轴上跨度为 715,但在垂直 (y) 轴上仅跨度 55。 200 个高度值是否被截断?原始图上的值仅达到 1565,而不是 5767 的最大高度。

标签: r


【解决方案1】:

您在 y 轴上绘制的高度值只是值范围的一小部分(1565 与 5767)。这个提议的解决方案假设绘图是正确的,并且您只需要该范围内的合理值。将您的 axis(side=2, ... ) 命令替换为以下内容:

y = seq(1,length(altitudes),1)    
alt.lm <- lm(y~altitudes)
ylbl <- seq(0, 1500, by=250)
yval <- predict(alt.lm, data.frame(altitudes=ylbl))
axis(side = 2, yval, ylbl, las=1, cex.axis=1)

【讨论】:

  • 谢谢,我收到一条错误消息,上面写着Error in eval(predvars, data, env) : object y not found y 是什么?只是从 1:200 开始的一个序列?
  • 编辑:是的,设置 y = seq(1,length(altitudes),1) 有效。谢谢!
猜你喜欢
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多