【数据挖掘】实验6:初级绘图

实验6:初级绘图

一:实验目的与要求

1:了解R语言中各种图形元素的添加方法,并能够灵活应用这些元素。

2:了解R语言中的各种图形函数,掌握常见图形的绘制方法。

二:实验内容

【直方图】

Eg.1:画出cars数据集中speed的直方图

hist(cars$speed)

【条形图】

Eg.1:VADeaths数据集的条形图——beside=T,horiz=F

barplot(VADeaths, beside = T)

Eg.2:VADeaths数据集的条形图——beside=F,horiz=T

barplot(VADeaths, beside = T)

Eg.3:VADeaths数据集的条形图——beside=T,horiz=T

barplot(VADeaths, beside = T, horiz = T)

【饼图】

Eg.1:数据集VADeaths展示不同人群死亡率的占比情况

percent <- colSums(VADeaths)*100/sum(VADeaths)

pie(percent, labels = paste0(colnames(VADeaths),'\n',round(percent,2),'%'))

【散点图】

Eg.1:cars数据集的速度与刹车距离的散点图,绘制方法1

plot(cars[, 1], cars[,2])

Eg.2:cars数据集的速度与刹车距离的散点图,绘制方法2

plot(cars)

【散点矩阵图】

Eg.1:iris数据集为例,用pairs函数绘制散点矩阵图

# 方法1

pairs(iris[,1:4])

# 方法2

pairs(~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data=iris)

【多变量相关矩阵图】

Eg.1:mtcars数据集绘图

install.packages("corrgram")

library(corrgram)

corrgram(mtcars, order=TRUE, upper.panel=panel.ellipse, main="Correlogram of mtcars intercorrelations")

corrgram(mtcars, order=TRUE, upper.panel=panel.pts, lower.panel=panel.pie,main="Correlogram of mtcars intercorrelations")

corrgram(mtcars, order=TRUE, upper.panel=panel.conf, lower.panel=panel.cor,main="Correlogram of mtcars intercorrelations")

[1] 相关图,主对角线上方绘制置信椭圆和平滑拟合曲线,主对角线下方绘制阴影

[2] 相关图,主对角线上方绘制散点图,主对角线下方绘制饼图

[3] 相关图,主对角线上方绘制置信区间,主对角线下方绘制相关系数

【核密度图】

Eg.1:

install.packages("sm")

library(sm)

sm.density.compare(mtcars$wt, factor(mtcars$cyl))

【小提琴图】

Eg.1:mtcars数据集wt的小提琴图

install.packages("vioplot")

library(vioplot)

attach(mtcars)

par(mfrow=c(1,2))

vioplot(wt[cyl == 4], wt[cyl == 6], wt[cyl == 8], border = "black", col = "lightgreen", rectCol = "blue", horizontal = TRUE)

title(main = '小提琴图')

boxplot(wt~cyl, main = '箱线图', horizontal=TRUE, pars=list(boxwex=0.1),border="blue")

par(mfrow = c(1, 1))

【QQ图】

Eg.1:mtcars数据集wt的QQ图

qqnorm(wt)

qqline(wt)

qqplot(qt(ppoints(length(wt)), df = 5), wt,xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", main = "Q-Q plot for t dsn")

qqline(wt)

 

 

【星状图】

Eg.1:

stars(mtcars,draw.segments = T)

【等高图】

Eg.1:

library(KernSmooth)

mtcars1 = data.frame(wt, mpg)

est = bkde2D(mtcars1, apply(mtcars1, 2, dpik))

contour(est$x1, est$x2, est$fhat, nlevels = 15, col = "darkgreen", xlab = "wt",ylab = "mpg")

points(mtcars1)

【固定颜色选择函数】

Eg.1:查看前20种颜色

colors()[1:20]

Eg.2:打印657种颜色

par(mfrow = c(length(colors())%/%60 + 1, 1))

par(mar=c(0.1,0.1,0.1,0.1), xaxs = "i", yaxs = "i")

for(i in 1:(length(colors())%/%60 + 1)){

  barplot(rep(1,60),col=colors()[((i-1)*60+1):(i*60)],border=colors()[((i-1)*60+1):(i*60)],axes=F)

  box()

}

Eg.3:固定调色板

palette()

palette(colors()[1:10])

palette()

palette('default')

Eg.4:在不同Species使用不同的颜色绘制散点,以便区分种类

# 方法1

plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)

