Flying_Nun
Line Up and Wait
Borrowed some code I found on stack overflow but having problems getting my data to map in place of some random data that was in the code. See below.
sales_data = read.xlsx("IncomingSales Wk1.xlsx", sheet = "ZipSum", startRow = 1, colNames = TRUE,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
namedRegion = NULL, na.strings = "0", fillMergedCells = FALSE)
# Prepare the zip poly data for US
mydata <- readOGR(dsn = "Heatmaps", layer = "cb_2017_us_zcta510_500k")
# Texas zip code data
zip <- read_csv("zip_code_database.csv")
us <- filter(zip, country == "US")
# Get polygon data for TX only
mypoly <- subset(mydata, ZCTA5CE10 %in% us$zip)
# Create a new group with the first three digit.
# Drop unnecessary factor levels.
mypoly$group <- substr(mypoly$ZCTA5CE10, 1,3)
mypoly$ZCTA5CE10 <- droplevels(mypoly$ZCTA5CE10)
# Merge polygons using the group variable
# Create a data frame for ggplot.
mypoly.union <- unionSpatialPolygons(mypoly, mypoly$group)
mymap <- fortify(mypoly.union)
# Check how polygons are like
plot(mypoly)
plot(mypoly.union, add = T, border = "red", lwd = 1)
# Convert SpatialPolygons to data frame and add values
require(sp)
mypoly.df <- merge(mypoly, sales_data, by="group", duplicateGeoms=TRUE)
# Find a center point for each zip code area
centers <- data.frame(gCentroid(spgeom = mypoly.union, byid = TRUE))
centers$zip <- rownames(centers)
# Finally, drawing a graphic
ggplot() +
scale_x_continuous(limits = c(-125,-67)) +
scale_y_continuous(limits = c(25,50)) +
geom_cartogram(data = mymap, aes(x = long, y = lat, map_id = id), map = mymap) +
geom_cartogram(data = mypoly.df, aes(fill = Sales, map_id = group), map = mymap) +
# geom_text_repel(data = centers, aes(label = zip, x = x, y = y), size = 3) +
scale_fill_gradientn(colours = rev(brewer.pal(10, "Spectral"))) +
coord_map() +
theme_map()
Getting the error:
Regions defined for each Polygons
Error in FUN(X[], ...) : object 'Sales' not found
But Sales is in the data frame:
head (mypoly.df)
An object of class "SpatialPolygonsDataFrame"
Slot "data":
group ZCTA5CE10 AFFGEOID10 GEOID10 ALAND10 AWATER10 Orders Sales
11419 354 35442 8600000US35442 35442 610213891 10838694 4 3113.19
29211 853 85365 8600000US85365 85365 3545016067 9766486 4 509.28
24749 719 71973 8600000US71973 71973 204670474 1264709 1 186.13
31376 954 95445 8600000US95445 95445 221559097 7363179 1 56.00
1945 068 06870 8600000US06870 06870 5945321 3841130 5 541.17
6195 199 19964 8600000US19964 19964 24601672 124382 2 185.22
sales_data = read.xlsx("IncomingSales Wk1.xlsx", sheet = "ZipSum", startRow = 1, colNames = TRUE,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
namedRegion = NULL, na.strings = "0", fillMergedCells = FALSE)
# Prepare the zip poly data for US
mydata <- readOGR(dsn = "Heatmaps", layer = "cb_2017_us_zcta510_500k")
# Texas zip code data
zip <- read_csv("zip_code_database.csv")
us <- filter(zip, country == "US")
# Get polygon data for TX only
mypoly <- subset(mydata, ZCTA5CE10 %in% us$zip)
# Create a new group with the first three digit.
# Drop unnecessary factor levels.
mypoly$group <- substr(mypoly$ZCTA5CE10, 1,3)
mypoly$ZCTA5CE10 <- droplevels(mypoly$ZCTA5CE10)
# Merge polygons using the group variable
# Create a data frame for ggplot.
mypoly.union <- unionSpatialPolygons(mypoly, mypoly$group)
mymap <- fortify(mypoly.union)
# Check how polygons are like
plot(mypoly)
plot(mypoly.union, add = T, border = "red", lwd = 1)
# Convert SpatialPolygons to data frame and add values
require(sp)
mypoly.df <- merge(mypoly, sales_data, by="group", duplicateGeoms=TRUE)
# Find a center point for each zip code area
centers <- data.frame(gCentroid(spgeom = mypoly.union, byid = TRUE))
centers$zip <- rownames(centers)
# Finally, drawing a graphic
ggplot() +
scale_x_continuous(limits = c(-125,-67)) +
scale_y_continuous(limits = c(25,50)) +
geom_cartogram(data = mymap, aes(x = long, y = lat, map_id = id), map = mymap) +
geom_cartogram(data = mypoly.df, aes(fill = Sales, map_id = group), map = mymap) +
# geom_text_repel(data = centers, aes(label = zip, x = x, y = y), size = 3) +
scale_fill_gradientn(colours = rev(brewer.pal(10, "Spectral"))) +
coord_map() +
theme_map()
Getting the error:
Regions defined for each Polygons
Error in FUN(X[], ...) : object 'Sales' not found
But Sales is in the data frame:
head (mypoly.df)
An object of class "SpatialPolygonsDataFrame"
Slot "data":
group ZCTA5CE10 AFFGEOID10 GEOID10 ALAND10 AWATER10 Orders Sales
11419 354 35442 8600000US35442 35442 610213891 10838694 4 3113.19
29211 853 85365 8600000US85365 85365 3545016067 9766486 4 509.28
24749 719 71973 8600000US71973 71973 204670474 1264709 1 186.13
31376 954 95445 8600000US95445 95445 221559097 7363179 1 56.00
1945 068 06870 8600000US06870 06870 5945321 3841130 5 541.17
6195 199 19964 8600000US19964 19964 24601672 124382 2 185.22