How to use the same color scale for points on different maps in R?

I have an object of the SpatialPointsDataFrame class that looks like this:

            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

and I want to create one map for each category "div"(5 in total) with point colors different from the column score(which varies from 0 to 7).

My main problem: How to use the same color scale on different cards so that I can compare them?

I believe that a function spplotcan do exactly what I want, but I could not understand how the arguments of this function work. If this is a function, I have another question: how to build the borders of continents beyond points? (I use data wrld_simplthat is already in R)

+3
source share
2

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)
  )

enter image description here

, . . data.frame .

+3

spplot() cuts= col.regions=, :

library(sp)
library(gridExtra)          ## For grid.arrange()

## Example data
data(meuse)
coordinates(meuse) <- ~x+y
range(meuse$copper)         ## Examine range of values to be plotted
# [1]  14 128

## Establish intervals and colors to be applied to them
cuts <- 20*(0:7)
cols <- blues9[3:9]

## Show that it works by creating two plots with different data ranges
## but same color scale    
a <- spplot(subset(meuse, copper>40), "copper", cuts=cuts, col.regions=cols)
b <- spplot(subset(meuse, copper<40), "copper", cuts=cuts, col.regions=cols)
gridExtra::grid.arrange(a, b, ncol=2)

enter image description here

+1

All Articles