R language and map of China

Source: Internet
Author: User
Tags ggplot

is a geo-distributed data map drawn by R language. More scientific, more rigorous, but also a more textured look.

Writing something today, I wonder what the meaning of the data analysis is, perhaps the form of studying the existence of things. And what is the form of the existence of things, from the original three-dimensional space, Albert Einstein took the time to pull in, so time as a change of space and exist, become the fourth dimension. Now it seems that the fifth space, may be a person's mental space or consciousness space, others say is curvature, and so on. Personally, I, all things should be connected to each other, there is no pure independent and other things insulated existence. In the human mind, people's thinking is indeed independent of the other four-dimensional space. So he should be a different kind of existence, but it should be in one of the other forms that can be unified with other dimensions. A wave? Or a finer particle? Or something that is even more overhanging and not far away.

Pull back data analysis, data analysis commonly used dimensions bar, time, space, and complex crowd distribution (people's various attributes, resulting in the distribution of various groups of people). While space, R language has some map methods. So stand on the shoulders of giants and draw a map today.

Basic Map Method:

1. World map: Some of the R packages contain data on common maps, such as maps, maps of the United States, maps of the U.S. counties, maps of the United States, map of the City of Canada, and maps of the Canadian cities, which can be easily and happily drawn. The MapData package contains data on Chinese maps, but it's old, and the data, Chongqing has not been separated from Sichuan.

Library (Maps) map ("world", fill = TRUE, col = Rainbow (+),    Ylim = C ( -60, a), Mar = C (0, 0, 0, 0)) title ("Map")

2. Map of China:

Pick up the Maptools,ggplot2,mapproj package and read it into the Chinese dimension map that the great God himself adapted to draw.

Library (Maptools)

x=readshapepoly (' bou2_4p.shp ')

Library (GGPLOT2)

Library (mapproj)

#可以看到中国地图的框框In order to further draw in the Ggplot2 package,you need to convert the Spatialpolygonsdataframe data type to a true data.frame type. the Ggplot2 package specifically provides a special version of the Fortify function for geographic data to do this workUse this function to cook the X,Geom_polygon is a function of the polygon fill path, and the map is actually a variety of combinations of polygons, so with this function, it is appropriate to draw a map. mymap=ggplot (data = fortify (x)) +geom_polygon (Aes (X=LONG,Y=LAT,GROUP=ID), colour= "Black", Fill=na) +theme_grey ()

print (Mymap+coord_map ())here, you can see a hand-drawn map of China. The second step, I would like to put the data, the whole to this map,ch = fortify (x,region = ' NAME ')#拿出各省的编码数据, converting from "GBK" formatch = transform (ch,id=iconv (id,from = ' GBK '), group = Iconv (Group, from = ' GBK '))names (CH) [1:2] = C ("x", "Y")#省份去重且排序Mychdat = data.frame (id = unique (sort (ch$id)))t<-read.table ("First purchase user distribution. csv", header=true,sep= ",")#数据拼接mychdat$users <-t$usersMychdattmp= coordinates (x)
TMP = AS.DATA.FRAME (TMP)tmp$names = X$name#画图, with tags. Oh, the road is blind.Chmap = Ggplot (mychdat) + Geom_map (AES (map_id = ID, fill = users), color = "white", map = CH) +geom_text (AES (x = V1,y = V2,label = names), family = "GB1", data = tmp) + scale_fill_gradient (high = "Darkblue", low = "lightblue") + expand_limits (CH) + coord_map ()print (CHMAP)Note that family= "GB1" is a fontAll right, let's get here today.

R language and map of China

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.