# 方法2

plot(iris$Sepal.Length, iris$Sepal.Width, col = rep(palette()[1:3], each = 50))

Eg.5:渐变色生成函数调色板的颜色样式

rgb<-rgb(red=255,green=1:255,blue=0,max=255)

par(mfrow=c(6,1))

par(mar=c(0.1,0.1,2,0.1), xaxs="i", yaxs="i")

barplot(rep(1,255),col= rgb,border=rgb,main="rgb")

barplot(rep(1,100),col=rainbow(100),border=rainbow(100),main="rainbow(100))")

barplot(rep(1,100),col=heat.colors(100),border=heat.colors(100),main="heat.colors(100))")

barplot(rep(1,100),col=terrain.colors(100),border=terrain.colors(100),main="terrain.colors(100))")

barplot(rep(1,100),col=topo.colors(100),border=topo.colors(100),main="topo.colors(100))")

barplot(rep(1,100),col=cm.colors(100),border=cm.colors(100),main="cm.colors(100))")

【RColorBrewer依赖包】

Eg.1:颜色展示

par(mfrow = c(1,3))

library(RColorBrewer)

par(mar=c(0.1,3,0.1,0.1))

display.brewer.all(type="seq")

display.brewer.all(type="div")

display.brewer.all(type="qual")

Eg.2:绘制颜色散点图

library(RColorBrewer)

par(mfrow = c(1,2))

my_col <- brewer.pal(3,'RdYlGn')

plot(iris$Sepal.Length, iris$Sepal.Width, col = rep(my_col, each =50))

plot(iris$Sepal.Length, iris$Sepal.Width, col = rep(rainbow(3), each = 50))

【axis函数绘图】

Eg.1:

plot(c(1:12), col="white", xaxt="n", yaxt="n", ann = FALSE)

axis(1, at=1:12, col.axis="red", labels=month.abb)

axis(2, at=seq(1,12,length=10), col.axis="red", labels=1:10, las=2)

axis(3, at=seq(1,12,length=7), col.axis="blue", cex.axis=0.7, tck=-0.01, labels = c("Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun"))

axis(4, at=seq(1,12,length=11), col.axis="blue", cex.axis=0.7, tck=-0.01, labels=seq(0, 1, 0.1),las=2)

【legend函数绘制图例】

Eg.1:

local=c("bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right", "center")

par(mar = c(4,2,4,2), pty='m')

plot(c(0:10), col = "white")

legend(3, 8, "图例在(3,8)")

legend(1, 13, "图例在(11,11)", xpd=T)

for(i in 1:9){

  legend(local[i],paste("图例在",local[i]))

}

【par函数】

Eg.1:

mfrow1=par(mfrow=c(2,3))

for(i in 1:6){

  plot(c(1:i),main=paste("I'm image:",i))

}

【layout函数】

Eg.1:

mat<-matrix(c(1,1,2,3,3,4,4,5,5,6), nrow = 2, byrow = TRUE)

layout(mat)

for(i in 1:6){

  plot(c(1:i),main=paste("I'm image:",i))

}

【保存图形】

Eg.1:

jpeg(filename = "C:/Users/86158/Desktop/iris.jpg")

plot(iris[,1:4])

dev.off()

Eg.2:657种颜色的打印并保存为PDF文件格式

pdf("colors-bar.pdf", height=120)

par(mar = c(0,10,3,0)+0.1,yaxs="i")

barplot(rep(1, length(colors())), col = rev(colors()), names.arg=rev(colors()),horiz = T, las = 1, xaxt="n", main = expression("Bars of colors in"~ italic(colors())))

dev.off()

三:课堂练习

【练习1】PPT-08,第18页,iris箱线图

Eg.1:合并显示

par(mfrow = c(1,2))

boxplot(iris[1:4], main = '单独的箱线图')

boxplot(Sepal.Length ~ Species, data = iris, main = '组间比较的箱线图')

Eg.2:单独显示

par(mfrow = c(1,1))

boxplot(iris[1:4], main = '单独的箱线图')

boxplot(Sepal.Length ~ Species, data = iris, main = '组间比较的箱线图')

【练习2】绘图散点图

Eg.1:

