【问题标题】:Rescale contour plot and mapdata重新缩放等高线图和地图数据
【发布时间】:2012-08-02 04:58:09
【问题描述】:

我正在尝试将图像叠加到世界地图上。这是可能的代码

   require(reshape)
   require(mapdata)
   df <- read.table('year.dat',head=F)
   names(df) <- c("value", "x", "y", "t")
   dfc <- cast(df[ ,-4], x ~ y)
   filled.contour(as.matrix(dfc),color.palette = colorRampPalette(c("lightblue",       "blue","violet", "black")),
   xlab = "Longitude (°)", ylab = "Latitude (°)",
   xlim = c(0, 360), ylim = c(-90, 90), 
   nlevels = 25,
   plot.axes = {axis(1); axis(2);           
   map('world2Hires', 
   xlim = c(0, 360), 
   ylim = c(-90, 90), 
   add = T, col = "darkgrey")}
   )

文件“year.dat”中包含的数据格式为

 0.35217720               1         201           1
 0.26413283               1         209           1
 1.1665874                1         210           1
 ...
 0.30815500               2         194           1
 0.15407741               2         196           1
 0.15407741               2         197           1
 0.33016610               2         205           1
 ...

其中第一列是标量值,第二列是经度索引,第三列是纬度索引,第四列是时间索引。即行

 0.35217720               1         1           1

表示在时间 1,在坐标 -90° N,0° E 中,我们有值 0.35217720。整个格子包含 480x241 个点。 问题如下,如果我尝试在 R 中执行前面的代码,世界地图和条形图例会被淹没但没有颜色叠加。如果我评论该行

#xlim = c(0, 360), ylim = c(-90, 90),

然后绘制颜色良好的图像,但不存在世界地图。所以我在 xlim 和 ylim 中尝试了不同的限制,实际上问题在于输入矩阵的比例。即R将两个轴的输入范围从0到1,而实际上它们从0到360和0到180。我可以通过哪种方式告诉R,x轴的输入比例从0到360,从0到180 y轴?

【问题讨论】:

    标签: r


    【解决方案1】:

    解决办法是:

    require(reshape) 
    require(mapdata)
    require(mapproj)
    df <- read.table('year.dat',head=F)
    names(df) <- c("value", "x", "y", "t")
    dfc <- cast(df[ ,-4], x ~ y)
    mm<-as.matrix(dfc,ncol=480,nrow=241)
    #pdf('mappamondo.pdf')
     filled.contour(x=seq(0,360,length.out=480),y=seq(-90,90,length.out=241),mm,color.palette = colorRampPalette(c("lightblue", "blue","violet", "black")),
    xlab = "Longitude (°)", ylab = "Latitude (°)",
    plot.axes = {axis(1); axis(2);          
    map('world2Hires',
    xlim = c(0, 360), 
    ylim = c(-90, 90), 
    add = T, col = "black")}
    )
    
    #dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多