首先利用python爬取天氣數(shù)據(jù)
預(yù)處理
在導(dǎo)入數(shù)據(jù)到 R 之前运敢,先用 excel 在數(shù)據(jù)第一列前加一列 ID(可為連續(xù)的數(shù)字),作為 rownames。
R
# 線圖繪制
setwd("D:/R learning")
library(ggplot2)
library(reshape2)
data = read.csv("i.csv", header = T, row.names = 1, sep = ",")
plot(rownames(data), data$最高氣溫..., type = "l")
ggplot 畫圖
ggplot(data, aes(x = ID, y = 最高氣溫...)) +
geom_point()
data2 = data[1:20, ]
data2$ID = factor(data2$ID, levels = data$ID)
# 不加 group = 1 會報(bào)錯
ggplot(data2, aes(x = ID, y = 最高氣溫..., group = 1)) +
geom_line()
局部擬合
# 局部擬合
ggplot(data2, aes(x = ID, y = 最高氣溫..., group = 1, color = "最高溫")) +
geom_line(size = 2) +
stat_smooth(method = "auto", se = FALSE, ) +
theme(legend.position = c(0.1,0.9))
多線圖
# 多線圖
data3 = data2[,1:3]
data_long = melt(data3, id.vars = "日期")
ggplot(data_long, aes(x = 日期, y = value, color = variable, group = variable)) +
geom_line(size = 1) +
stat_smooth(method = "auto", se = FALSE, ) +
theme(legend.position = c(0.85,0.16)) +
theme(axis.text.x = element_text(angle = 50, hjust = 1, vjust = 1, family = "serif")) +
theme(axis.title = element_text(family = "serif", size = 18)) +
theme(legend.text = element_text(family = "serif"))