attach(mtcars)  # 绑定数据框mtcars

plot(wt, mpg)  # 打开图形窗口,绘制散点图

detach(mtcars)  # 解除绑定数据框mtcars

【练习3】palette函数的应用

Eg.1:

data(mtcars)

par(mfrow = c(1, 1))

plot(mtcars$wt, mtcars$mpg, col = "blue")  

plot(mtcars$wt, mtcars$mpg, col = 4) 

attach(mtcars)   

str(mtcars)

plot(wt, mpg, col = "red", xlim = c(1.3, 5.6), ylim = c(8, 35))

points(wt[cyl == 6], mpg[cyl == 6], col = "green")

points(wt[cyl == 8], mpg[cyl == 8], col = "blue")

legend(5, 35, c(4, 6, 8), pch = 1, col = c("red", "green", "blue"), bty = "n")

【练习4】渐变色生成函数

Eg.1:

library(RColorBrewer)

attach(mtcars)

cl <- brewer.pal(3, "Dark2")  # 左图代码,RColorBrewer包配色方案的使用

par(mfrow = c(1, 1))

plot(wt, mpg, col = cl[1])

points(wt[cyl == 6], mpg[cyl == 6], col = cl[2])

points(wt[cyl == 8], mpg[cyl == 8], col = cl[3])

legend(5, 35, c(4, 6, 8), pch = 1, col = cl, bty = "n")

Eg.2:

cl <- rainbow(3)  # 右图代码,rainbow函数的使用

plot(wt, mpg, col = cl[1])

points(wt[cyl == 6], mpg[cyl == 6], col = cl[2])

points(wt[cyl == 8], mpg[cyl == 8], col = cl[3])

legend(5, 35, c(4, 6, 8), pch = 1, col = cl, bty = "n")

【练习5】点的样式

Eg.1:

plot(1, col = "white", xlim = c(1, 8), ylim = c(1, 7))

symbol <- c("*", "、", ".", "o", "O", "0", " + ", " - ", "|")

# 创建循环添加点

for (i in c(0:34)) {

  x <- (i %/% 5) * 1 + 1

  y <- 6 - (i %% 5)

  if (i > 25) {

    points(x, y, pch = symbol[i - 25], cex = 1.3)

    text(x + 0.5, y + 0.1, labels = paste("pch = ", symbol[i - 25]), cex = 0.8)

  } else {

    if (sum(c(21:25) == i) > 0) {

      points(x, y, pch = i, bg = "red", cex = 1.3)

    } else {

      points(x, y, pch = i, cex = 1.3)

    }

    text(x + 0.5, y + 0.1, labels = paste("pch = ", i), cex = 0.8)

  }

}

【练习6】改变点的样式

Eg.1:

attach(mtcars)  # 绑定数据框mtcars

cyl <- as.factor(cyl)

plot(wt, mpg, col = "white")

points(wt, mpg, pch = as.integer(cyl) + 1, col = as.integer(cyl) + 1)

legend(5, 35, c(4, 6, 8), pch = 2:4, col = 2:4, bty = "n")

Eg.2:

plot(wt, mpg, pch = as.integer(cyl) + 1, col = as.integer(cyl) + 1)

legend(5, 35, c(4, 6, 8), pch = 2:4, col = 2:4, bty = "n")

detach(mtcars)  # 解除绑定数据框mtcars

运行结果:同上图。

【练习7】使用title()展示标题位置

Eg.1:

plot(c(0:5), col = "white", xlab = "", ylab = "")

title(main = list("主标题", cex = 1.5), sub = list("副标题", cex = 1.2),

      xlab = "x轴标题", ylab = "y轴标题")

【练习8】使用text()展示字体样式、字体大小

Eg.1:

plot(c(0:5), col = "white")

text(2, 4, labels = "font = 1:正常字体(默认)", font = 1)

text(3, 3, labels = "font = 2:粗体字体", font = 2)

text(4, 2, labels = "font = 3:斜体字体", font = 3)

text(5, 1, labels = "font = 4:粗斜体字体", font = 4)

# 大小

plot(c(0:6), col = "white", xlim = c(1, 8))

text(2, 5, labels = "cex = 0.5:放大0.5倍", cex = 0.5)

text(3, 4, labels = "cex = 0.8:放大0.8倍", cex = 0.8)

