Unable to create dot-density map in RStudio

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.

It would have been easier to help with a reproducible example. The data from tblGeocodeTest are missing so I'm not sure that what I propose will actually work.

Anyway, your problem probably comes from the fact that sf_data is a sf object that ggplot fails to coerce into something it can use. In practice, you should extract yourself the points from sf_data using st_coordinates() and put them in a data frame or tibble before plotting. The code below should do the job, although I could not test it.

Loading libraries and your data:

library(tidyverse)
library(sf)

filtered_data <- subset(tblGeocodeTest, TRACTCODE == "005204" & STATE_CODE == "41" & COUNTY_CODE == "053")
sf_data <- st_as_sf(filtered_data, coords = c("Longitude_X", "Latitude_Y"), crs = 4326)

This converts the sf object. I renamed the variables Longitude_X and Latitude_Y to make them consistent with your code but the st_coordinates()output simply calls them X and Y.

points <- sf_data  %>%
  st_coordinates() %>%
  as_tibble() %>%
  rename(
    Longitude_X = X,
    Latitude_Y = Y
  )

Plotting: note data = points in geom_point()

ggplot() +
  geom_sf(data = st_make_grid(sf_data, n = 100, square = TRUE), color = "grey") +
  geom_point(aes(x = Longitude_X, y = Latitude_Y), data=points, size = 0.5) + 
  theme_minimal() +
  labs(title = "Dot-Density Map for Census Tract 0052.04 in Polk County, Oregon")

Final note. I removed the color = "dots" in geom_point() and the scale_color_manual()line, since this will probably cause an error (where does "dots" come from?). I guess this is just one of the weird additions that ChatGPT puts in the code. To control the color of the points, just use color="black" (or any other color) in geom_point().

The quality of Chat GPT suggestions is directly proportional to the volume of available training data; spatial data manipulation and presentation in R is a niche topic.

You may want to clarify your question a bit:

  • do you have a dataframe ("shapefile") with actual dots, which you want displayed? then ggplot2::geom_sf() should be your friend
  • do you have a dataframe with polygons (census tracts?) that contain some sort of value whose intensity you want to display? then you may want to check ggpatern::geom_sf_pattern()

Thank you for taking the time to respond. Your code worked without any modification! The map needs a lot of changes and additions to display what I want so I will need to spend time learning how to make those. However, your code provided the breakthrough I needed. I'm sure I will have more questions going forward but, for now, you have given me a foundation upon which to build.

Thank you for responding. The code from gbravo worked, and while I need to make many changes to get the map to appear as desired, it provided a foundation upon which to build. I'm sure I will have more questions going forward.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.