Weather visualization in major cities of China

Source: Internet
Author: User

Remap is an R interface for interactive map visualization in Echarts, which allows you to visualize interactive maps directly using R code instead of writing JS code. This article will show you how to use the remap package to implement weather visualization for major cities nationwide.

1 Preparation 1.1 Installing R and Rstudio

See Basic part R

1.2 Installing Remap
library(devtools)install_github(‘lchiffon/REmap‘)

Please note that the above command by default you have installed the Devtools, if you do not install Devtools, then please install it first. # # 1.3 Prepare three pictures of the weather pictures can be self-baidu or design, or directly quoted on my GitHub pictures.

Image Link Address https://github.com/wcc3358/ChinaWeatherVisualization

The picture form is as follows:

Simplify the weather data with only three kinds of sunny, cloudy and cloudy days. 2 Loading Remap Packages
library(rvest) #用来爬取数据library(REmap) #实现地图可视化options(remap.js.web=T)

When you set options (remap.js.web=t), the generated HTML will be saved in the current working directory, otherwise it will be saved in the default Temp folder.

3 Crawling Weather data

Select the weather data for the Central Meteorological Observatory and use the Rvest package to crawl weather data:

## 爬取天气数据website <- "http://www.nmc.cn/publish/forecast/china.html"web <- read_html(website,encoding = "UTF-8") weather.data <- web %>% html_nodes("div.area ul li") %>% html_text()fun <- function(x){  temp  <- strsplit(x,split = "[\n]+")[[1]]    item <- gsub(pattern = "\\s+",replacement = "",x = temp)    res <- item[item!=""]  res}weather.data <- sapply(weather.data,fun)# 转置weather.data <- t(weather.data)# 重命名行和列weather.data <- as.data.frame(weather.data,row.names=1:nrow(dat0),stringsAsFactors=FALSE)colnames(weather.data) <- c("area","weather","temperature")

4 Data consolidation

Continue to collate data and generate derivative variables: WEATHERC

# 数据整理# 获取末尾的40个城市的记录weather.data <- tail(weather.data,40)# 提取天气中的前3个字儿weather <- substr(weather.data$weather,1,3)weather#  [1] "多云"   "多云转" "多云"   "阵雨转" "阴转小" "晴转多"#  [7] "晴"     "晴"     "晴"     "晴"     "晴转多" "多云"  # [13] "晴转小" "阵雨转" "大雨"   "大雨转" "大雨转" "大雨"  # [19] "大到暴" "小雨转" "晴"     "多云"   "阵雨"   "阴"    # [25] "晴"     "多云"   "晴"     "多云"   "多云"   "小雨转"# [31] "多云"   "阴转多" "中雨"   "小雨转" "阵雨转" "晴"    # [37] "多云转" "晴转多" "晴"     "阴转小"# 查出匹配字段的索引ind1 <- grep("晴",weather)ind20 <- grep("云",weather)ind21 <- grep("阴",weather)ind2 <- c(ind20,ind21)ind3 <- grep("雨",weather)# 将各匹配字段赋对应的英文天气weather.data$weatherc <- NAweather.data$weatherc[ind1] <- "sunny"weather.data$weatherc[ind2] <- "cloudy"weather.data$weatherc[ind3] <- "rainy"# 去掉了未能成功赋值的行weather.data <- weather.data[-19,]

After finishing the data as above, the last column WEATHERC represents the simplified weather type, that is, sunny, cloudy or rainy, where the rain turns cloudy, cloudy and sunny these weather conditions are directly simplified to rainy and cloudy, The simplification is convenient and corresponds to the three weather images represented in the preparation work 3.

The specific code does not do a detailed analysis, the middle involves regular matching and replacement, if you do not know the regular, to Baidu search 30 minutes to learn the regular expression, you can roughly understand.

I would like to specifically note that the author of this package to extract the latitude and longitude of the function Get_city_coord in the extraction of three words of the city when there is a bug, so the above code in the last use of code dat0[-c (1,6),] temporarily removed three words of city data. This part of the bug has been submitted to the author and I believe it will be repaired soon.

5 Visual display

Three steps to visualize your presentation:

A. Using Get_city_coord to obtain city latitude and longitude data

B. Picture and label information of the encapsulated city

