ggplot2没有在R中打印我需要的所有信息

时间:2015-12-01 22:52:59

标签: json r ggplot2

我正在尝试复制以下脚本:San Francisco Crime Classification

这是我的代码:

library(dplyr)
library(ggmap)
library(ggplot2)
library(readr)
library(rjson)
library(RCurl)
library(RJSONIO)
library(jsonlite)



train=jsonlite::fromJSON("/home/felipe/Templates/Archivo de prueba/databritanica.json")

counts <- summarise(group_by(train, Crime_type), Counts=length(Crime_type))
#counts <- counts[order(-counts$Crime_type),]
# This removes the "Other Offenses" category
top12 <- train[train$Crime_type %in% counts$Crime_type[c(1,3:13)],]


map<-get_map(location=c(lon = -2.747770, lat = 53.389499)  ,zoom=12,source="osm")

p <- ggmap(map) +
  geom_point(data=top12, aes(x=Longitude, y=Latitude, color=factor(Crime_type)), 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文件中读取数据,并尝试根据数据在地图上打印点。每个点都是已经提交的犯罪类型,每个点的位置取决于两个参数:经度和纬度。

有什么问题?这些要点没有印刷。该脚本生成一个没有要显示的点的新地图。

这是原始地图:http://i.imgur.com/agSZ3sP.png

这就是结果:enter image description here

任何想法??

这个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"},

{"Month":"2014-05","Longitude":-2.804451,"Latitude":53.352456,"Location":"On or near Higher Road","LSOA_name":"Halton 008B","Crime_type":"Anti-social behaviour"}

]

1 个答案:

答案 0 :(得分:1)

简短回答:

如同@aosmith所提到的那样,当您在彩色地图背景上绘制时,alpha = 0.05使点几乎不可见。

更长的答案:

我建议您对geom_point进行以下更改:

  • alpha增加到更合理的内容
  • 增加积分的size
  • 或者,将shape更改为具有背景的一个,并填写以获得更好的可见性
    • 这将要求您更改fill中的aes参数,以及scale_color_brewerscale_fill_brewer

示例:

# Load required packages
library(dplyr)
library(ggplot2)
library(ggmap)
library(jsonlite)

# Example data provided in question, with one manually entered entry with
# Crime_type = "Other Offenses"
'[
{"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"},
{"Month":"2014-05","Longitude":-2.804451,"Latitude":53.352456,"Location":"On or near Higher Road","LSOA_name":"Halton 008B","Crime_type":"Anti-social behaviour"},
{"Month":"2014-05","Longitude":-2.81,"Latitude":53.36,"Location":"On or near Higher Road","LSOA_name":"Halton 008B","Crime_type":"Other Offenses"}

]' -> example_json
train <- fromJSON(example_json)

# Process the data, the dplyr way
counts <- train %>% 
  group_by(Crime_type) %>% 
  summarise(Counts = length(Crime_type))

# This removes the "Other Offenses" category
top12 <- train %>% 
  filter(Crime_type != "Other Offenses")

# Get the map
map <- get_map(location=c(lon = -2.747770, lat = 53.389499), zoom=12, source="osm")

# Plotting code
p <- ggmap(map) +
  # Changes made to geom_point. 
  # I increased the alpha and size, and I used a shape that has 
  # a black border and a fill determined by Crime_type.
  geom_point(data=top12, aes(x=Longitude, y=Latitude, fill=factor(Crime_type)),
             shape = 21, alpha = 0.75, size = 3.5, color = "black") +
  guides(fill = guide_legend(override.aes = list(alpha=1.0, size=6.0),
                               title="Type of Crime")) +
  # Changed scale_color_brewer to scale_fill_brewer
  scale_fill_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())

Better map