0
votes

I'm learning R at the moment and I'm at my wits end trying to figure out how to define two colours on spatial data points on a map I have made in R. In progress map

In the data frame there are three variables, x and y coordinates and then observed. 0 meaning no observation, 1 meaning observed. I wish to allocate a purple colour to the observed (1) and a red colour to non-observed (0).

This is the code I've used to make my map so far:

plot(Bp_shp4, col = "grey", axes = TRUE, xlab = "Longtitude", ylab = "Latitude", xlim = c(-1, 35), ylim = c(35, 60), main = expression('Sampling sites for 'italic('Brachytron pretense')' presence or absence'))

plot(Bp_hydro, pch = 20, col = "cyan", cex = 0.5, add = TRUE)

plot(Bp_Spatial_df, pch = 20, cex = 1, add = TRUE,)

Bp_Spatial_df is my data frame with the three variables, however if i specify a colour it only colours one unanimous colour, I've tried making it a factor but cannot add it to my existing code without the points just not plotting.

Any help would be appreciated so much, I've hit a complete stop in my assignment and it's pretty much just the start of it...

Str(data) of all data frames in my project:

loaded .CSV:

    > str(Bp_Coord)
     'data.frame':  93 obs. of  3 variables:
     $ x       : num  7.29 9.88 1.12 -3.88 22.21 ...
     $ y       : num  43.9 54 49.3 43.1 40.6 ...
     $ Observed: int  1 1 1 1 1 1 1 1 1 0 ...

**Spatial point assignment**
coords <- SpatialPoints(Bp_Coord[, c("x", "y")])
> str(coords)
Formal class 'SpatialPoints' [package "sp"] with 3 slots
  ..@ coords     : num [1:93, 1:2] 7.29 9.88 1.12 -3.88 22.21 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] -9.12 37.21 26.46 58.62
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

Spatial point data frame code:

Bp_Spatial_df <- SpatialPointsDataFrame(coords, Bp_Coord)
proj4string(Bp_Spatial_df) <- CRS("+proj=longlat +ellps=WGS84")

> str(Bp_Spatial_df)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 93 obs. of  3 variables:
  .. ..$ x       : num [1:93] 7.29 9.88 1.12 -3.88 22.21 ...
  .. ..$ y       : num [1:93] 43.9 54 49.3 43.1 40.6 ...
  .. ..$ Observed: int [1:93] 1 1 1 1 1 1 1 1 1 0 ...
  ..@ coords.nrs : num(0) 
  ..@ coords     : num [1:93, 1:2] 7.29 9.88 1.12 -3.88 22.21 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] -9.12 37.21 26.46 58.62
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=longlat +ellps=WGS84"

Other two dataframes are just shapefiles.

1
You need to use a vector of colors - one color for each point that you are plotting.G5W

1 Answers

0
votes

I wish to allocate a purple colour to the observed (1) and a red colour to non-observed (0).

Create the colors first:

cols <- c("red", "purple")

Then, because observed is a numeric vector, you can use it to index these pre-defined colors in your data. But add 1L to make it a (1,2) vector since indexing starts at 1 in R.

plot(Bp_Spatial_df, pch = 20, col = cols[Bp_Spatial_df$Observed+1L], cex = 0.5, add = TRUE)