C. Use of REMAPB to make City weather visualization display

remapB(center = c(104.114129,37.550339),       zoom = 5,       color = "Bright",       title = "",       subtitle = "",       markLineData = NA,       markPointData = NA,       markLineTheme = markLineControl(),       markPointTheme = markPointControl(),       geoData = NA)

REMAPB function Related parameter description:

    • Makepointdata: Information about the city, including pictures of the weather conditions to be displayed and text messages
    • GeoData: The latitude and longitude of city correspondence
    • Color: The theme color of the map, with "Bright", "Blue", "Light", "dark", "Redalert", "Googlelite", "Grassgreen", "Midnight", "Pink", " Darkgreen "," bluish "," grayscale "," Hardedge "optional, example see below-subtitle: subtitle
    • Makepointtheme: Control the size of the displayed picture, display the effect, etc.
    • Marklinedata: Marking the data of a line
    • Marklinetheme: The subject of the marker line
    • Zoom:bmap size scale zoom:5 country data zoom:15 City data

# Weather Visualization Show # remap.init () geoData <-sapply (Weather.data$area, Get_city_coord) geodata# Hong Kong Macau Taipei Beijing Xiqing # [1,] 114.17199 113.54940 121.52487 116.41355 117.01380# [2,] 22.28109 22.19296 25.04218 39.91101 39.14744# Tianjin Tai Formerly Shenyang Changchun Shanghai # [1,] 117.20591 112.55706 123.43897 125.33017 121.48024# [2,] 39.09091 37.87689 41.81134 43. 82178 31.23631# Nanjing Hangzhou Hefei Nanchang Jinan # [1,] 118.80289 120.16169 117.23545 115.86453 117.00132# [2,] 32.0647 3 30.28006 31.82687 28.68767 36.67163# Zhengzhou Wuhan Changsha Guangzhou Shenzhen # [1,] 113.63135 114.31183 112.94533 113.2707 9 114.06611# [2,] 34.75349 30.59843 28.23397 23.13531 22.54851# haikou Chongqing Chengdu Guiyang Kunming # [1,] 114.3       9999 110.20642 106.5572 104.07122 106.63682 102.83967# [2,] 15.91595 20.05006 29.5710 30.57628 26.65275 24.88595# Lhasa  Xi ' an Lanzhou xining Yinchuan Taipei # [1,] 91.12103 108.94631 103.84069 101.78427 106.23898 121.52487# [2,] 29.65009 34.34744 36.06731 36.62348 38.49239 25.04218citynames <-dimnames (geoData) [[2]]geodata <-As.data.frame (t (geoData), row.names = 1:nrow ( GeoData), stringsasfactors = FALSE) colnames (geoData) <-C ("Lon", "lat") geodata$city <-citynamessymbol <- Paste0 ("image://https://github.com/wcc3358/chinaweathervisualization/", Weather.data$weatherc, ". png") tooltip <-paste (A=weather.data$area, "----------", weather.data$weather,weather.data$temperature,sep= "<br>") NewData <-data.frame (a=weather.data$area,symbol=symbol,tooltip=tooltip,stringsasfactors=false) RemapB ( Markpointdata = NewData, GeoData = GeoData, color= "Blue", title = "Major City weather in China", Markpointtheme = Mar                                         Kpointcontrol (symbolsize=20, effecttype= ' bounce ', effect=t, color = "Random"))

Show results such as, this is just a screen, and in fact, this picture is dynamic, the weather icon will have a floating effect, and the mouse moved to the weather icon, there will be a text effect to show the specific weather and temperature data.

6 Summary

This article realizes the national main city weather visualization, this is only one application of the remap package, it can also be used to make the famous Baidu migration chart as well as the city heat-seeking and so on. The official documentation for the REMAP package is detailed in: Https://github.com/Lchiffon/REmap

This article link: http://www.cnblogs.com/homewch/p/5875148.html

GitHub Link: https://github.com/wcc3358/ChinaWeatherVisualization

Reference: http://mp.weixin.qq.com/s?__biz=MzI1NDMyMjgyMA==&mid=2247483793&idx=1&sn= A1126452e785422742952efadb0dd145&scene=4#wechat_redirect

Weather visualization in major cities of China

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.