text(4, 3, labels = "cex = 1(默认):正常大小", cex = 1)

text(5, 2, labels = "cex = 1.2:放大1.2倍", cex = 1.2)

text(6, 1, labels = "cex = 1.5:放大1.5倍", cex = 1.5)

【练习9】mtext()展示文本位置

Eg.1:

plot(c(0:5), col = "white")

mtext("side = 1:下边", side = 1, line = 2); mtext("side = 2:左边" , side = 2, line = 2)

mtext("side = 3:上边", side = 3); mtext("side = 4:右边" , side = 4)

【练习10】以mtcars数据集为例,将散点图变为文本字符

Eg.1:

cyl <- as.factor(cyl)

plot(wt, mpg, col = "white", xlab = "", ylab = "")

text(wt, mpg, cyl, col = as.integer(cyl) + 1)

title(main = list("Miles per Gallon vs. Weight by Cylinder", cex = 1.5),

      xlab = "Weight", ylab = "Miles per Gallon")

Eg.2:

plot(wt, mpg, pch = as.character(cyl), col = as.integer(cyl) + 1,

     xlab = "Weight", ylab = "Miles per Gallon ",

     main = "Miles per Gallon vs. Weight by Cylinder", cex.main = 1.5)

运行结果:同上图。

【练习11】线的样式和线的宽度

Eg.1:线的样式

data <- matrix(rep(rep(1:7), 10), ncol = 10, nrow = 7)

plot(data[1, ], type = "l", lty = 0, ylim = c(1, 8), xlim = c(-1, 10), axes = F)

text(0, 1, labels = "lty = 0")

for (i in c(2:7)) {

  lines(data[i, ], lty = i - 1)

  text(0, i, labels = paste("lty = ", i - 1))

}

Eg.2:线的宽度

data <- matrix(rep(rep(1:6), 10), ncol = 10, nrow = 6)

plot(data[1, ], type = "l", lwd = 0.5, ylim = c(1, 8), xlim = c(-1, 10), axes = F); text(0, 1, labels = "lwd = 0.5")

lines(data[2, ], type = "l", lwd = 0.8);text(0, 2, labels = "lwd = 0.8")

lines(data[3, ], type = "l", lwd = 1);text(0, 3, labels = "lwd = 1")

lines(data[4, ], type = "l", lwd = 1.5);text(0, 4, labels = "lwd = 1.5")

lines(data[5, ], type = "l", lwd = 2);text(0, 5, labels = "lwd = 2")

lines(data[6, ], type = "l", lwd = 4);text(0, 6, labels = "lwd = 4")

【练习12】添加参考线

Eg.1:

# 绘制空白画布

plot(c(0:10), col = "white")

# 添加水平线

abline(h = c(2, 6, 8))

# 添加垂直线

abline(v = seq(2, 10, 2), lty = 2, col = "blue")

# 添加直线y = 2+x

abline(a = 2, b = 1)

 

【练习13】添加线段和箭头

Eg.1:

plot(c(0:10), col = "white")

segments(2, 1, 4, 8)

arrows(4, 0, 7, 3, angle = 30)

arrows(4, 2, 7, 5, angle = 60)

【练习14】添加网格线

Eg.1:

plot(c(0:10), col = "white")  # 空白画布

grid(nx = 4, ny = 8, lwd = 1, lty = 2, col = "blue")  # 添加网格线

【练习15】添加坐标轴须

Eg.1:

set.seed(123)  # 种子

x <- rnorm(500)  # 生成500个标准正态分布的数据

plot(density(x))  # 绘制核密度曲线

rug(x , col = "blue")  # 添加坐标轴须

【练习16】以mtcars数据集为例查看不同的线元素函数的用法

Eg.1:

smpg <- (mpg - min(mpg)) / (max(mpg) - min(mpg))

plot(wt, smpg, ylab = "standardized mpg")

# 添加核密度曲线图

lines(density(wt), col = "red")

# 指向密度曲线的箭头

arrows(1.8, 0.05, 1.5, 0.1, angle = 10, cex = 0.5)

text(2, 0.05, "核密度曲线", cex = 0.6)

# 添加回归线

abline(lm(smpg ~ wt), lty = 2, col = "green")

# 指向回归直线的箭头

