《R语言入门经典》第13章 用基础函数画图

碎碎念一下

昨天看到别人发的威信文章也有写这个学习笔记的,写到第3章就没有再更新了,一年了。

好吧,我会看完这本书的(后面几章可能够呛,嘻嘻)

补充练习

rm(list = ls())
x <- rnorm(100)
hist(x)#创建直方图

#为airquality数据中的每一个月,创建一个Wind和Ozone的关系图
#确保所有图在相同的轴,包含标题。

head(airquality)
par(mfrow = c(1,5))
# plot_list = list()
# for (M in unique(airquality$Month)) {
# b <- airquality[airquality$Month == M,]
# p = plot(b[,"Wind"],b[,"Ozone"],main = "Month")
# i = M-4
# plot_list[[i]] = p 
# }
dev.off()
for (M in unique(airquality$Month)) {
b <- airquality[airquality$Month == M,]
a <- paste("M",M,sep = "")
assign(a,b)
}
plot(M9$Wind,M9$Ozone,main = "9")  
plot(M5$Wind,M5$Ozone,main = "5")  
plot(M6$Wind,M6$Ozone,main = "6")
plot(M7$Wind,M7$Ozone,main = "7")
plot(M8$Wind,M8$Ozone,main = "8")

#创建成为PDF文档
dev.off()
pdf("myGrafic.pdf")
par(mfrow = c(1,1))
plot(M9$Wind,M9$Ozone,main = "9")  
plot(M5$Wind,M5$Ozone,main = "5")  
plot(M6$Wind,M6$Ozone,main = "6")
plot(M7$Wind,M7$Ozone,main = "7")
plot(M8$Wind,M8$Ozone,main = "8")
dev.off()

#创造单页PNG文件,内含练习2的5个图形,确定一个合适的布局显示数据
png("myPng.png")

a = rbind(c(1,3),c(1,4),c(2,5))
layout(a)#a这里注意一定要是矩阵
plot(M9$Wind,M9$Ozone,main = "9")  
plot(M5$Wind,M5$Ozone,main = "5")  
plot(M6$Wind,M6$Ozone,main = "6")
plot(M7$Wind,M7$Ozone,main = "7")
plot(M8$Wind,M8$Ozone,main = "8")
dev.off()

#每月单独一行,颜色不同,添加图例
a = matrix(c(1,2,3,4,5))
par(mfrow = c(5,2))
layout(a)#a这里注意一定要是矩阵
plot(M9$Wind,M9$Ozone,main = "9",col = "blue")#如果报错说不够宽,就把画图区域拉宽就好了
legend("topright",legend = "wind",pch = 16,"red")
plot(M5$Wind,M5$Ozone,main = "5",col = "red") 
legend("topright",legend = "wind",pch = 16,"red")
plot(M6$Wind,M6$Ozone,main = "6",col = "yellow")
legend("topright",legend = "wind",pch = 16,"red")
plot(M7$Wind,M7$Ozone,main = "7",col = "violetred")
legend("topright",legend = "wind",pch = 16,"red")
plot(M8$Wind,M8$Ozone,main = "8",col = "turquoise1")
legend("topright",legend = "wind",pch = 16,"red")
dev.off()
graphics.off()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。