Exploring the Wechat Friends Data Again!

Exploring the Wechat Friends Data Again!

About one week ago, I added a new functions to hchinamap package, that is chinamappoint() and provincepoint(). these two functions can map points on chinese map and it province map. The tweets indtroduce a method to display the geographical distribution of your wechat friends on chinese map, and also some other methods to plot wechat friends’s geographical distribution.

First, use itchat, a python module, to get wechat friends data:

getfriends.py
1
2
3
4
5
6
7
def getfriends():
import itchat
import pandas as pd
itchat.auto_login(hotReload = True)
friends = itchat.get_friends(update = True)
friends = pd.DataFrame(friends)
return(friends)

Next, source it by reticulate package, a R package, provided a interface between R and Python.

tXvh2UTmo4z0zq5t41XLPTm1kQbMHjYl is my baidu map api key, it’s limited, so please do not abuse it.

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
library(reticulate)
use_python("/opt/anaconda3/bin/python")
source_python("getfriends.py")
df <- getfriends()

library(tidyverse)
library(tibble)
library(ggmap)
library(baidumap)
library(progress)
options(baidumap.key = 'tXvh2UTmo4z0zq5t41XLPTm1kQbMHjYl')

wxdf <- df %>%
as_tibble() %>%
select(NickName, Province, City, Sex, Signature)

wxdf$lon = NA
wxdf$lat = NA
pb <- progress_bar$new(total = nrow(wxdf))
for(i in 1:nrow(wxdf)){
pb$tick()
tempv <- getCoordinate(paste0(wxdf$Province[i], wxdf$City[i]), formatted = T)
wxdf$lon[i] = tempv['longtitude']
wxdf$lat[i] = tempv['latitude']
}

library(magrittr)

wxdf %<>%
mutate(
gender = case_when(
Sex == 0 ~ "Unknown",
Sex == 1 ~ "Boy",
Sex == 2 ~ "Girl"
)
) %>%
dplyr::filter(!is.na(lon))

# add some noise to avoid overlapping
for (i in 1:nrow(wxdf)) {
wxdf$lat[i] = wxdf$lat[i] + runif(1)
wxdf$lon[i] = wxdf$lon[i] + runif(1)
}
hchinamap::chinamappoint(cat = wxdf$gender, name = wxdf$NickName, lat = wxdf$lat, lon = wxdf$lon, title = "The Geographical Distribution of My Wechat Friends", subtitle = "https://czxa.top", itermName = "count")

The provincepoint() can be used to mark points on province maps, for example, Guangdong province map:

R
1
2
3
4
5
6
7
8
9
hchinamap::provincepoint(
region = "guangdong",
cat = gddf$gender,
name = gddf$NickName,
lat = gddf$lat,
lon = gddf$lon,
title = "The Geographical Distribution of My Wechat Friends in Guangdong",
subtitle = "https://czxa.top",
itermName = "count")

Also, hchinamap package can be use to plot filled map of China:

R
1
2
3
4
provincedf <- wxdf %>% 
count(Province)

hchinamap::hchinamap(name = provincedf$Province, value = provincedf$n, theme = "sandsignika", title = "The Geographical Distribution of My Wechat Friends", subtitle = "https://czxa.top", itermName = "count")

The leafletCN package provides a simple method to plot interactive Chinese maps:

R
1
2
3
4
5
6
7
8
library(leafletCN)
provincedf <- as.data.frame(provincedf)
geojsonMap(provincedf, "china",
namevar = ~ Province,
valuevar = ~ n,
palette = "YlOrRd",
legendTitle = "count",
popup = paste0("count: ", provincedf$n))

R
1
2
3
4
5
6
7
8
9
countydf <- wxdf %>% 
count(City) %>%
as.data.frame()
geojsonMap(countydf, "city",
namevar = ~ City,
valuevar = ~ n,
palette = "YlOrRd",
legendTitle = "count",
popup = paste0(countydf$City, "count: ", countydf$n))

You can also add markers:

wechat.svg

R
1
2
3
4
5
6
7
8
9
10
11
12
wxicon <- makeIcon(
iconUrl = 'wechat.svg',
iconWidth = 50, iconHeight = 50
)

library(leaflet)
leaflet() %>%
amap() %>%
addMarkers(lng = wxdf$lon,
lat = wxdf$lat,
popup = wxdf$NickName,
icon = wxicon)

The baidumap also can be used to plot Chinese map:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
library(baidumap)
library(ggmap)
library(tidyquant)
gd <- getCoordinate('广州', formatted = T)
p1 <- getBaiduMap(c(gd['longtitude'], gd['latitude']),
zoom = 10, width = 900, height = 900)
map1 <- ggmap(p1) +
geom_point(data = wxdf, aes(x = lon, y = lat), size = 4, color = palette_light()[2], shape = 18) +
guides(color = "none") +
theme_ipsum_rc() +
labs(title = "Guangdong Province") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = grid::unit(c(2.5, 0.5, 0.5, 1), "cm"))

sh <- getCoordinate('上海', formatted = T)
p2 <- getBaiduMap(c(sh['longtitude'], sh['latitude']),
zoom = 10, width = 900, height = 900)
map2 <- ggmap(p2) +
geom_point(data = wxdf, aes(x = lon, y = lat), size = 4, color = palette_light()[3], shape = 18) +
guides(color = "none") +
theme_ipsum_rc() +
labs(title = "Shanghai Municipality") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = grid::unit(c(2.5, 1, 0.5, 0.5), "cm"))

fy <- getCoordinate('阜阳', formatted = T)
p3 <- getBaiduMap(c(fy['longtitude'], fy['latitude']),
zoom = 8, width = 900, height = 900)
map3 <- ggmap(p3) +
geom_point(data = wxdf, aes(x = lon, y = lat), size = 4, color = palette_light()[8], shape = 18) +
guides(color = "none") +
scale_colour_ipsum() +
theme_ipsum_rc() +
labs(title = "Anhui Province") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = grid::unit(c(0.5, 0.5, 1, 1), "cm"))

jn <- getCoordinate('济南', formatted = T)
p4 <- getBaiduMap(c(jn['longtitude'], jn['latitude']),
zoom = 8, width = 900, height = 900)
map4 <- ggmap(p4) +
geom_point(data = wxdf, aes(x = lon, y = lat), size = 4, color = palette_light()[10], shape = 18) +
guides(color = "none") +
scale_colour_ipsum() +
theme_ipsum_rc() +
labs(title = "Shandong Province") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = grid::unit(c(0.5, 1, 1, 0.5), "cm"))

library(cowplot)
plot_grid(map1, map2, map3, map4, ncol = 2, rel_heights = c(2, 1.7)) +
draw_label("My Wechat Friends' Geographical Distribution",
x = 0.4, y = 0.95,
fontfamily = "Roboto Condensed", size = 20, fontface = "bold") +
draw_label("https://czxa.top",
x = 0.2, y = 0.9,
fontfamily = "Roboto Condensed", size = 16)

It doesn’t look right!

# R

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×