R:使用经度/纬度分散点 [英] R: scatter points using longitude/latitude

查看:134
本文介绍了R:使用经度/纬度分散点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在地图上填充颜色。但是,该图并没有按比例显示。



如何可视化经度和纬度的数据?

  install.packages( WDI)
install.packages( tidyverse)

库(WDI)
库( tidyverse)

literacy_globe<-WDI(国家=全部,指标= SE.ADT.LITR.ZS,开始= 2015,结束= 2018,额外= TRUE)

literacy_globe<-na.omit(literacy_globe)

ggplot(literacy_globe,aes(x =经度,y =纬度,组= iso3c))+
geom_point(aes(填充=收入),颜色=白色)

我想要的结果类似于:

解决方案

您可以使用以下代码

 #加载所需的软件包
库(WDI)
库(tidyverse)
库(maptools)
库( ggplot2)
库( sf)

#下载数据
literacy_globe<-WDI(国家=所有,指标= SE.ADT.LITR.ZS,开始= 2015,结束= 2018,额外= TRUE)

#删除NAs
literacy_globe_1<-na.omit(literacy_globe)

#将数据另存为.csv文件,因为您的数据包含非NAs的空白单元格
写.csv(literacy_globe_1, literacy_globe_1.csv)

#从.csv文件中读取数据
data<-read.csv( literacy_globe_1.csv)

#删除NAs
literacy_globe<-na.omit(数据)
摘要(literacy_globe)
头(literacy_globe,2)

#使用ggplot2软件包映射
data(wrld_simpl)

#sp到sf转换
world<-st_as_sf(wrld_simpl)

#现在创建地图
ggplot(世界)+
geom_sf(颜色=黑色,填充= NA)+ coord_sf(expand = FALSE)+
theme_bw()+ geom_point(aes(经度,纬度) ,data = literacy_globe,colour = alpha(红色,0.7))


更新


choroppleth map

  literacy_globe<-WDI(country = all,指标= SE.ADT.LITR.ZS,开始= 2015,结束= 2018,Extra = TRUE)

literacy_globe<-na.omit(literacy_globe)
摘要(literacy_globe)
head(literacy_globe,2)

#使用ggplot2软件包
data(wrld_simpl)

#加强形状文件进入数据框
wrld_simpl.f<-fortify(wrld_simpl,region = " NAME")
类(wrld_simpl.f)

头(wrld_simpl.f)

#与系数合并并重新排序
merge.shp< ; -merge(wrld_simpl.f,literacy_globe,by.x = id,by.y = country,all.x = TRUE)
final.plot< -merge.shp [order(merge .shp $ order),]

head(final.plot,2)
#基本图
ggplot()+
geom_polygon(data = final.plot,
aes(x =长,y =纬度,组=组,填充=收入),
颜色=黑色,大小= 0.25)


I want to fill the colour on a map. However, the plot doesn't come out as aspected.

How can I visualize the data with longitude and latitude?

install.packages("WDI")
install.packages("tidyverse")

library(WDI)
library(tidyverse)

literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

literacy_globe <- na.omit(literacy_globe)

ggplot(literacy_globe, aes(x = longitude, y = latitude, group = iso3c)) +
    geom_point(aes(fill = income), colour = "white")

I'd like the result similar to:

解决方案

You can use the following code

#Loading the required packges
library(WDI)
library(tidyverse)
library(maptools)
library("ggplot2")
library("sf")

#Downloading the data
literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

#Removing the NAs
literacy_globe_1 <- na.omit(literacy_globe)

#Saving the data as .csv file as your data contains blank cells which are not NAs
write.csv(literacy_globe_1, "literacy_globe_1.csv")

#Reading the data from .csv file
data <- read.csv("literacy_globe_1.csv")

#Removing the NAs
literacy_globe <- na.omit(data)
summary(literacy_globe)
head(literacy_globe,2)

#Mapping using ggplot2 package
data(wrld_simpl)

#sp to sf conversion
world <- st_as_sf(wrld_simpl)

# now create the map
ggplot(world) +
  geom_sf(colour = "black", fill = NA) + coord_sf(expand = FALSE) + 
  theme_bw() + geom_point(aes(longitude, latitude),data= literacy_globe, colour=alpha("red",0.7))

For white fill of polygon and grey outside area, you can use

ggplot(world) +
  geom_sf(colour = "black", fill = "white") + coord_sf(expand = FALSE) + 
  geom_point(aes(longitude, latitude),data= literacy_globe, colour=alpha("red",0.7)) 

Update

choropleth map

literacy_globe <- WDI(country = "all", indicator = "SE.ADT.LITR.ZS", start = 2015, end = 2018, extra = TRUE)

literacy_globe <- na.omit(literacy_globe)
summary(literacy_globe)
head(literacy_globe,2)

#Using ggplot2 package
data(wrld_simpl)

#fortify shape file to get into dataframe 
wrld_simpl.f <- fortify(wrld_simpl, region = "NAME")
class(wrld_simpl.f)

head(wrld_simpl.f)

#merge with coefficients and reorder
merge.shp<-merge(wrld_simpl.f,literacy_globe, by.x = "id", by.y = "country", all.x=TRUE)
final.plot<-merge.shp[order(merge.shp$order), ] 

head(final.plot, 2)
#basic plot
ggplot() +
  geom_polygon(data = final.plot, 
               aes(x = long, y = lat, group = group, fill = income), 
               color = "black", size = 0.25) 

这篇关于R:使用经度/纬度分散点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