【问题标题】:Visualize correlations on a map in R在 R 中的地图上可视化相关性
【发布时间】:2014-03-13 12:48:27
【问题描述】:

我计算了温度与葡萄收获日期之间的相关性。我将结果存储为矩阵:

       32.5         37.5        42.5       47.5          52.5        57.5        62.5
-12.5 -0.05783118 -0.001655467 -0.07857098 -0.1526494 -0.0007327898 -0.02078552  0.06121682
-7.5  -0.23219824 -0.059952117 -0.06895444 -0.1674386 -0.1311612338 -0.08476390  0.09831010
-2.5  -0.11040995 -0.147325160 -0.15016740 -0.1796807 -0.1819844495 -0.14472899 -0.03550576
2.5   -0.20577359 -0.180857373 -0.15077067 -0.2293366 -0.2577666092 -0.21645676 -0.13044584
7.5   -0.44526971 -0.176224708 -0.15114994 -0.2459971 -0.2741514139 -0.19281484 -0.15683870
12.5  -0.12481683 -0.121675085 -0.16011098 -0.2288839 -0.2503969467 -0.26616721 -0.23089796
17.5  -0.15352693 -0.220012419 -0.11456690 -0.2314059 -0.2194705426 -0.20557053 -0.22529422

现在我想在地图上可视化结果。它应该是这样的:

example for visualizing correlations on a map

只有我的经纬度不同。我的纬度范围从 32,5°N 到 62,5°N,我的经度从 -12,5°E 到 17,5°E。 我完全不知道它是怎么做到的!如果有人可以帮助我,那就太好了。 问候。

【问题讨论】:

标签: r


【解决方案1】:

这是一种方式。您的网格相当粗糙(增量为 5°,或赤道约 350 英里),当然您没有提供实际地图,但这将在您提供的坐标处绘制相关性“热图”。

df <- cbind(lon=rownames(df),df)
library(reshape2)
library(ggplot2)
library(RColorBrewer)
gg <- melt(df,id="lon",variable.name="lat",value.name="corr")
gg$lat <- as.numeric(substring(gg$lat,2))  # remove pre-pended "X"
gg$lon <- as.numeric(as.character(gg$lon)) # convert factor to numeric
ggplot(gg)+
  geom_tile(aes(x=lon,y=lat, fill=corr))+
  scale_fill_gradientn(colours=rev(brewer.pal(9,"Spectral")))+
  coord_fixed()

【讨论】:

    【解决方案2】:

    corrplot R 包 中的corrplot() 函数可用于绘制相关图。

    library(corrplot)  
    M<-cor(mtcars) # compute correlation matrix
    corrplot(M, method="circle")
    

    这里有描述:

    在线 R 软件也可用于计算和可视化相关矩阵,只需单击即可,无需任何安装:

    http://www.sthda.com/english/rsthda/correlation-matrix.php

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 1970-01-01
      • 2019-10-19
      • 2018-09-02
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多