作者:带土;Daitu; Adam 邮箱:2505131775@qq.com
介绍graphics包中简单的参数设置,和使用Rstudio时的一些小技巧,
常用的参数使用,主要是par()函数的介绍,可视化点图、线图等
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
par(bg="gray80",bty = "l",cex=0.85,col.lab = "red",family = "STKaiti",
fg = "blue",pty = "m",pch=18)
plot(iris$Sepal.Length,iris$Sepal.Width,type = "p",col="red",
main = "可视化散点图",xlab = "Sepal.Length",
ylab = "Sepal.Width",sub = "Iris dataset")
text(x=mean(iris$Sepal.Length),y=mean(iris$Sepal.Width),
labels = "均值",cex = 2)## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
iS <- iris$Species == "setosa"
iV <- iris$Species == "versicolor"
matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width",
main = "Petal and Sepal Dimensions in Iris")
## 可视化数据中长度和宽度
matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = "sS", col = c(2,4))
matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = "vV", col = c(2,4))
legend(1, 4, c(" Setosa Petals", " Setosa Sepals",
"Versicolor Petals", "Versicolor Sepals"),
pch = "sSvV", col = rep(c(2,4), 2))介绍如何graphics包绘制多个子图时的一些使用方法。 绘制图像,设置图像之间的间距,子图的布局方法等内容的介绍
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 绘制子图,1 行2列
par(family = "STKaiti",mfrow = c(1,2))
plot(iris$Sepal.Length,iris$Sepal.Width,type = "p",col="red")
plot(iris$Sepal.Length,iris$Sepal.Width,type = "p",col="red")## 绘制子图,2行1列,
## 设置图像之间的间距,mai = c(bottom, left, top, right)
par(family = "STKaiti",mfrow = c(2,1),mai = c(0.4,0.8,0.1,0.1))
plot(iris$Sepal.Length,iris$Sepal.Width,type = "p",col="red")
plot(iris$Sepal.Length,iris$Sepal.Width,type = "p",col="red")## Loading required package: magrittr
##
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
##
## add
## The following objects are masked from 'package:stats':
##
## convolve, spectrum
## The following object is masked from 'package:graphics':
##
## frame
## The following object is masked from 'package:base':
##
## save.image
im <- load.image("data/Rimage.jpg")
## 绘制子图,5行5列,
## 设置图像之间的间距,mai = c(bottom, left, top, right)
## mai = c(0.01,0.01,0.01,0.01)
par(family = "STKaiti",mfrow = c(5,5),mai = c(0.05,0.1,0.05,0.1))
for (ii in 1:25){
plot(im,axes = FALSE)
}## mar = c(bottom, left, top, right)
par(family = "STKaiti",mfrow = c(5,5),mar = c(0.2,1,0.2,1))
for (ii in 1:25){
plot(im,axes = FALSE)
}## 第一个参数mat。它是一个矩阵,表示绘图区分隔的各部分,相同数字的部分合并成一个绘图区,数字也是它们作图时的顺序。
set.seed(12)
mat = matrix(c(1,2,3,3),nrow = 2,byrow = TRUE)
mat## [,1] [,2]
## [1,] 1 2
## [2,] 3 3
par(mfrow = c(2,2),mar = c(2,2,1,1))
layout(mat = mat)
plot(rnorm(20),pch = 1)
plot(rnorm(20),pch = 2)
plot(rnorm(20),pch = 3)2.3 graphics包可视化统计图
介绍使用graphics包中的直方图、箱线图、饼图、等高线图等视化图。
## 直方图
par(family = "STKaiti")
hist(iris$Sepal.Length,breaks = 20,freq = TRUE,
col = "lightblue",main = "直方图")## 密度曲线,lwd:线宽
par(family = "STKaiti")
plot(density(iris$Sepal.Length,bw = 0.5),type = "l",
col = "red",lwd = 2,main = "密度曲线")## 箱形图,notch:是否绘制一个凹口,outline:是否绘制离群点
par(family = "STKaiti")
boxplot(Sepal.Length~Species,data = iris,
notch = T,outline = T,main = "箱形图")## 饼图
pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12) ## 百分比
## 设置名字
names(pie.sales) <- c("Blueberry", "Cherry","Apple",
"Boston Cream", "Other", "Vanilla Cream")
## 绘制饼图
pie(pie.sales, col = c("purple", "violetred1", "green3",
"cornsilk", "cyan", "white"))