如何在R中的不同地图中使用相同的色标? [英] How to use the same color scale for points in different maps in R?

查看:400
本文介绍了如何在R中的不同地图中使用相同的色标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有SpatialPointsDataFrame类的对象,如下所示:

I have an object of the class SpatialPointsDataFrame which looks like:

            coordinates div score
1   (-86.2905, 32.7131)   1  0.73
2    (-113.17, 34.4462)   2  3.00
3   (-112.769, 35.1567)   4  0.94
4   (-113.193, 33.9167)   5  4.09
5    (-113.265, 34.407)   1  1.50
6   (-113.585, 34.8241)   2  5.98
7   (-113.793, 34.7597)   3  2.55
8   (-113.651, 35.1733)   2  3.21
9   (-113.675, 35.3431)   4  2.83
10   (-113.09, 34.4036)   5  6.07
11  (-114.172, 34.6878)   1  4.56
12  (-120.153, 37.3049)   3  7.00

我想要的是为每个div类别(只有5),点颜色随分数列变化(从0到7不等)。

and what I want is to produce one map for each "div" category (just 5) with the points colors varying with the score column (that varies from 0 to 7).

我的主要问题是:
如何在不同的地图中使用相同的颜色尺度,以便我们可以比较它们?

My main problem is: How to use the same color scale in the different maps so I can compare them?

我认为 spplot 函数m我完全按照我想要的,但我不明白这个功能的论据如何工作。如果这是功能,那么我有另一个问题,如何绘制大陆背后的边界? (我正在使用已经在R中的 wrld_simpl 数据。

I figure that the spplot function may do exactly what I want, but I couldn't understand how the arguments of this function work. If this is the function then I have another question, how to plot the borders of the continents behind the points? (I am using the wrld_simpl data that is already in R)

推荐答案

以下是ggplot的示例:

Here is an example with ggplot:

library(ggplot2)
library(maps)
df$div <- paste("DIV#", df$div)
ggplot(data=df, aes(x=lon, y=lat)) + 
  geom_polygon(
    data=map_data("state"), 
    aes(x=long, y=lat, group = group),
    colour="white", fill="grey10"
  ) + 
  geom_point(aes(color=score), size=3) +
  facet_wrap(~ div) +
  coord_cartesian(xlim=c(-125, -85), ylim=c(30, 42)) +
  scale_colour_gradient2(
    low="red", mid="yellow", high="green", midpoint=mean(df$score)
  )

我刚刚把颜色标尺放在一起,但是它们之间是一致的。注意我创建了我自己的 data.frame 与单独的lat lon列。

I just threw the color scale together quickly, but it is consistent between plots. Note I created my own data.frame with separate lat lon columns.

这篇关于如何在R中的不同地图中使用相同的色标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