R语言绘制城市天气热力图

R语言绘制城市天气热力图

本文是学习zonination/weather-intl的笔记,绘制了25个城市的温度热力图和温度湿度关系图。

首先准备好本次绘图所需的数据,是25个csv文件,下载压缩即可:
城市天气热力图.zip

整理数据

我没有采用作者的逐个合并的办法,而是直接做了个循环:

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
setwd("~/Desktop/城市天气热力图")
library(ggplot2)
library(scales)
library(lubridate)
library(RColorBrewer)
library(plyr)
library(gridExtra)
# 合并每个城市的数据集
weather <- read.csv("london.csv", header = T, stringsAsFactors = F)
filename <- c("beijing.csv",
"capetown.csv",
"delhi.csv",
"edmonton.csv",
"hongkong.csv",
"jakarta.csv",
"lagos.csv",
"lima.csv",
"mcmurdo.csv",
"mecca.csv",
"melbourne.csv",
"mexcity.csv",
"moscow.csv",
"nairobi.csv",
"novosibirsk.csv",
"nyc.csv",
"puntaarenas.csv",
"reykjavik.csv",
"rome.csv",
"saopaulo.csv",
"stockholm.csv",
"tehran.csv",
"tokyo.csv",
"yakutsk.csv")
for(i in filename){
weather <- rbind(read.csv(i, header = T, stringsAsFactors = F), weather)
}
rm(i)
rm(filename)

# 去除异常值
weather$Mean.TemperatureF <- as.numeric(weather$Mean.TemperatureF)
weather <- subset(weather, Mean.TemperatureF >= -99)

weather$Mean.TemperatureF <- as.numeric(weather$Mean.TemperatureF)
weather$Mean.Humidity <- as.numeric(weather$Mean.Humidity)
weather$CloudCover <- as.numeric(weather$CloudCover)
weather$PrecipitationIn <- as.numeric(weather$PrecipitationIn)
weather$Mean.Sea.Level.PressureIn <- as.numeric(weather$Mean.Sea.Level.PressureIn)
weather$Mean.Wind.SpeedMPH <- as.numeric(weather$Mean.Wind.SpeedMPH)

# 把温度转换成摄氏温度
weather$Mean.TemperatureF <- (weather$Mean.TemperatureF - 32) * 5 / 9

绘制温度热力图

1
2
3
4
5
6
7
8
9
10
11
12
13
theme_set(theme_bw(base_size = 15, base_family = "STSong") + theme(plot.title = element_text(hjust = 0.5)))
ggplot(subset(weather, !is.na(Mean.TemperatureF)), aes( x = as.Date(strftime(as.Date(Date), format = "%j"), "%j"), y = year(Date))) +
geom_tile(aes(fill = Mean.TemperatureF)) +
labs(title = "图:各个城市平均每天的温度",
x = "天",
y = "年") +
scale_x_date(labels = date_format("%b")) +
scale_y_reverse(breaks = seq(1996, 2015, 3),
limits = c(2015, 1996)) +
facet_wrap(~city, ncol = 5) +
scale_fill_gradientn(
colours = rev(brewer.pal(11, "Spectral")),
guide_legend(title = "温度(摄氏度)"))

这里面,%j格式的日期是从年初开始的天数,取值为001-366。%b格式的日期是缩写月份。

然后仔绘制一幅很炫酷的温度湿度关系图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
weather$season <- factor(weather$season, c("Spring","Summer","Autumn","Winter"))
awthr <- ddply(weather, c("city", "season"), summarise, Temp = mean(Mean.TemperatureF, na.rm = T), Humid = mean(Mean.Humidity))

ggplot(weather) +
geom_jitter(alpha = 0.1, size = 2, aes(x = Mean.TemperatureF, y = Mean.Humidity, color = season)) +
geom_density2d(alpha = 0.2, color = "black", aes(x = Mean.TemperatureF, y = Mean.Humidity)) +
geom_point(size = 4, data = awthr, aes(x = Temp, y = Humid, color = season)) +
geom_point(size = 4, pch = 1, data = awthr, aes(x = Temp, y = Humid, color = season)) +
geom_point(size = 8, pch = 3, data = awthr, color = "black", aes(x = Temp, y = Humid, color = season)) +
scale_color_manual(values = c(
"Summer" = "#FFBB00",
"Autumn" = "#EE4444",
"Spring" = "#11BB44",
"Winter" = "#4488fF"
), labels = c("春", "夏", "秋", "冬")) +
labs(title = "图:各个城市的一天",
x = "温度(摄氏度)",
y = "湿度(%)") +
facet_wrap(~city, ncol = 5) +
scale_x_continuous(breaks = seq(-60, 40, 10)) +
scale_y_continuous(breaks = seq(0, 100, 20))

# R

评论

Your browser is out-of-date!

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

×