您好我有以下代码
train=jsonlite::fromJSON("/home/felipe/Templates/Archivo de prueba/databritanica.json")
map<-get_map(location="united kingdom" ,zoom=12,source="osm")
p <- ggmap(map) +
geom_point(data=train, aes(x=X, y=Y, color=factor(LSOA_name)), alpha=0.05) +
guides(colour = guide_legend(override.aes = list(alpha=1.0, size=6.0),
title="Type of Crime")) +
scale_colour_brewer(type="qual",palette="Paired") +
ggtitle("Top Crimes in Britain") +
theme_light(base_size=20) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
ggsave("united kingdom_top_crimes_map.png", p, width=14, height=10, units="in")
该脚本的主要目标是从JSON文件中获取数据并映射到我使用get_map获取的地图。
执行此代码会给我这个错误:
eval(expr,envir,enclos)中的错误:找不到对象'X'
JSON文件中包含的数据具有以下格式:
[
{"Month":"2014-05",
"Longitude":-2.747770,
"Latitude":53.389499,
"Location":"On or near Cronton Road",
"LSOA_name":"Halton 001B",
"Crime_type":"Other theft"},
{"Month":"2014-05",
"Longitude":-2.799099,
"Latitude":53.354676,
"Location":"On or near Old Higher Road",
"LSOA_name":"Halton 008B",
"Crime_type":"Anti-social behaviour"},
Thousands of lines more
]
我在这里缺少什么?可能是什么问题?
更新
这是应用str(train)之后的输出,就像用户pascal说的那样:
str(train)
'data.frame': 10632 obs. of 6 variables:
$ Month : chr "2014-05" "2014-05" "2014-05" "2014-05" ...
$ Longitude : num -2.75 -2.8 -2.8 -2.87 -2.87 ...
$ Latitude : num 53.4 53.4 53.4 53.5 53.5 ...
$ Location : chr "On or near Cronton Road" "On or near Old Higher Road" "On or near Higher Road" "On or near Sanderling Road" ...
$ LSOA_name : chr "Halton 001B" "Halton 008B" "Halton 008B" "Knowsley 001A" ...
$ Crime_type: chr "Other theft" "Anti-social behaviour" "Anti-social behaviour" "Anti-social behaviour" ...
现在,这是应用dput(head(train))之后的另一个输出,就像用户42所说的那样
> dput(head(train))
structure(
list(
Month = c("2014-05", "2014-05", "2014-05", "2014-05",
"2014-05", "2014-05"),
Longitude = c(-2.74777, -2.799099, -2.804451,
-2.873557, -2.871609, -2.871841),
Latitude = c(53.389499, 53.354676,
53.352456, 53.485957, 53.489432, 53.489745),
Location = c("On or near Cronton Road",
"On or near Old Higher Road", "On or near Higher Road", "On or near Sanderling Road",
"On or near Watts Close", "On or near Gilescroft Avenue"),
LSOA_name = c("Halton 001B",
"Halton 008B", "Halton 008B", "Knowsley 001A", "Knowsley 001A",
"Knowsley 001A"),
Crime_type = c("Other theft", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour",
"Anti-social behaviour")),
.Names = c("Month", "Longitude", "Latitude",
"Location", "LSOA_name", "Crime_type"),
row.names = c(NA, 6L), class = "data.frame")
这里是变量“train”的输出