美文网首页R
R语言画地图的文章记录2

R语言画地图的文章记录2

作者: 小明的数据分析笔记本 | 来源:发表于2019-09-14 10:46 被阅读0次
    参考文献
    根据第一篇文章的内容重复第二个例子
    install.packages("mapdata")
    library(mapdata)
    library(maps)
    map('worldHires','Canada',col="red4",panel.first=grid())
    
    image.png
    canada<-map('worldHires','Canada',plot=F)
    df<-data.frame(x=canada$x,y=canada$y)
    dim(df)
    df1<-read.csv("../../PopulationDensity.csv")
    
    library(ggplot2)
    library(ggthemes)
    png("1.png")
    ggplot()+
      geom_path(data=df,aes(x=x,y=y),color="#FD9FA4")+
      theme_bw()+
      theme(axis.text = element_blank(),
            axis.ticks = element_blank(),
            axis.title = element_blank(),
            panel.border = element_blank(),
            legend.position = "none",
            legend.background = element_blank())+
      geom_point(data=df1, aes(x=LONG,y=LAT), color="blue",alpha = 0.3)
    dev.off()
    
    image.png
    重复第一篇文章的内容
    • 代码基本完全照搬第一篇文章的内容
    beijing <- c('北京&天津', 39.90419989999999, 116.4073963, 1961.24 + 1293.82)
    shanghai <- c('上海', 31.2303904, 121.4737021, 2301.91)
    zhengzhou <- c('郑州', 34.7472541716, 113.6249284647, 862.65)
    wulumuqi <- c('乌鲁木齐', 43.8266013700, 87.6168405804, 311.03)
    haerbin <- c('哈尔滨', 45.8021755616, 126.5358247345, 1063.6)
    xian <- c('西安', 34.3412614674, 108.9398165260, 846.78)
    wuhan <- c('武汉', 30.5927599029, 114.3052387810, 978.54)
    chengdu <- c('成都', 30.5702183724, 104.0647735044, 1404.76)
    lasa <- c('拉萨', 29.6441135160, 91.1144530801, 55.94)
    chongqing <- c('重庆', 29.5647048135, 106.5507137149, 2884.62)
    kunming <- c('昆明', 24.8796595146, 102.8332118852, 643.2)
    guangshen <- c('广州&深圳', 23.0206747828, 113.7517837567, 1270.08 + 1035.79)
    
    cities <- c(beijing, shanghai, zhengzhou, wulumuqi, haerbin, xian, wuhan,
               chengdu, lasa, chongqing, kunming, guangshen)
    mat.cities <- as.data.frame(matrix(cities, ncol = 4, byrow = T), stringsAsFactors = F)
    names(mat.cities) <- c('names', 'lat', 'long', 'population')
    mat.cities$population <- as.numeric(as.character(mat.cities$population)) / 100
    mat.cities$lat <- as.numeric(as.character(mat.cities$lat))
    mat.cities$long <- as.numeric(as.character(mat.cities$long))
    
    china <- map("china", plot = F)
    library(ggrepel)
    ggplot() + 
      geom_path(data = china, aes(long, lat, group = group),
                color = '#FD9FA4', show.legend = F) +
      geom_point(data = mat.cities, 
                 aes(x = long, y = lat, size = population), 
                 alpha = 0.8, color = '#8BB6D6') +
      geom_text_repel(data = mat.cities, 
                aes(x = long, y = lat, label = names), 
                family = "STHeiti") +
      labs(x = '经度', y = '纬度', 
           title = '中国十二个地区人口地图',
           size = '人口(百万)') + 
      theme_bw() +
      theme(panel.border = element_blank(),
            text = element_text(family = "STHeiti"),
            plot.title = element_text(hjust = 0.5),
            legend.position = "none")
    
    image.png
    模仿第三篇文章的内容
    library(REmap)
    destination<-c("唐山","北京","天津")
    origin<-c("南京","南京","南京")
    map_data<-data.frame(origin,destination)
    options(remap.ak="~~~") ###引号里添加自己的API
    remap(mapdata=map_data)
    
    image.png

    欢迎大家关注我的公众号
    小明的数据分析笔记本


    公众号二维码.jpg

    相关文章

      网友评论

        本文标题:R语言画地图的文章记录2

        本文链接:https://www.haomeiwen.com/subject/kzkkyctx.html