R 实例 葡萄牙森林火灾数据 : forestfires 方法:绘图可视化 回归分析 因子分析

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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值