arrows(2, 0.5, 2, 0.7, angle = 10, cex = 0.5)

text(2, 0.45, "回归线", cex = 0.6)

# wt与mpg反向线性相关,添加最大最小值线段表现这种关系

segments(min(wt), max(smpg), max(wt), min(smpg), lty = 3, col = "blue")

# 指向最大最小值线段的箭头

arrows(3, 0.8, 2.5, 0.76, angle = 10, cex = 0.5)

text(3.3, 0.8, "最大最小值线段", cex = 0.6)

# 添加网格线作为背景

grid(nx = 4, ny = 5, lty = 2, col = "grey")

Eg.2:

par(mfrow = c(1, 3))

plot(density(wt), col = "red")  # 绘制核密度曲线

plot(wt, fitted(lm(mpg ~ wt)), type = "l", lty = 2, col = "green")  # 绘制回归线

plot(seq(min(wt), max(wt), length = 100), seq(max(mpg), min(mpg), length = 100),

     type = "l", lty = 3, col = "blue")  # 绘制最大、最小值线

【练习17】输出到屏幕

Eg.1:

windows()  # 打开图形设备界面

attach(mtcars)

plot(wt, mpg)

X11()  # 打开图形设备界面

plot(wt, mpg)

【练习18】以mtcars为例,生成的直方图

Eg.1:

op <- par(mfrow = c(2, 3), mar = c(4, 4, 2, 0.5), mgp = c(2, 0.5, 0))

hist(wt, main = "freq = TRUE")  # 默认的频数直方图,左下,中上,中下,右上,右下

hist(wt, breaks = 5, main = "breaks = 5")  # 减小区间段数的直方图

hist(wt, col = "light blue", main = "colored")  # 给直方图的柱形添加颜色

hist(wt, freq = FALSE, main = "freq = FALSE")  # 概率密度直方图

hist(wt, breaks = 40, main = "breaks = 40")  # 增大区间段数的直方图

# 在直方图上添加密度曲线和正态分布概率密度曲线

hist(wt, freq = FALSE, main = "with density curve and normal curve")  

lines(density(wt), col = "blue")

lines(density(rnorm(1e+6, mean(wt), sd(wt))), lty = 2, col = "red")

par(op)

【练习19】绘图箱线图

Eg.1:

set.seed(1234)

normal <- rnorm(100, mean(wt), sd(wt))  # 生成100个正态分布数据

op <- par(mfrow = c(1, 3))

boxplot(list(wt, normal), xaxt = "n")  # 绘制箱线图

axis(1, at = 1:2, labels = c("wt", "normal"))  # 添加坐标轴

rug(wt, side = 2, col = 2); rug(normal, side = 4, col = 3)  # 添加坐标轴须

legend("bottomleft", c("wt", "normal"), lty = 1, col = 2:3, bty = "n")  # 添加图例

boxplot(list(wt, normal), xaxt = "n", varwidth = TRUE)

rug(wt, side = 2, col = 2); rug(normal, side = 4, col = 3)

axis(1, at = 1:2, labels = c("wt", "normal"))

legend("bottomleft", c("wt", "normal"), lty = 1, col = 2:3, bty = "n")

boxplot(wt ~ cyl)

rug(wt[cyl == 4], side = 2, col = 2); rug(wt[cyl == 6], side = 4, col = 3)

rug(wt[cyl == 8], side = 2, col = 4)

legend("topleft", c("4", "6", "8"), lty = 1, col = 2:4, bty = "n")

par(op)

【练习20】绘图小提琴图

Eg.1:

# 页面分割掉1/2,为与箱线图和核密度图对比而作,小提琴图只需要第二个语句即可

par(fig = c(0, 1, 0.5, 1), mfrow = c(2, 1))

# 绘制小提琴图

library(vioplot)

vioplot(wt[cyl == 4], wt[cyl == 6], wt[cyl == 8], border = "black",

        col = "light green", rectCol = "blue", horizontal = TRUE)

# 分割另外1/2页面

par(fig = c(0, 1, 0, .5), mar = c(0, 2, 0, 0.5) , new = TRUE)

# 绘制箱线图

boxplot(wt ~ cyl, horizontal = TRUE, pars = list(boxwex = 0.1), border = "blue")

# 在箱线图上叠加核密度图

