1

I have an error in my codes which is: Error: Fill argument neither colors nor valid variable name(s)

In the following, I describe how I get this error. Indeed, I have two sets of data and assigned them into two variables named Caifornia_shp and Houses. The information of these two variables are as follow:

head(California_shp)
       NAME
1   Alameda
2    Alpine
3    Amador
4     Butte
5 Calaveras
6    Colusa

head(Houses)
  houseValue income houseAge rooms bedrooms population households latitude longitude
1     452600 8.3252       41   880      129        322        126    37.88   -122.23
2     358500 8.3014       21  7099     1106       2401       1138    37.86   -122.22
3     352100 7.2574       52  1467      190        496        177    37.85   -122.24
4     341300 5.6431       52  1274      235        558        219    37.85   -122.25
5     342200 3.8462       52  1627      280        565        259    37.85   -122.25
6     269700 4.0368       52   919      213        413        193    37.85   -122.25

I merged Houses and California_shp using the following codes:

pts.poly <- point.in.poly(Houses,California_shp)

head(pts.poly@data)

houseValue income houseAge rooms bedrooms population households latitude longitude
1     452600 8.3252       41   880      129        322        126    37.88   -122.23
2     358500 8.3014       21  7099     1106       2401       1138    37.86   -122.22
3     352100 7.2574       52  1467      190        496        177    37.85   -122.24
4     341300 5.6431       52  1274      235        558        219    37.85   -122.25
5     342200 3.8462       52  1627      280        565        259    37.85   -122.25
6     269700 4.0368       52   919      213        413        193    37.85   -122.25
     NAME
1 Alameda
2 Alameda
3 Alameda
4 Alameda
5 Alameda
6 Alameda

head(tapply(pts.poly@data$houseValue,list(pts.poly@data$NAME),mean))
  Alameda    Alpine    Amador     Butte Calaveras    Colusa 
208747.39 118700.00 117146.43  89611.54 107893.75  77731.25

When I want to draw a thematic map by codes:

tm_shape(California_shp)+tm_borders("Blue",lwd = 2)+tm_fill("houseValue")

I get the following error:

Error: Fill argument neither colors nor valid variable name(s)

How can I solve this error?

3
  • Try tm_shape(pts.poly). You are plotting California_shp that doesn’t have any variable named houseValue, that’s why you get that error.
    – dieghernan
    Commented Jun 18, 2021 at 11:05
  • I used your suggestion and wrote: tm_shape(pts.poly)+tm_borders("Blue",lwd = 2). Unfortunately, I get the following error: Error: pts.poly consists of spatial points, so it cannot accept tm_fill/tm_borders/tm_polygons. Commented Jun 18, 2021 at 11:19
  • See also @Spacedman comment gis.stackexchange.com/questions/401855/… Ideally try to provide the full code you used (reprex) and if possible links to the external files. You can host it on GitHub, Google Drive, etc. so we can check your steps and try to help you
    – dieghernan
    Commented Jun 18, 2021 at 12:13

1 Answer 1

0

Find a reprex here. Since you didn't provide the data I used mock data to try to put together a solution.

Note also that the solution is based on the sf package, while your objects are on sp. You can convert sp to sf using st_as_sf().

Also, if you are a beginner I would recommend you to stick to sf unless you really need to work on sp:

library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tmap)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Use sf from tigris
California_shp <- counties("California")

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

# Convert Houses to sf
Houses <- st_as_sf(Houses, coords = c("longitude", "latitude"), crs = st_crs(4326))

# Transform both shapes to same CRS
California_shp <- st_transform(California_shp, st_crs(Houses))

# Join points to identify county, transform to dataframe and create mean
pts.poly <- st_join(Houses, California_shp) %>%
  st_drop_geometry() %>%
  group_by(COUNTYFP) %>%
  summarise(houseValue = mean(houseValue))


# Join the newly created dataframe to shape by common variable
California_shp_end <- California_shp %>% left_join(pts.poly, by = "COUNTYFP")

# Plot


tm_shape(California_shp_end) +
  tm_borders("Blue", lwd = 2) +
  tm_fill("houseValue")

Created on 2021-06-18 by the reprex package (v2.0.0)

4
  • Thank you @dieghernan. I still receive errors. I need to use point_in_poly function, mean function, and tapply. Commented Jun 18, 2021 at 13:56
  • Your pts.poly is a SpatialPointDataFrame. tapply/mean gives you a vector with the mean of houseValue by NAME. If you really need to go that way, try to paste the result of your tapply back to California_shp. As you are mapping from California_shp you have to figure out the way to paste your values to that shape. All that is already solved in my reprex however I understand that for some reason you need to focus more on the process rather than in the result
    – dieghernan
    Commented Jun 18, 2021 at 14:12
  • Thank you @dieghernan. How can I paste the result of my tapply to California-shp? Commented Jun 18, 2021 at 14:16
  • Figure out the way of producing a dataframe from tapply and use merge rdocumentation.org/packages/sp/versions/1.4-5/topics/merge
    – dieghernan
    Commented Jun 18, 2021 at 14:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.