0

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. Commented Apr 3, 2020 at 0:03

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so much for answering, however when i run the code i get this error: Error in plot.xy(xy.coords(x, y), type = type, ...) : object 'observed' not found
Still the same error, I'm not sure why it doesn't think that 'Observed' exists, when I do the data frame then '$' it suggests the x, y and observed columns. Running str(Bp_Spatial_df) shows it within the data frame ($ Observed: int [1:93] 1 1 1 1 1 1 1 1 1 0 ...) Do I need to specify the column as its own factor? Sorry this is just eluding me.
Still the same, Bp_hydro is one of my shapefiles, I would assume it would have to be Bp_Spatial_df as that is where all my coordinates lie?
Well now I'm confused. Can you edit your question and show the output of str(data) for all your data frames?
So am I! I'll post it now for you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.