par(fig = c(0, 0.53, 0.1, 0.2), new = TRUE)

plot(density(wt[cyl == 4], bw = 0.3), xaxt = "n", yaxt = "n", ann = FALSE, bty = "n")

par(fig = c(0.26, 0.56, 0.25, 0.35), new = TRUE)

plot(density(wt[cyl == 6], bw = 0.3), xaxt = "n", yaxt = "n", ann = FALSE, bty = "n")

par(fig = c(0.33, 1, 0.4, 0.5), new = TRUE)

plot(density(wt[cyl == 8], bw = 0.5), xaxt = "n", yaxt = "n", ann = FALSE, bty = "n")

【练习21】绘图条形图

Eg.1:

bardata <- table(cyl, carb)  # 得到表格数据

pal <- RColorBrewer::brewer.pal(3, "Set1")  # 颜色调配

op <- par(mfrow = c(2, 2), mar = c(3, 3, 3, 2), mgp = c(1.5, 0.5, 0)) 

barplot(bardata, col = pal, beside = TRUE, xlab = "carb")  # 分组条形图

legend("topright", c("4", "6", "8"), pch = 15, col = pal, bty = "n")

barplot(bardata, col = pal, xlab = "carb")  # 默认堆砌条形图

legend("topright", c("4", "6", "8"), pch = 15, col = pal, bty = "n")

barplot(bardata, col = pal, beside = TRUE, horiz = TRUE, ylab = "carb")  # 水平放置的条形图

legend(5.3, 26, c("4", "6", "8"), pch = 15, col = pal, bty = "n", cex  =  0.6)

barplot(bardata, col = pal, beside = TRUE, ylim = c(0, 7), xlab = "carb")    

legend("topright", c("4", "6", "8"), pch = 15, col = pal, bty = "n")

# 显示数值

text(labels = as.vector(bardata), cex = 0.7, x = c(1.5:23.5)[1:23 %% 4 > 0],

     y = as.vector(bardata) + 0.5)

par(op)

【练习22】绘图点图

Eg.1:

dotchart(bardata, bg = pal)

【练习23】绘图饼图

Eg.1:

percent <- as.vector(table(cyl)) / sum(as.vector(table(cyl))) * 100  # 计算百分比

pie(table(cyl), labels = paste(c("4", "6", "8"), "cylinders:", percent, "%"))  # 画饼图

【练习24】用methods()查plot()的作图方法

Eg.1:

methods("plot")

【练习25】plot()函数的应用

Eg.1:

plot(density(wt), type = "l")

class(density(wt))  # 第一个参数density类,画核密度曲线

plot(table(cyl, vs)); class(table(cyl, vs))  # 第一个参数table类,画马赛克图

opr <- par(mfrow = c(2, 3), mar = c(4, 4, 2, 4))   

for (i in 1:6) {

  plot(lm(mpg ~ wt), i)  # 第一个参数lm类,画回归诊断图

}

par(opr); class(lm(mpg ~ wt))

plot(mtcars[, c(1, 3:7)])

class(mtcars[, c(1, 3:7)])  # 第一个参数data.frame类,画散点图矩阵

Eg.2:

x <- seq(from = 0, to = 2*pi, length = 10)  # 取10个x值

y <- sin(x)  # 计算相对应的y值

type <- c("p", "l", "b", "o", "c", "h", "s", "S", "n" )  # 图形类型向量

op <- par(mfrow = c(3, 3), mar = c(4, 4, 1, 1))

for (i in 1:9) {

  plot(x, y, type = type[i] , main = paste("type:", type[i]))

}

par(op) 

【练习26】绘图散点图矩阵

Eg.1:绘图对象为公式

pairs( ~ mpg + disp + drat + wt, data = mtcars, col = as.integer(factor(cyl)) + 1,

       main = "Scatter Plot Matrix")

Eg.2:绘图对象为数据框

pairs(mtcars[, c(1, 3, 5, 6)], col = as.integer(factor(cyl)) + 1,

      main = "Scatter Plot Matrix")

运行结果:同上图。

Eg.1:

mosaicdata <- ftable(cyl, vs)  # 二维列联表

par(mfrow = c(1, 1))

mosaicplot(mosaicdata, shade = TRUE, main = "")  # 绘制马赛克图

【练习28】绘图向日葵散点图

