0

I have run a factor analysis on a spatial dataset, and I would like to plot the results on a map so that the color of each individual point (location) is a combination in a RGB/HSV space of the scores at that location of the three factors extracted.

I am using base R to plot the locations, which are in a SpatialPointsDataFrame created with the spdep package:

Libraries

library(sp)
library(classInt)

Sample Dataset

fas <- structure(list(MR1 = c(-0.604222013102789, -0.589631093835467, 
                         -0.612647301042234, 2.23360319770647, -0.866779007222414), MR2 = c(-0.492209397489792, 
                                                                                            -0.216810726717787, -0.294487678489753, -0.60466348557844, 0.34752411748663
                         ), MR3 = c(-0.510065798219453, -0.61303212834454, 0.194263734935779, 
                                    0.347461766159926, -0.756375966467285), x = c(1457543.717, 1491550.224, 
                                                                                  1423185.998, 1508232.145, 1521316.942), y = c(4947666.766, 5001394.895, 
                                                                                                                                4948766.5, 4950547.862, 5003955.997)), row.names = c("Acqui Terme", 
                                                                                                                                                                                     "Alagna", "Alba", "Albera Ligure", "Albuzzano"), class = "data.frame")

Create spatial object

fas <- SpatialPointsDataFrame(fas[,4:5], fas,
                          proj4string = CRS("+init=EPSG:3003"))

Plotting function

map <- function(f) {
pal <- colorRampPalette(c("steelblue","white","tomato2"), bias = 1)
collist <- pal(10)
class <- classIntervals(f, 8, style = "jenks")
color <- findColours(class, collist)
plot(fas, pch=21,cex=.8, col="black",bg=color)
}

#example usage
#map(fas$MR1)

The above code works well for producing a separate plot for each factor. What I would like is a way to produce a composite map of the three factors together.

Many thanks in advance for any suggestion.

eazyezy
  • 21
  • 4
  • 1
    Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? What are `f` and `points`? – jrcalabrese Feb 09 '23 at 18:11
  • I have edited the question with the complete function I am using to map the points, I hope it clarifies the question a bit more, if not I will try to include a dummy example. I apologize if it's still not clear, I'm new on SO and figuring out how to properly format code in comments and questions – eazyezy Feb 10 '23 at 14:47
  • If `f` and `points` aren't built into an R package, can you provide `f` and `points` using `dput()`? If your data is particularly large, you may want to recreate your post using a spatial dataset already built-into R. Also please include any relevant `library()` calls in your code. – jrcalabrese Feb 10 '23 at 16:11
  • I created a reproducible example with a sample of the first 5 rows of the dataset. Many thanks for introducing me to dput! :) – eazyezy Feb 10 '23 at 18:25

1 Answers1

1

I found a solution through this post! With the data shown above, it goes like this:

#choose columns to map to color
colors <-fas@data[,c(1:3)] 
#set range from 0 to 1
range_col <- function(x){(x-min(x))/(max(x)-min(x))}
colors_norm <- range_col(colors)
print(colors_norm)
#convert to RGB
colors_rgb <- rgb(colors_norm)
print(colors_rgb)
#plot
plot(fas, main="Color Scatterplot", bg=colors_hex, 
col="black",pch=21)
eazyezy
  • 21
  • 4