N/A - Anyone experienced with R?

Flying_Nun

Line Up and Wait
Joined
Oct 7, 2011
Messages
537
Location
KLOU
Display Name

Display name:
Flying_Nun
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
 
R, it's drivin me nuts.
 
Not until Talk Like a Pirate Day.
 
I've never even seen r code before so I'm assuming a lot that I don't really know but I'll take a stab at this...

If I'm reading your data set right I see 8 column headers but in the data rows I see 9 columns of data. If I'm reading that first line of code right I'm guessing it's supposed to read the first row and glean it's column names from that so if it's reading in 8 columns to drop data variables into... what an array? and then you're proceeding to read in a data set of 9 on each line it might just be gumming up the works.
 
Back
Top