1
votes

I am trying to plot addresses on a precinct map. My program uses the following libraries:

# load libraries
library(ggplot2)
library(maptools)
library(mapproj)
library(plyr)
library(sp)
library(rgdal)
library(rgeos)
library(ggmap)

The relevant code is below:

#Generating Map
#f.dist_1 contains longitude, latitude and a group id identifting precincts

distMap <- ggplot(data = f.dist_1, aes(x=longitude, y=latitude, group = id))

#Create map file and precinct outlines 

distMap <- distMap + geom_polygon(fill="aliceblue") 
distMap <- distMap + geom_path(color= "black",aes(group=group))

#f.canvassu2 contains household data, longitude ("lon") and latitude ("lat") value
#Plot selected households; this statement throws the error:

Don't know how to automatically pick scale for object of type function. Defaulting to continuous Error in data.frame(x = c(-105.0038579, -105.003855, -105.002154, -105.001437, : arguments imply differing number of rows: 48, 0

distMap <- distMap + geom_point(data=f.canvassu2,aes(x=lon, y=lat), size=2)

The "log" and "lat" variables in the f.canvassu2 data frame are numeric.

Does anyone know why the "Don't know how to automatically pick scale for object..." error occurs? How have others resolved this error?

2
Welcome to SO. :) At the moment, it is hard to see what you were trying to do since you have not provided any concrete data. If you want to receive some support from SO users, you want to post reproducible code and sample. Would you please consider to revise your question? Meanwhile I think your f.dist_1 seems to be a SpatialPolygonDataFrame. If not, that is already covered to data.frame using fortify. I am just guessing here though. Please update your question. :)jazzurro
dput(head(f.canvassu2)) & dput(head(f.dist_1)) would really helphrbrmstr

2 Answers

0
votes

Use this instead

# using aesthetic to just load data to map
distMap <- distMap + aes(x=as.numeric(f.canvassu2$lon), y=as.numeric(f.canvassu2$lon)) + geom_point(size=2)

# just check if map is displayed
distMap

and let me know if it works for you.

0
votes

If you're a package developer and came here because your new class throws this error, you can fix it by 'adding' a function to ggplot2:

x <- letters[1:10]
class(x) <- "my_great_class" # here starts the problem
y <- runif(10)

ggplot(data.frame(x = x, y = y), aes(x = x, y = y)) + 
  geom_col()
#> Don't know how to automatically pick scale for object of type my_great_class. Defaulting to continuous.
#> Error: Discrete value supplied to continuous scale

# ------ ADD THIS TO YOUR PACKAGE ------
#' @exportMethod scale_type.my_great_class
#' @export
#' @noRd
scale_type.my_great_class <- function(x) {
  "discrete"
}
# ------------- UNTIL HERE -------------

# now works:
ggplot(data.frame(x = x, y = y), aes(x = x, y = y)) +
  geom_col()

So scale_type() is a generic function of ggplot2. Glad these folks thought about us :)