【问题标题】:R: scatter points using longitude/latitudeR:使用经度/纬度的散点
【发布时间】:2020-01-03 21:30:34
【问题描述】:

我想在地图上填充颜色。但是,情节并没有表现出来。

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

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

我希望结果类似于:

【问题讨论】:

标签: r ggplot2 data-visualization


【解决方案1】:

您可以使用以下代码

#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))

对于多边形的白色填充和灰色外部区域,您可以使用

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)) 

更新

等值线图

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) 

【讨论】:

  • 非常感谢您的回答,您能解释一下这一步的目的吗? #sp 到 sf 的转换,世界
  • 如果我需要绘制等值线图,我该如何添加图层来填充?非常感谢
  • @James Huang 因为geom_sf( ) 需要sf 对象。 wrld_simpl 是一个 sp 对象。因此,我已将 sp 对象转换为 sf 对象。如果它回答了您的问题,您可以考虑点击√接受答案。
  • 我这里有几个问题 1) 我想我已经使用了 left_join() 来合并空间数据和读写数据。这两者有什么区别? 2)如果我使用map_data,程序是否保持不变?
  • 看到这个回答你的第一个问题stackoverflow.com/q/1299871/6123824
【解决方案2】:

我找到了另一种在世界地图上绘制层次结构散点图的方法,但我不确定它是否有一些 drawbakcs。

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

lit.long <- literacy_globe$longitude
lit.lat <- literacy_globe$latitude
income <- literacy_globe$income

# prepare a NULL map
mp<-NULL 
mapworld<-borders("world",colour = "gray50",fill="white")
#mp = empty map

#plot a map
mp <- ggplot() + mapworld + ylim(-60,90)

#geom_point plot the data on it 
mp2 <- mp + geom_point(aes(x = lit.long, y = lit.lat), color = "darkblue",
fill = income) + 
scale_size(range = c(1,1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 2012-04-19
    • 2021-10-10
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    相关资源
    最近更新 更多