相关疑难解决方法(0)

如何制作一个很好的R可重复的例子

在与同事讨论性能,教学,发送错误报告或在邮件列表上搜索指导时,以及在Stack Overflow上,通常会询问可重复的示例并始终提供帮助.

您有什么建议创建优秀示例的提示?如何以文本格式粘贴中的数据结构?您还应该包含哪些其他信息?

在另外还有其他招数来使用dput(),dump()structure()?你什么时候应该包括library()require()声明?其中保留字应避免一个,此外c,df,data等?

怎样才能成为一位伟大的重复的例子?

r r-faq

2474
推荐指数
23
解决办法
28万
查看次数

对于R中的空间对象,使用fortify {ggplot2}将地图数据转换为数据框

我用来能够毫无问题地运行这个脚本,但是现在fortify {ggplot2}命令给了我一个错误信息.任何可能是问题的暗示都会很棒!我使用fortify命令能够使用ggplot2对shapefile进行geom_map.

下面是我下载数​​据的脚本和链接.

#######################################################
#######################################################
rm(list = ls(all = TRUE))#clear workspace
getwd()
#upload packages
library(maps)
library(mapdata)
library(gridExtra)
library(rgdal)
library(rgeos)
library(ggplot2)
library(sp)
library(maptools)
gpclibPermit()

#setwd(".../FAO") FAO data are major fishing area divisions
> FAO<- readOGR(dsn="fao", layer="World_Fao_Zones")
OGR data source with driver: ESRI Shapefile 
Source: "fao", layer: "World_Fao_Zones"
with 19 features and 1 fields
Feature type: wkbPolygon with 2 dimensions
> names(FAO);dim(FAO)
[1] "zone"
[1] 19  1
> str(FAO,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 19 obs. …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
1万
查看次数

R ggplot2与shapefile和csv数据合并以填充特定的多边形

我知道这个问题已在其他地方得到解答.我试图按照@jlhoward的指示,但显然我的技能太有限了.我可以再问你的帮助,R社区吗?

这就是我所拥有的:

瑞士的shapefile:链接

以及相应的CSV文件,用于市政名称及其邮政编码:链接

数据网站:cadastre.ch

关于最后一次流行投票的其他数据:直接链接,excel文件

我通过合并在CSV文件(wow.csv)(我要说明的数据)中添加了一列.该文件现在看起来像这样:

Gemeinden   code    Ja.Anteil   Ortschaft       PLZ Zusatzziffer    Kantonskürzel   E   N
1   Aadorf  4551    78.78638    Aawangen        8522    2           TG            710206    263564
2   Aadorf  4551    78.78638    Ettenhausen TG  8356    0           TG            710129    259411
3   Aadorf  4551    78.78638    Aadorf          8355    0           TG            710588    261648
4   Aadorf  4551    78.78638    Guntershausen   8357    0           TG            711741    258934
5   Aadorf  4551    78.78638    Wittenwil       9547    0           TG            712002    262572
Run Code Online (Sandbox Code Playgroud)

之后我试着按照@jlhoward的说明:

  1. 导入温度数据文件
  2. 导入市政的多边形shapefile
  3. muni多边形转换为数据框以进行绘图
  4. 从加入列 …

merge r polygon ggplot2

5
推荐指数
1
解决办法
3013
查看次数

R-在城市地图上拟合网格并将数据输入到网格正方形中

我试图像这样在圣何塞上放置网格:

圣何塞网格

您可以使用以下代码直观地制作网格:

  ca_cities = tigris::places(state = "CA") #using tigris package to get shape file of all CA cities

  sj = ca_cities[ca_cities$NAME == "San Jose",] #specifying to San Jose

  UTM_ZONE = "10" #the UTM zone for San Jose, will be used to convert the proj4string of sj into UTM

  main_sj = sj@polygons[[1]]@Polygons[[5]] #the portion of the shape file I focus on. This is the boundary of san jose

  #converting the main_sj polygon into a spatialpolygondataframe using the sp package
  tst_ps …
Run Code Online (Sandbox Code Playgroud)

r raster spatial geospatial r-sp

4
推荐指数
1
解决办法
879
查看次数

使用ggplot2绘制颜色编码的世界地图

我正在尝试生成生成世界地图的图,其中每个国家/地区的颜色对应于存储在数据框中的特定值.

> aggregated_country_data
   country num_responses                                     region
1       AL             1                                    Albania
2       AM             1                                    Armenia
3       AR            32                                  Argentina
...
75      ZW             3                                   Zimbabwe
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的

library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")

gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat), fill="white", colour="black", size=0.25)
gg
Run Code Online (Sandbox Code Playgroud)

这绘制了世界地图就好了,接下来我想为每个国家添加颜色,与aggregated_country_data中的'num_responses'值成比例

gg <- gg + geom_map(data=aggregated_country_data, map=map.world, aes(map_id=region, fill=num_responses), color="white", size=0.25)
gg
Run Code Online (Sandbox Code Playgroud)

但是现在它对每种颜色进行颜色编码,因为它们对应于国家/地区代码,而不是在aggregated_country_data中列num_responses中的值.

很明显,我没有得到关于ggplot2的东西,但我无法弄清楚那是什么.

我很感激任何投入,布拉德


我弄清楚问题是什么,它与ggplot2或我正在做的任何其他事情无关.aggregated_country_data数据框的"region"名称与map.world中的名称不同.我的输入数据(aggregated_country_data)默认使用两个字母的国家代码,我使用国家代码R包转换为国家名称(在数据框中称为"区域"),但它使用的名称的命名约定与map.world.所以这是一个完全不同的问题.

plot r ggplot2 rworldmap

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

r ×5

ggplot2 ×3

geospatial ×1

merge ×1

plot ×1

polygon ×1

r-faq ×1

r-sp ×1

raster ×1

rworldmap ×1

spatial ×1