Code Monkey home page Code Monkey logo

ggplot2-china-map's Introduction

China Map with ggplot2

This project demonstrates how to use ggplot2 to plot a non-built-in map using the shapefile.

Plot the map

The plot process is fairly straightforward. We can load the shapefile (in china_shapefile) using readOGR(). Then we fortify it to a normal dataframe. We can modify the dataframe if needed. For this project I ignored islands in the South China Sea, since there are no students on the list from there. Next, we use geom_polygon layer to plot the map, and add coord_map to make it looks pretty. Finally we can add other standard data layer onto the map, using the geometrical coordinates as aesthetic mapping.

The process is done in plot_china.r.

Data Processing

For this specific project, data are parsed from the UW-Madison’s Dean’s List of 2017 Spring. My data include all students from mainland China, Hong Kong and Taiwan. The cleaned file is stored as deanlist_2017_fall.csv, and the cleaning process is in data_cleaning.ipynb.

China city coordinates

To plot the cities on the map, I parsed a simple JSON file of Chinese city coordinates.

This JSON file includes over 300 major cities in China. The main key is the English name of one, and value is another dictionary containing its Chinese name and coordinates (latitude, longitude).

{
	"kelamayi": {
		"coord": [
			45.5943,
			84.8812
		],
		"hanzi": "克拉玛依市"
	},
	"kezileisu": {
		"coord": [
			39.7503,
			76.1376
		],
		"hanzi": "克孜勒苏柯尔克孜自治州"
	},
	"kunming": {
		"coord": [
			25.0492,
			102.7146
		],
		"hanzi": "昆明市"
	}
}

To make the JSON file more generic, I also added some alias of keys. For example, xianggang and hongkong, xian and xi'an, kelamayi and karamay, etc.

ggplot2-china-map's People

Contributors

xiaohk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.