Eg.1:

sunflowerplot(mpg, cyl, col = "green", seg.col = "light green")

【练习29】绘图热图

Eg.1:

heatmap(as.matrix(mtcars), col = pal, scale = "column")

四:实验知识点总结

1:初级绘图主要包括以下四个部分——绘制基础图形、修改图形参数、绘制组合图形、保存图形。

2:分析数据分布情况时,可以使用直方图、条形图、饼图、箱线图、散点图等图形。相关图形的使用函数和功能如下图所示。

3:R语言绘图函数分类,如下图所示。

五:遇到的问题和解决方法


问题1:在PPT08,第84页中,jpeg设置保存的路径和文件名称、类型处运行会出错。具体的报错如下图所示。

解决1:经过查询后,发现jpeg函数中参数应该是【filename】而不是【filenames】,修改之后即可通过。

问题2:安装包可能因为镜像源的问题而无法下载。

解决2:换源,可使用【chooseCRANmirror()】代码。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/543596.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

单路高清HDMI编码器JR-3211HD

产品简介&#xff1a; JR-3211HD单路高清HDMI编码器是专业的高清音视频编码产品&#xff0c;该产品具有支持1路高清HDMI音视频采集功能&#xff0c; 1路3.5MM独立外接音频输入&#xff0c;编码输出双码流H.264格式&#xff0c;音频MP3/AAC格式。编码码率可调&#xff0c;画面质…

CASA(Carnegie-Ames-Stanford Approach)模型原理及实践应用

植被作为陆地生态系统的重要组成部分对于生态环境功能的维持具有关键作用。植被净初级生产力&#xff08;Net Primary Productivity, NPP&#xff09;是指单位面积上绿色植被在单位时间内由光合作用生产的有机质总量扣除自养呼吸的剩余部分。植被NPP是表征陆地生态系统功能及可…

Linux登录访问限制

Linux系统下&#xff0c;用户密码的有效期可以通过编辑/etc/login.defs文件控制&#xff1b;密码复杂度规则设定需要通过/etc/pam.d/system-auth文件控制&#xff1b;登录失败次数限制通常由/etc/pam.d/login文件限制&#xff0c;可使用pam_tally2模块进行设置。 Linux系统下的…

Linux的学习之路:10、进程(2)

摘要 本章主要是说一下fork的一些用法、进程状态、优先级和环境变量。 目录 摘要 一、fork 1、fork的基本用法 2、分流 二、进程状态 三、优先级 四、环境变量 1、常见环境变量 2、和环境变量相关的命令 3、通过代码如何获取环境变量 五、导图 一、fork 1、fork…

Houdini笔记操作技巧_集锦

个人记录下&#xff0c;谨防遗忘。同时丰富下Hou的中文搜素环境。 1、自定义启动界面 ① 设置完界面后&#xff0c;保存自定义界面&#xff08;Save Current Desktop As...&#xff09; ② Edit-->Preferences-->General UIInterface-->Startup in Desktop&#xff1…

换手机了怎么重新设置Microsoft Authenticator验证?

进入到这个页面 https://mysignins.microsoft.com/security-info 选择安全信息

顺序表实战——基于顺序表的通讯录

前言&#xff1a;本篇文章主要是利用顺序表作为底层&#xff0c; 实现一个通讯录。偏向于应用&#xff0c; 对于已经学习过c的友友们可能没有难度了已经。没有学习过c的友友&#xff0c; 如果顺序表不会写&#xff0c; 或者说没有自己实现过&#xff0c; 请移步学习顺序表相关内…

手机数据恢复工具

下载地址&#xff1a;手机数据恢复工具.zip Android/HarmonyOS 文件误删是日常使用电子设备时经常遇到的问题&#xff0c;也许一不小心就就可能会误删。 俗话说&#xff1a;数据无价&#xff0c;一但想要找回一些被删除的文件&#xff0c;就需要耗费大量的精力和财力来恢复文…

【网站项目】面向企事业单位的项目申报小程序

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

【电控笔记5】电流环速度环三环参数整定

旋转坐标系下的电压方程,由id和iq计算出ud和uq Lq:q轴电感 Ld:d轴电感 输入是电流,输出是电压? 内嵌式pmsm(ipmsm)模型建立: 其中: λf是转子磁场在定子绕组所产生的磁通链,为一常数,在psms中转子磁场非常稳定几乎不变。 ipmsm转矩方程式: 对永磁同步马达而言,使…

