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

Mathematical modeling skills how to connect on a map without for loop and using ggplot2

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you the skills of digital modeling without for loop and how to use ggplot2 to connect on the map. I hope you will get something after reading this article. Let's discuss it together.

1. Review the previous article

First of all, we can review how we used ggplot2 to make a better-looking map of China without ladders: using R to make a beautiful map of China (no need to get it through google).

Next, based on the previous map of China, according to the calculation (the minimum spanning tree algorithm, there is time to talk about this later, but now it is said in great detail on the Internet, and there are a lot of codes), the corresponding links are posted on our map.

Background image

two。 Using ggplot2 to connect multiple points

We need to select some of these points and connect them. Here's what we want to achieve in the end (the previous picture was painted with Mac, and the following is Windows, which may be a little different in texture, I hope you can understand it):

In order to achieve this effect, we mainly need to organize the dataset in a way that can be drawn comfortably. Using ggplot2 drawing, collating datasets takes up more than 80% of the work.

1) existing data

Our ultimate goal is to connect 33 lines, so we first list the lines that need to be connected in groups (each city corresponds to a set of latitudes and longitudes, corresponding to a point on the map).

(the previous final effect diagram is the first 11 groups of city connections)

The following data is named city_pair.

City1 city21 Beijing & Tianjin Shanghai 2 Shanghai Guangzhou & Shenzhen 3 Guangzhou & Shenzhen Chongqing 4 Chongqing Chengdu 5 Chongqing Xi'an 6 Beijing & Tianjin Harbin 7 Beijing & Tianjin Wuhan 8 Wuhan Zhengzhou 9 Chongqing Kunming 10 Beijing & Tianjin Urumqi 11 North Beijing & Tianjin Lhasa 12 Zhengzhou Xi'an 13 Wuhan Chongqing 14 Beijing & Tianjin Zhengzhou 15 Beijing & Tianjin Xi'an 16 Zhengzhou Chongqing 17 Beijing & Tianjin Chongqing 18 Wuhan Guangzhou & Shenzhen 19 Shanghai Wuhan 20 Shanghai Zhengzhou 21 Beijing & Tianjin Guangzhou & Shenzhen Shenzhen 22 Shanghai Chongqing 23 Kunming Guangzhou & Shenzhen 24 Wuhan Chengdu 25 Zhengzhou Chengdu 26 Xi'an Chengdu 27 Beijing & Tianjin Chengdu 28 Chengdu Kunming 29 Xi'an Wuhan 30 Chengdu Guangzhou & Shenzhen 31 Shanghai Chengdu 32 Harbin Chongqing 33 Harbin Guangzhou & Shenzhen

So we also need to correspond to the longitude, latitude and population information of the city. The object here is mat.cities, which looks like this: (the data generation of this part is also mentioned in our previous blog.)

Names lat long population1 Beijing & Tianjin 39.90420 116.40740 32.55062 Shanghai 31.23039 121.47370 23.01913 Zhengzhou 34.74725 113.62493 8.62654 Urumqi 43.82660 87.61684 3.11035 Harbin 45.80218 126.53582 10.63606 Xi'an 34.34126 108.93982 8.46787 Wuhan 30.59276 114.30524 9.78548 Chengdu 30.57022 104.06477 14.04769 Lhasa 29.64411 91.11445 0.559410 Chongqing 29.56470 106.55071 28.846211 Kunming 24.87966 102.83321 6.43 2012 Guangzhou & Shenzhen 23.02067 113.75178 23.0587

With these two raw data, we can start drawing.

First of all, the data need to be sorted out to be able to draw, in order to facilitate the operation, we directly operate on the original 33 groups of city pairs. The following is the process of data preprocessing. (because the digital simulation time is limited and the amount of data is not very large at that time, a large number of for loops are used below. I hope everyone will use more vectorization operations and less for loops when using R)

2) data preprocessing

The preprocessing code is as follows:

Dat_plot = matrix (nrow = 66, ncol = 4) k = 0for (I in 1:33) {for (j in 1:2) {k = k + 1 my.row = mat.cities$names [city _ pair [I, j] = mat.cities$names,] dat_plot [k, 1] = unlist (my.row [1]) dat_plot [k, 2] = unlist (my.row [2]) dat_ plot [k 3] = unlist (my.row [3]) dat_plot [k, 4] = I}} colnames (dat_plot) = c ('lat',' long', 'group') dat_plot = as.data.frame (dat_plot) dat_plot$lat = as.numeric (as.character (dat_plot$lat)) dat_plot$long = as.numeric (as.character (dat_plot$long))

Our main idea here is to split a paired group of cities into two groups, and then add a group variable at the end, mainly for connection (if two cities are in the same group, use the parameter group in the ggplot drawing to connect the two points).

After generating the desired dataset, remember to adjust the latitude and longitude to numerical values, and group can be directly factor.

Finally, we get the data dat_plot that looks like this:

Regional lat long group1 Beijing & Tianjin 39.90420 116.40740 12 Shanghai 31.23039 121.47370 13 Shanghai 31.23039 121.47370 24 Guangzhou & Shenzhen 23.02067 113.75178 25 Guangzhou & Shenzhen 23.02067 113.75178 36 Chongqing 29.56470 106.55071 37 Chongqing 29.56470 48 Chengdu 30.57022 104 . 06477 49 Chongqing 29.56470 106.55071 510 Xi'an 34.34126 108.93982 511Beijing & Tianjin 39.90420 116.40740 612 Harbin 45.80218 126.53582 Beijing & Tianjin 39.90420 116.40740 714 Wuhan 30.59276 114.30524 715 Wuhan 30.59276 114.30524 816 Zhengzhou 34.74725 113.62493 817 Chongqing 29.56470 106.55071 Kunming 24.87966 102.83321 Beijing & Tianjin 39.90420 116.40740 1020 Urumqi 43.82660 87.61684 1021 Beijing & Tianjin 39.90420 116.40740 1122 Lhasa 29.64411 91.11445 1123 Zhengzhou 34.74725 113.62493 1224 Xi'an 34.34126 108.93982 1225 Wuhan 30.59276 114.30524 1326 weight Qing 29.56470 106.55071 1327 Beijing & Tianjin 39.90420 116.40740 1428 Zhengzhou 34.74725 113.62493 1429 Beijing & Tianjin 39.90420 116.40740 1530 Xi'an 34.34126 108.93982 1531 Zhengzhou 34.74725 113.62493 Chongqing 29.56470 106.55071 1 633 Beijing & Tianjin 39.90420 116.40740 1734 Chongqing 29.56470 106.55071 1735 Wuhan 30. 59276 114.30524 1836 Guangzhou & Shenzhen 23.02067 113.75178 1837 Shanghai 31.23039 121.47370 1938 Wuhan 30.59276 114.30524 Shanghai 31.23039 121.47370 Zhengzhou 34.74725 113.62493 Beijing & Tianjin 39.90420 116.40740 2142 Guangzhou & Shenzhen 23.02067 113.75178 2143 Shanghai 31.23039 121.47370 2244 Chongqing 29.56470 106. 55071 2245 Kunming 24.87966 102.83321 2346 Guangzhou & Shenzhen 23.02067 113.75178 2347 Wuhan 30.59276 114.30524 2448 Chengdu 30.57022 104.06477 2449 Zhengzhou 34.74725 113.62493 2550 Chengdu 30.57022 104.06477 2551 Xi'an 34.34126 108.93982 2652 Chengdu 30.57022 104.06477 2653 Beijing & Tianjin 39.90420 116.40740 Chengdu 30.57022 104.06477 2755 Chengdu 30.57022 104.06477 2856 Kunming 24.87966 102.83321 2857 Xi'an 34.34126 108.93982 2958 Wuhan 30.59276 114.30524 Chengdu 30.57022 104.06477 Guangzhou & Shenzhen 23.02067 113.75178 3061 Shanghai 31.23039 121.47370 3162 Chengdu 30.57022 104.06477 3163ha Erbin 45.80218 126.53582 3264 Chongqing 29.56470 106.55071 3265 Harbin 45.80218 126.53582 3366 Guangzhou & Shenzhen 23.02067 113.75178 333) drawing

Below our core drawing code is as follows. To connect different lines, we just change the selected rows in the data, such as: dat_plot [1:22].

# # Line 11 ggplot () + geom_path (data = china, aes (long, lat, group = group), color ='# FD9FA4', show.legend = F) + geom_point (data = mat.cities, aes (x = long, y = lat, size = population), alpha = 0.8, color ='# 8BB6D6') + geom_line (data = dat_plot [1:22,], aes (long, lat, group = group), size = 1, alpha = 0.8 Color = '8BB6D6') + geom_text_repel (data = mat.cities, aes (x = long, y = lat, label = names), family = "STHeiti") + labs (x =' longitude', y = 'latitude', title = 'eleven lines', size = 'population (millions)') + theme_bw + theme (panel.border = element_blank (), text = element_text (family = "STHeiti") Plot.title = element_text (hjust = 0.5) # # 16 lines ggplot () + geom_path (data = china, aes (long, lat, group = group), color ='# FD9FA4', show.legend = F) + geom_point (data = mat.cities, aes (x = long, y = lat, size = population), alpha = 0.8, color ='# 8BB6D6') + geom_line (data = dat_plot [1:32,], aes (long, lat, group = group), size = 1 Alpha = 0.8, color ='# 8BB6D6') + geom_text_repel (data = mat.cities, aes (x = long, y = lat, label = names), family = "STHeiti") + labs (x = 'longitude', y = 'latitude', title = 'sixteen lines', size = 'population (millions)') + theme_bw () + theme (panel.border = element_blank (), text = element_text (family = "STHeiti") Plot.title = element_text (hjust = 0.5) # # 33 line ggplot () + geom_path (data = china, aes (long, lat, group = group), color ='# FD9FA4', show.legend = F) + geom_point (data = mat.cities, aes (x = long, y = lat, size = population), alpha = 0.8, color ='# 8BB6D6') + geom_line (data = dat_plot [1:66,], aes (long, lat, group = group), size = 1 Alpha = 0.8, color ='# 8BB6D6') + geom_text_repel (data = mat.cities, aes (x = long, y = lat, label = names), family = "STHeiti") + labs (x = 'longitude', y = 'latitude', title ='33 lines', size = 'population (millions)') + theme_bw () + theme (panel.border = element_blank (), text = element_text (family = "STHeiti") Plot.title = element_text (hjust = 0.5)

There is nothing to say about the drawing process, and the functions and methods used in the drawing process have been mentioned in the previous blog:

R language learning ggplot2 drawing statistical graphics package in detail

The only function used for the added connection: geom_line, which only needs to pay attention to the extra parameter group, and remember to add it.

4) result display

The effects of the last 16 lines and 33 lines are shown below:

16 connections:

33 connections:

After reading this article, I believe you have a certain understanding of "Digital and Analog skills without for cycle and how to connect on a map using ggplot2". If you want to know more about it, welcome to follow the industry information channel. Thank you for your reading!

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

Development

Wechat

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

12
Report