我正在尝试在区域地图上绘制地址。我的程序使用以下库:
# load libraries
library(ggplot2)
library(maptools)
library(mapproj)
library(plyr)
library(sp)
library(rgdal)
library(rgeos)
library(ggmap)
相关代码如下:
#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:
不知道如何自动选择类型函数对象的比例。违约持续 data.frame中的错误(x = c(-105.0038579,-105.003855,-105.002154,-105.001437,: 参数意味着不同的行数:48,0
distMap <- distMap + geom_point(data=f.canvassu2,aes(x=lon, y=lat), size=2)
f.canvassu2数据框中的“log”和“lat”变量是数字。
有谁知道为什么“不知道如何自动选择对象缩放...”错误发生? 其他人如何解决这个错误?
答案 0 :(得分:0)
改为使用
# 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
让我知道它是否适合你。
答案 1 :(得分:0)
如果您是程序包开发人员,并且因为新类抛出此错误而来这里,则可以通过将功能“添加”到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()
因此scale_type()
是ggplot2
的通用函数。很高兴这些人想到我们:)