A ramble on the R language map

Source: Internet
Author: User

Original address: Click to open the link

The R language has a commendable ability to visualize, and in this article, we try to use the R language to show map data, which is to draw a map and show the distribution of the data on the map.

Because the Chinese map of the R language is too old, we are looking for an external map data file and loading and displaying the map in R.

The map data file we use is a shapefile-formatted file that stores the geometric location and attribute information of geographic features that can be represented by points, lines, and polygons in a shapefile. A complete shape file consists of a set of files, where necessary basic files include a coordinate file (. shp), an index file (. shx), a property file (. dbf), three files, and sometimes a feature space index file (. SBN and. sbx). Map data File download address) (Http://cos.name/wpcontent/uploads/2009/07/chinaprovinceborderdata_tar_ gz.zip), download decompression, you can see there are three files, respectively, is bou2_4p.dbf,dou2_4p.shp,bou2_4p.shx, the number 1~4 respectively represents the state, province, city, county, 4 administrative division, the letter P end of the expression polygon data, used to draw the area, BOU2 is the boundary of Provincial administrative division. This data is not the latest data, the latest data in the National Basic Geographic Information Center can not be found, probably to buy to get, but for the general drawing, this data has been able to meet the requirements.

This article draws a map with 3 packages: Sp,maptools,ggplot2. The SP package mainly provides some of the parties that manipulate the geographic information data. The Maptools package provides some tools for reading and manipulating geographic information data. GGPLOT2 provides the ability to visualize data. See the documentation that came with each package for specific features.

> Library (SP)

>library (Maptools) #windows系统加载时都会提示以下信息, little effect on drawing.

Checking Rgeos Availability:false

Note:when Rgeos is not available,polygon geometry computationsin maptools depend on Gpclib,

which has a restricted licence. It isdisabled by default;

To enable Gpclib, Typegpclibpermit ()

> Library (GGPLOT2)

Map data can be divided into points, lines, polygons three kinds of data, in the Maptools package has corresponding functions to read (Readshapepoints, Readshapelines and Readshapepoly), here we use the Readshapepoly function.

> x <-readshapepoly (File.choose ()) #读入bou2_4p. shp file

> class (x) #x是sp包的数据框

[1] "Spatialpolygonsdataframe"

attr (, "package")

[1] "SP"

> Names (x)

[1] "area" "PERIMETER" "Bou2_4m_" "bou2_4m_id" "ADCODE93"

[6] "ADCODE99" "NAME"

We found that X contains 7 types of information, where area is the region, Perimeier is perimeter, and name is Chinese. The others are some encodings. We use Ggplot2 for map drawing, Ggplot2 to draw the object format as a data frame, and X in the format of the geographic information class data frame (class (x)), we convert its format to meet the Ggplot2 drawing requirements.

> <-fortify (x) #将地理信息文件转换为data. Frame Format Data

Regions defined for each polygons

> Head (China)

Long lat order hole piece Group ID

1 121.4884 53.33265 1 FALSE 1 0.1 0

2 121.4995 53.33601 2 FALSE 1 0.1 0

3 121.5184 53.33919 3 FALSE 1 0.1 0

4 121.5391 53.34172 4 FALSE 1 0.1 0

5 121.5738 53.34818 5 FALSE 1 0.1 0

6 121.5840 53.34964 6 FALSE 1 0.1 0

The converted China data contains 7 columns of data, long indicates precision, and lat represents latitude.

Now we can draw the original map.

> P <-ggplot (China,aes (X=long,y=lat)) +geom_polygon (Aes (GROUP=ID), fill=i ("Red"))

The shape of the map is somewhat flat, because in the process of drawing, the longitude and latitude are treated as normal coordinates evenly and equally, and plotted in Cartesian coordinate system. And the map should be drawn in the professional geographic map, which is reflected in the Coord_map () function in Ggplot2, details please? Coor_map (), it has different projection mode. Also, the tick and coordinate lines in the graph can be removed, showing only the map.

> P1 <-p+theme (Panel.background=element_rect (fill= "LightBlue")) +coord_map ()

> P2 <-p1+theme (Panel.grid=element_blank ())

> P3 <-p2+theme (Axis.title=element_blank ())

> P4 <-p3+theme (Axis.ticks=element_blank ())

> P5 <-p4+theme (Axis.text=element_blank ())

> P6 <-P5 +theme (Plot.background=element_rect (fill= "LightBlue")) +theme (Legend.background=element_rect (fill= "LightBlue"))

> P6

Finally, add a headline.

>p6+ggtitle ("Map of China")



Resources

1.Shapefile Format Description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.