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 does ggplot2 fill polygons with color signals?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how ggplot2 fills polygons through color signals". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how ggplot2 fills polygons through color signals.

Of all the layer objects in ggplot2, geom_ploygon () geometric layer objects are the most complex and special:

What's the complication?

The polygon defined by this kind of geometric object (especially in the geographic information data), the territorial boundary is based on the administrative division, which is subdivided into a single polygon (that is, group), and the single polygon is composed of a set of longitude and latitude coordinate points (sorted by order).

So the parameters to be explicitly declared by geom_ploygon () need at least four:

Data (Geographic Information data frame)

Long (Longitude abbreviation)

Lat (Dimension abbreviation)

Group (Polygon grouping variable)

In other words, the layer needs at least these four parameters to ensure that a map with complete geographic information boundaries can be output.

Note that the complete geographic information boundary I am talking about here does not have a table administrative boundary, and the geographic information boundary only describes the outline of the geographical units that actually exist on the earth, so this picture is only a description of the geographical outline. you can't see the national boundaries.

So what is the definition of national boundaries?

National lines usually need to apply id on the basis of group (the id classifies different group belonging to the same country into a number), of course, we can also match the id to the actual name of the country (administrative division) (usually the data map material obtained will match both the id and administrative division names, and even a lot of additional information: ios coding, abbreviations, pinyin, English names, and so on).

So the question is, why isn't the order explicitly declared in the geom_polygon () parameter?

Boundary points do not declare the order, how does the software know in what order to print?

This question is a good question, to the point, indeed, order variables are very important, but usually in the geographic information files obtained, order variables have been sorted according to group grouping variables, that is, in the commonly used geographic information data frame, all the boundary points longitude and latitude information are first grouped according to group, and sorted within the group according to order, so as to ensure that the final drawn geographic information boundary points will not be confused. Different polygons have connecting lines and other situations that we don't want to see.

When this kind of group and order order definition, the software will first print all the latitude and longitude coordinates according to the group order, that is, the first print group order of polygons, group internal in accordance with the order of order, print the left point, a single group, but after the end, this group of points is defined as a group, group identification is the name of group (can be numeric, floating-point or text, mainly can be distinguished from the group).

After all the latitude and longitude points in the group are printed according to this rule, we can see the outline of the entire geographic information boundary.

At this time, the definition of geographic information outline is complete, then color mapping needs to be carried out. The color mapping rules are very simple. Different countries (or administrative divisions) correspond to an ID or division name, and each division name (or country, here collectively referred to as id) corresponds to several group (all of which are several, because the correspondence here may be one-to-one correspondence, or one-to-many relationship. Some countries or administrative regions have only one outline, while some countries or regions have multiple geographically separated territories, such as Ireland, the United Kingdom, China's Hainan Island, Taiwan Province, and many islands along the southeast coast, etc.).

Each id (country or region) corresponds to a numerical (or factor variable). When you assign an indicator variable to ID, you have completed the mapping from group to color.

All we need is a fill / colour aesthetic mapping attribute assigned to an indicator variable (numeric or factorial), and after that, the software will print each geographic polygon colleague and assign a fill color (or outline color) to the polygon. Because there may be a situation where an id corresponds to multiple polygons (group), these polygons that belong to the same id will all be filled (or outline colors) of the same order of magnitude.

So at this point, let's look back at the above:

A complete syntax for the layer integrity of geom_polygon () with mapping between administrative divisions and indicator variables needs to specify one of the following parameters:

Gggplot (data=mydata) +

Geom_polygon (aes (xylene long.m.lt. Lad. Group.m.fillplink.))

The above is the most typical data mapping practice, the color mapping results of fill/colour will be adjusted according to the nature of zhibiao variables, that is, if zhibiao is a continuous variable, then it will eventually be filled in accordance with continuous gradients, legends are also legends for practicing gradients, and indicators are classified or factor types, then they will be filled according to discrete gradients. The specific rules and principles of these two color mapping methods will be discussed later.

Not considering order does not mean that order is not important, in fact, order is a very important variable, but it has been considered in advance (because it is too important), so we need to reconsider it, but if the data order is disrupted for some reason, then we need to reorder ourselves.

Usually all you need is an additional line of code:

Dplyr::arrange (mymapdata,group,order)

That is, it is grouped according to group and sorted according to order within the group, which ensures that the final data is in accordance with the rules of geometric layer mapping and can be mapped correctly.

Next, I'll use a few graphics to show all the principles mentioned above:

Library (maps)

Library ("ggplot2")

Library ("ggthemes")

Library ("ggthemes")

Library ("RColorBrewer")

World_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