Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the data map material types supported by ggplot in R language visualization

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to understand the data map material types supported by ggplot in R language visualization. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

After making so many data maps, it's time to summarize some ideas and ideas and discuss the data map material formats supported by ggplot2 today.

Library ("plyr")

Library ("dplyr")

Library ("rgdal")

Library ("sf")

Library (maptools)

Library ("ggplot2")

Library ("ggthemes")

Library ("geojsonio")

Options (stringsAsFactors=FALSE,warn=FALSE,encoding= "UTF-8")

Today, I would like to share with you about the source data support for making data maps in ggplot2 system. I have always felt that this issue is very important.

In fact, after sharing so many issues of data maps, I have used nothing more than the following three kinds of data maps:

Traditional shp material

Json material

The map package contains map material.

Because the format of the third material is consistent with the format of the shp spatial dataset we imported, here we focus on the first two data sources.

Although we can be divided into shp material and json material in terms of data storage format, due to the use of ggplot2 in R language, the supported dataset objects can be roughly divided into two categories, all of which can be transformed from shp and json data files.

Sp:SpatialPolygonDataFrame

Sf:Simple feature list column

So the relationship between the data file format and the spatial dataset object format can be expressed as follows:

The information described by the datasets in the two formats is almost the same. The first format (sp) is the more traditional data format of R language drawing, which divides the geographic information data into two blocks: description layer and mapping layer.

When the data is stored, the description layer records the name, ID, number, abbreviation, iOS code, and other identification information and measurement variables of each geographical region. The description layer is a dataframe, and we can use data@data to extract the data box of the description layer.

The corresponding geometric mapping layer is the polygonal boundary points of each administrative region, which are sorted according to order and grouped according to group. The polygon boundary point information is a multi-layer nested list structure, but we can still convert it into a data box through the fortity function.

That is, sp spatial data object is a combination of dataframe (description layer) and polygons (geometric mapping layer).

On the other hand, the sf object makes a more orderly layout of this control data format, and the spatial data object imported by st_read () is completely a neat data box with neat rows and columns that contain data descriptions and boundary point information of geometric polygons. The biggest feature is that it encapsulates the geometric boundary points corresponding to each administrative division into a record of a list object, which, like other ordinary text records and numerical records, is arranged in the cells described by the corresponding administrative divisions.

The advantage of this is that we do not need to link the correspondence from the description layer to the geometric mapping layer, because the correspondence already exists. Then if it is the first sp format, in the process of making a ggplot2 map, we need to separate the description layer and the geometric mapping layer, and specify a connected id (primary key) for both. If you take into account the action that you want to merge your own business data and description layer data, then we need to merge the data twice in total. If the description layer does not have a corresponding id, you need to construct a virtual id for it, and this time you need to merge three times.

However, in the sf object, we only need to specify a merge, that is, the merge of the description layer and business metric data.

Next, a case demonstration is used to explain the above principles:

Usually we make a data map in the following way:

Shapefile file import:

Setwd ("D:/R/rstudy/CHN_adm")

China_map

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report