Hi everyone!
So I'm new to R, but I'm trying to map in/out provincial migration with R Studio. I've been using the code provided here: RPubs - Using ipumsr. The code works well, but when I get to the part where I am creating the map objects, I get this message when I run prov_point:
Warning message:
In st_point_on_surface.sf(.) :
st_point_on_surface assumes attributes are constant over geometries of x
And so I subsequently get this message when I try to run prov_lines:
Error in Ops.sfc(xi, xj) : operation > not supported
At this point, I've tried looking at the other forums to see why this is going wrong, but I can't figure it out. Anyone have suggestions?
Here's the code:
Create map objects ----
China 2000 Shape File from:
IPUMS International
vignette("ipums-geography", package = "ipumsr")
proj4_china <- "+proj=lcc +lat_1=18 +lat_2=24 +lat_0=21 +lon_0=114 +x_0=500000 +y_0=500000 +ellps=WGS72 +towgs84=0,0,1.9,0,0,0.814,-0.38 +units=m +no_defs "
proj4_latlon <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
provinces <- read_ipums_sf("geo1_cn2000.zip", verbose = FALSE) %>%
ms_simplify(keep = 0.01)
#This is where I get the warning message
prov_point <- provinces %>%
st_transform(proj4_china) %>%
st_point_on_surface() %>%
st_transform(proj4_latlon) %>%
select(IPUM2000, ADMIN_NAME)
#This is where I get the message that the operation is not supported
prov_lines <- prov_point %>%
st_transform(proj4_latlon) %>%
crossing(., .) %>%
as_data_frame() %>%
rename(GEO1START = IPUM2000, NAMESTART = ADMIN_NAME, GEO1END = IPUM20001, NAMEEND = ADMIN_NAME1) %>%
filter(GEO1START != GEO1END) %>%
mutate(
geometry = gcIntermediate(as(geometry, "Spatial"), as(geometry1, "Spatial")) %>%
map(st_linestring) %>%
st_sfc()
) %>%
select(-geometry1) %>%
st_as_sf()
here is where I can no longer run the code without prov_lines
prov_lines_data <- ipums_shape_inner_join(
migration_by_province,
prov_lines,
by = c("GEO1START", "GEO1END"),
verbose = FALSE
) %>%
mutate(
ANON = N_IN < 20 | N_OUT < 20,
WT_N_IN = ifelse(ANON, NA, WT_N_IN),
WT_N_OUT = ifelse(ANON, NA, WT_N_OUT),
LINE_WIDTH = sqrt(abs(WT_N_NET)) / 100,
COLOR = ifelse(WT_N_NET < 0, "#f1a340", "#998ec3"),
LABEL = map(paste0(
NAMESTART, " - ", NAMEEND,
"
In: ", comma(WT_N_IN),
"
Out: ", comma(WT_N_OUT),
"
Net: ", comma(WT_N_NET)
), HTML)
)