【问题标题】:ggplot2 is not printing all the information I need in Rggplot2 没有打印我在 R 中需要的所有信息
【发布时间】:2015-12-02 08:56:10
【问题描述】:

我正在尝试复制以下脚本: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 文件中读取数据,并尝试根据数据在地图上打印点。每个点都是一种犯罪类型,每个点的位置取决于两个参数:经度和纬度。

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

这是原图:

结果如下:

有什么想法吗??

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"}

]

【问题讨论】:

  • 您是否测试过更高的 alpha 值? 0.05 的 alpha 使点几乎透明,即使没有背景贴图我也几乎看不到。
  • 您还需要在所有的library() 电话中冷静下来。看起来您需要显式调用的唯一包是 ggplot2ggmapdplyrjsonlite

标签: json r ggplot2


【解决方案1】:

简答:

正如@aosmith 所述,当您的alpha = 0.05 绘制在彩色地图背景上时,这些点实际上是不可见的。

更长的答案:

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

  • alpha 增加到更合理的值
  • 增加size的点数
  • (可选)将shape 更改为带背景并填充以提高可见性
    • 这将要求您将aes 中的fill 参数以及scale_color_brewer 更改为scale_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())

【讨论】:

  • 您好,非常感谢您的回答。它工作得很好,我赞成并接受了你的回答。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-01-04
  • 1970-01-01
  • 2020-09-01
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2011-03-21
相关资源
最近更新 更多