r - 使用 shapefile 使用 R 绘制等值线图

标签 r map ggplot2 gis

你好 stackoverflow 社区!

有人可以帮助我,因为我在 R 中创建等值线图时遇到了一些困难。到目前为止,我已经将 LL 信息分配给了我的兴趣点,现在我想使用“cans”变量创建一个等值线图在高中区 (highdist_n83.shp.zip) 的数据集 (data.csv) 中。我想请教的是如何用每个地区的 jar 头总和正确填充 map 。我提供了代码,它从 dropbox 中提取示例数据文件和我想使用的形状文件。

编辑
抱歉,我忘了补充一点,当我只绘制形状文件时,我可以看到它通过 ggplot 呈现。但是,当我尝试使用“ jar ”变量的数量“填充”区域时,R 会挂起一段时间,然后才会在原始形状上呈现大量线条。不知道是不是由于以下可能的原因造成的错误

  • 形状文件不好
  • 我如何合并数据框和形状文件可能存在问题,因为我注意到在合并文件中添加了额外的行
  • 一个地区有多所学校,我在使用 ddply 时没有合并。

  • 感谢您的时间!
    ###load R scripts from dropbox
    dropbox.eval <- function(x, noeval=F) {
    require(RCurl)
    intext <- getURL(paste0("https://dl.dropboxusercontent.com/",x), ssl.verifypeer = FALSE)
    intext <- gsub("\r","", intext)
    if (!noeval) eval(parse(text = intext), envir= .GlobalEnv)
    return(intext)
    }
    
    ##pull scripts from dropbox 
    dropbox.eval("s/wgb3vtd9qfc9br9/pkg.load.r")    
    dropbox.eval("s/tf4ni48hf6oh2ou/dropbox.r")
    
    ##load packages
    pkg.load(c(ggplot2,plyr,gdata,sp,maptools,rgdal,reshape2))
    
    ###setup data frames
    dl_from_dropbox("data.csv","dx3qrcexmi9kagx")
        data<-read.csv(file='data.csv',header=TRUE)
    
    ###prepare GIS shape and data for plotting
    dropbox.eval("s/y2jsx3dditjucxu/dlshape.r")     
    temp <- tempfile()
    dlshape(shploc="http://files.hawaii.gov/dbedt/op/gis/data/highdist_n83.shp.zip", temp)
    shape<- readOGR(".","highdist_n83") #HDOE high school districts  
    shape@proj4string 
    
    shape2<- spTransform(shape, CRS("+proj=longlat +datum=NAD83"))
    
    data.2<-ddply(data, .(year, schoolcode, longitude, latitude,NAME,HDist,SDist), summarise,
            total = sum(total),
            cans= sum(cans))
    
    coordinates(data.2) <-~longitude + latitude
    shape2.df<-fortify(shape2)
    mshape2.df<- merge(shape2.df,shape2@data, by.x="id", by.y="ID",all=TRUE)
    
    newdata <- merge(mshape2.df,data.2, by.x="NAME", by.y="NAME", all=TRUE)
    newdata  <- newdata [order(newdata $order),]
    
    ###choropleth map: 
    mapPlot <- ggplot(newdata,aes(x=long, y=lat,drop=FALSE)) +
    geom_polygon(aes(fill=cans, drop=FALSE), colour = "black", lwd = 1/9,na.rm=FALSE)
    + ggtitle("Total of Cans Across State")
    print(mapPlot)
    

    最佳答案

    当您强化 shapefile 时,您最终会得到错误的 ID。相反,根据 shapefile 的行名称显式添加 ID 列并使用它进行合并。你也不告诉geom_polygon如何对 data.frame 中的行进行分组所以它们都被绘制为一个连续重叠、自相交的多边形。向 geom_polygon 添加组参数.我也喜欢用RColorBrewer为 map 选择漂亮的颜色(使用函数 brewer.pal )。

    尝试这个:

    require(RColorBrewer)
    shape2@data$id <- rownames(shape2@data)
    sh.df <- as.data.frame(shape2)
    sh.fort <- fortify(shape2 , region = "id" )
    sh.line<- join(sh.fort, sh.df , by = "id" )
    
    
    mapdf <- merge( sh.line , data.2 , by.x= "NAME", by.y="NAME" , all=TRUE)
    mapdf <- mapdf[ order( mapdf$order ) , ]
    
    ggplot( mapdf , aes( long , lat ) )+
      geom_polygon( aes( fill = cans , group = id ) , colour = "black" )+
      scale_fill_gradientn( colours = brewer.pal( 9 , "Reds" ) )+
      coord_equal()
    

    enter image description here

    关于r - 使用 shapefile 使用 R 绘制等值线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060232/

    相关文章:

    ggplot2中的R绘图填充.contour()输出

    r - 循环中的多个绘图列表 (ggplot2) - 列表元素被覆盖

    r - 根据周围的非缺失值有条件地替换缺失值

    android - Arcgis : how to get device location

    r - 包装 R 的绘图函数(或 ggplot2)以防止绘制大数据集

    json - jsonlite中使用fromJSON转换JSON时的有效数字

    c++ - 编译器使用了错误的函数原型(prototype)?

    java - 如何将 Map 封装到自定义对象中

    r - ggplot2 的 fortify 函数出错

    r - ggplot2 用颜色非线性调整比例