Niobe开发板OpenHarmony内核编程开发——定时器

本示例将演示如何在Niobe Wifi IoT开发板上使用cmsis 2.0 接口进行定时器开发 Timer API分析 osTimerNew() /// Create and Initialize a timer./// \param[in] func function pointer to callback function./// \param[in] type \ref osTimerOnce …

ELK日志分析系统+Filebeat

目录 一、Filebeat介绍 1、Filebeat简介 2、Filebeat的工作方式 3、filebeat工作流程 4、Filebeat的作用 5、filebeat的用途 1.为什么要用filebeat来收集日志&#xff1f;为什么不直接用logstash收集日志&#xff1f; 2.filebeat和logstash的区别 二、部署(ELFK)Fileb…

uniCloud联表查询方式举例

联查表&#xff1a; 1. 在shema中配置外键&#xff1a; 2.在前端使用&#xff1a; <unicloud-db v-slot:default"{data, loading, error, options}" :options"formData" collection"opendb-news-articles,uni-id-users" //这里这么写 fi…

八、Yocto 创建自定义的conf文件

文章目录 Yocto创建自定义的conf文件保存当前项目的配置文件使用自定义配置创建编译工作区 Yocto创建自定义的conf文件 本篇文章为基于raspberrypi 4B单板的yocto实战系列的第八篇文章&#xff1a; 一、yocto 编译raspberrypi 4B并启动 二、yocto 集成ros2(基于raspberrypi 4B…

本地web项目启起来后,无法在浏览器(chrome)看到源码,从而无法打断点;Framework Ignore list

问题描述 本地web项目启起来后&#xff0c;无法在浏览器(chrome)看到源码&#xff0c;从而无法打断点 其他浏览器没看&#xff0c;开发环境一致专注于chrome&#xff08;其余浏览器有测试同事提缺陷了&#xff0c;才会去看&#xff09;&#xff0c;其余浏览器有没有这个问题&…

Java代码基础算法练习-拆分一个三位数的个位、十位、百位-2024.04.14

任务描述&#xff1a;输入一个三位数&#xff0c;逆序输出这个三位数的个位、十位、百位对应的数字&#xff0c;用空格分开。 任务要求&#xff1a; 代码示例&#xff1a; package April_2024;import java.util.Scanner; public class a240414 {public static void main(Strin…

Leetcode - 周赛392

目录 一&#xff0c;3105. 最长的严格递增或递减子数组 二&#xff0c;3106. 满足距离约束且字典序最小的字符串 三&#xff0c;3107. 使数组中位数等于 K 的最少操作数 四&#xff0c;3108. 带权图里旅途的最小代价 一&#xff0c;3105. 最长的严格递增或递减子数组 本题求…

深度学习图像处理基础工具——opencv 实战2 文档扫描OCR

输入一个文档&#xff0c;怎么进行文档扫描&#xff0c;输出扫描后的图片呢&#xff1f; 今天学习了 opencv实战项目 文档扫描OCR 问题重构&#xff1a;输入图像 是一个含有文档的图像——> 目标是将其转化为 规则的扫描图片 那么怎么实现呢&#xff1f; 问题分解&#…

量子城域网系列(三):搭建一个点对点量子保密通信网络

各位小伙伴周末愉快呀&#xff0c;今天是4月14日世界量子日&#xff0c;至于为今天是世界量子日可以围观我之前的文章&#xff1a;关于世界量子日。 之前的文章中我们讨论了量子密钥在通信系统各层协议中的应用&#xff0c;那在实际工程中如何真正落地一个量子加密网络呢&a…

【Linux】序列化与反序列化{服客编程/守护进程/JSON}

文章目录 1.引入2. 静态成员函数3.TCP&#xff1a;传输控制协议4.守护进程4.0前台进程4.1介绍4.2认识4.3会话4.3ps axj4.4理解4.5/dev/null4.6守护进程和孤儿进程 5.JSON6.完整代码6.1Makefile6.2Socket.hpp6.3Protocol.hpp6.4Log.hpp6.5Daemon.hpp6.6TcpServer.hpp6.7Client.c…
最新文章