How to plot interactive and thematic maps in R?

Viewed 69

I have two files named counties.rds and houses1990.rds. The first one (counties.rds) includes conties of Caliornia state in the USA, and the second one (houses1990.rds) gives us some information about houses. I used the following codes to creat the variables Cali_s and Houses-Cali as follow:

Cali_s <- readRDS("counties.rds")
Cali_s <- raster::aggregate(California_shp, by = "NAME")
Houses-Cali <- readRDS("houses1990.rds")

To give you some information about Cali_s and Houses-Cali, the output of dput(head(Cali_s)) and dput(head(Houses_Cali)) are as follow:

dput(head(Cali_s))
structure(list(NAME = c("Alameda", "Alpine", "Amador", "Butte", 
"Calaveras", "Colusa")), row.names = c(NA, 6L), class = "data.frame")

dput(head(Houses_Cali))
structure(list(houseValue = c(452600L, 358500L, 352100L, 341300L, 
342200L, 269700L), income = c(8.3252, 8.3014, 7.2574, 5.6431, 
3.8462, 4.0368), houseAge = c(41L, 21L, 52L, 52L, 52L, 52L), 
    rooms = c(880L, 7099L, 1467L, 1274L, 1627L, 919L), bedrooms = c(129L, 
    1106L, 190L, 235L, 280L, 213L), population = c(322L, 2401L, 
    496L, 558L, 565L, 413L), households = c(126L, 1138L, 177L, 
    219L, 259L, 193L), latitude = c(37.88, 37.86, 37.85, 37.85, 
    37.85, 37.85), longitude = c(-122.23, -122.22, -122.24, -122.25, 
    -122.25, -122.25)), row.names = c(NA, 6L), class = "data.frame")

I used the following codes to plot the below interactive map displaying the boundaries:

tmap_mode("view")
tm_shape(Cali_s) +tm_borders(alpha = 0.9,col ="BLUE")+ tm_text("NAME", size = 0.7)

enter image description here

  • Considering the houseValue which is inside the Houses-Cali as the target (dependent) variable, I want to plot the following map (named map 1):

enter image description here

  • Furthermore, using the point-in-polygon operation and the mean function, I want to merge the Houses-Cali object to Cali_sin order to plot the following thematic map (named map 2) of houseValue variable:

enter image description here

Could you please help me with how I can plot the two above maps (map 1 and map 2)?

Thank you in advance for your help

0 Answers
Related