knitr::opts_chunk$set(warning = FALSE, comment = NA)
library(knitr)
一、数据读取
数据来自uci,葡萄牙森林火灾数据
forestfires <- read.table(file = "C:/Users/91333/Documents/semiester5/R course/hw3/forestfires.csv", header = T, sep = ",")
二、绘制条形图
1.数据准备
1)生成一列新数据iffire
根据area是否大于0生成新的一列数:是否着火
forestfires$iffire <- factor(ifelse(forestfires$area > 0, 1, 0), labels = c("unfired", "fired"))
2)给因子类型的变量添加level
添加level后,绘图会默认以level排序,比较方便。
forestfires$month <- factor(forestfires$month, levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
forestfires$day <- factor(forestfires$day, levels = c("mon","tue","wed","thu","fri","sat","sun"))
3)生成一列表示季节的变量
葡萄牙位于北半球,虽然是较为特殊的地中海气候但不影响季节划分,因为季节是由太阳公转和南北半球决定,即3-5月春季,6-8月夏季,9-11月秋季,12-2月冬季。
forestfires$season <- ifelse(forestfires$month=="mar"|forestfires$month=="apr"|forestfires$month=="may","spring",forestfires$month)
forestfires$season <- ifelse(forestfires$month=="jun"|forestfires$month=="jul"|forestfires$month=="aug","summer",forestfires$season)
forestfires$season <- ifelse(forestfires$month=="sep"|forestfires$month=="oct"|forestfires$month=="nov","autumn",forestfires$season)
forestfires$season <- ifelse(forestfires$month=="dec"|forestfires$month=="jan"|forestfires$month=="feb","winter",forestfires$season)
forestfires$season <- factor(forestfires$season, levels = c("spring","summer","autumn","winter"))
2.按月画着火次数图、着火概率图和着火总面积图
由于每一个月的记录次数不同,同时绘制着火未着火次数图和着火未着火概率图。
library(ggplot2)
layer1 <- ggplot(forestfires, aes(x = month, fill = iffire))
geom1 <- geom_bar(position = "dodge")
geom2 <- geom_bar(position = "fi