This is my first attempt at creating a dot-density map in RStudio so I used ChatGPT to write the code which is shown below. I'm receiving the error message: "Error in FUN(X[[i]], ...) : object 'Longitude_X' not found".
ChatGPT says " It seems that the object Longitude_X
is not being recognized, which suggests that it might not be present in the dataframe sf_data
". That is true; neither Longitude_X or Latitude_Y appear in dataframe sf_data. What does appear is a column named "geometry" that appears to contain both latitude and longitude values like this: POINT (-123.0762 44.93685). I'm guessing ChatGPT must recognize what the code is actually doing when creating sf_data. The full code is shown below.
library(ggplot2)
library(sf)
Filter the dataframe for census tract 0052.04 in Polk County, Oregon
filtered_data <- subset(tblGeocodeTest, TRACTCODE == "005204" & STATE_CODE == "41" & COUNTY_CODE == "053")
Convert dataframe to sf object and set CRS
sf_data <- st_as_sf(filtered_data, coords = c("Longitude_X", "Latitude_Y"), crs = 4326)
Create dot-density map
ggplot() +
geom_sf(data = st_make_grid(sf_data, n = 100, square = TRUE), color = "grey") +
geom_point(data = sf_data, aes(x = Longitude_X, y = Latitude_Y, color = "dots"), size = 0.5) +
scale_color_manual(values = "black", guide = FALSE) +
theme_minimal() +
labs(title = "Dot-Density Map for Census Tract 0052.04 in Polk County, Oregon")
Any suggestions on what changes I should make would be greatly appreciated.