【数据挖掘】实验7:高级绘图(上)

news2025/6/9 6:30:15

实验7:高级绘图(上)

一:实验目的与要求

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

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

二:实验内容

【lattice包绘图】

Eg.1:以mtcars数据集为例,绘制车身重量(wt)与每加仑汽油行驶的英里数(mpg)的散点图

library('lattice')

xyplot(wt ~ mpg, data = mtcars, xlab = 'Weight', ylab = 'Miles per Gallon', main = 'lattice包绘制散点图')

Eg.2:查看参数列表名称

names(trellis.par.get())

Eg.3:fontsize查看字体大小和散点大小的参数

op <- trellis.par.get()

trellis.par.get('fontsize')

trellis.par.set(fontsize = list(text = 20, points = 20))

xyplot(wt ~ mpg, data = mtcars, xlab = 'Weight', ylab = 'Miles per Gallon', main = 'lattice包绘制散点图')

trellis.par.set(op)


Eg.4:图形化显示所有参数

show.settings()

Eg.5:以Species(鸢尾花种类)为条件变量绘制Sepal.Length(花萼长度)与Sepal.Width(花萼宽度)的散点图

library(lattice)

attach(iris)

xyplot(Sepal.Length ~ Sepal.Width | Species)

detach(iris)

Eg.6:面板函数

my_panel <- function(x,y){

  panel.lmline(x, y, col = "red", lwd = 1, lty = 2)

  panel.loess(x,y)

  panel.grid(h = -1, v = -1)

  panel.rug(x, y)

  panel.xyplot(x, y)

}

xyplot(mpg ~ wt, data = mtcars, xlab = "Weight", ylab = "Miles per Gallon",main = "Miles per Gallon on Weight", panel = my_panel)

Eg.7:分组变量

xyplot(Sepal.Length ~ Sepal.Width, group = Species, data = iris,pch = 1:3, col = 1:3, main = 'Sepal.Length VS Sepal.Width', key = list(space = "right", title = "Species", cex.title = 1, cex = 1, text = list(levels(factor(iris$Species))), points=list(pch = 1:3, col= 1:3)))

Eg.8:图形组合

graph1 <- xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris, main = '栅栏图')

graph2 <- xyplot(Sepal.Length ~ Sepal.Width, group = Species, data = iris, main = '散点图1')

graph3 <- xyplot(Petal.Length ~ Petal.Width, group = Species, data = iris, main = '散点图2')

# split函数

plot(graph1, split = c(1,1,3,1))

plot(graph2, split = c(2,1,3,1),newpage=F)

plot(graph2, split = c(3,1,3,1),newpage=F)

# position函数

plot(graph1, position = c(0, 0, 1/3, 1))

plot(graph2, position = c(1/3, 0, 2/3, 1), newpage = F)

plot(graph3, position = c(2/3, 0, 1, 1), newpage = F)

Eg.9:条形图

barchart(VADeaths, main = 'Death Rates in 1940 Virginia(By Group)')

barchart(VADeaths, groups = FALSE, main = list("Death Rates in 1940 Virginia", cex = 1.2))

Eg.10:泰塔尼克号航行中不同人群获救与否的人数情况

str(Titanic)

as.data.frame(Titanic)

pic1 <- barchart(Class ~ Freq|Age + Sex, data = as.data.frame(Titanic), groups = Survived, stack = TRUE, auto.key = list(title = "Survived", columns = 2))

pic2 <- barchart(Class ~ Freq|Age + Sex, data = as.data.frame(Titanic), groups = Survived, stack = TRUE, auto.key = list(title = "Survived", columns = 2), scales = list(x = "free"))

pic3 <- update(pic2, panel=function(...){

  panel.grid(h=0,v=-1)

  panel.barchart(...,border = "Transparent")

})

plot(pic1, split = c(1,1,3,1))

plot(pic2, split = c(2,1,3,1), newpage = FALSE)

plot(pic3, split = c(3,1,3,1), newpage = FALSE)

Eg.11:点图

dotplot(VADeaths, pch = 1:4, xlab = 'Death rates per 1000',

        main = list('Death Rates in 1940 Virginia (By Group)', cex = 0.8),

        key = list(column = 4, text = list(colnames(VADeaths)), points = list(pch = 1:4, col =1:4)))

dotplot(VADeaths, group = FALSE, xlab = 'Death rates per 1000',main = list('Death Rates in 1940 Virginia', cex = 0.8))

Eg.12:直方图

histogram( ~ height | voice.part, data = singer, nint = 17, layout = c(1,8), xlab = "Height(inches)")

Eg.13:核密度图

densityplot( ~ height | voice.part, data = singer, layout=c(1, 8), xlab = "Height (inches)",main = "Heights of New York Choral Society singers")

Eg.14:叠加核密度图

densityplot( ~ height, group = voice.part, data = singer, xlab = "Height (inches)" , plot.points = FALSE,main = "Heights of New York Choral Society singers", lty = 1:8, col = 1:8, lwd = 1.5,key = list(text = list(levels(singer$voice.part)), column = 4, lines = list(lty = 1:8, col = 1:8)))

Eg.15:添加核密度图

histogram( ~ height | voice.part, data = singer,

           xlab = "Height (inches)", type = "density",

           panel = function(x, ...) {

          panel.histogram(x, ...)

          panel.mathdensity(dmath = dnorm, col = "black",

          args = list(mean=mean(x),sd=sd(x)))

})

Eg.16:带状图

nrow(singer[singer$voice.part == 'Bass 2', ])

stripplot(~ height, group = voice.part, data = singer, xlab = "Height (inches)",

main = "Heights of New York Choral Society singers",

          subset = (voice.part == "Bass 2"),jitter.data=T)

Eg.17:QQ图

qqmath(~ height | voice.part, data = singer, prepanel = prepanel.qqmathline,

       panel = function(x, ...) {

       panel.qqmathline(x, ...)

       panel.qqmath(x, ...)

       })

qq(voice.part ~ height, aspect = 1, data = singer,subset = (voice.part == "Bass 2" | voice.part == "Tenor 2"))

Eg.18:箱线图

pic1 <- bwplot( ~ height | voice.part, data=singer, xlab="Height (inches)")

pic2 <- bwplot(voice.part ~ height, data=singer, xlab="Height (inches)")

plot(pic1, split = c(1, 1, 2, 1))

plot(pic2, split = c(2, 1, 2, 1), newpage = FALSE)

Eg.19:散点图

xyplot(Sepal.Length~Sepal.Width|Species,data=iris)

Eg.20:散点矩阵图

splom(iris[, 1:4], groups = iris$Species, pscales = 0, pch = 1:3, col = 1:3, varnames = colnames(iris)[1:4],key = list(columns = 3, text = list(levels(iris$Species)), points = list(pch = 1:3, col = 1:3)))

Eg.21:三维水平图

data(Cars93, package = "MASS")

cor.Cars93 <-cor(Cars93[, !sapply(Cars93, is.factor)], use = "pair")

levelplot(cor.Cars93, scales = list(x=list(rot=90)))

Eg.22:三维等高线

contourplot(volcano, cuts = 20)

Eg.23:三维散点图

par.set <-list(axis.line = list(col = "transparent"), clip = list(panel = "off")) # 去除边框,不削减面板范围

cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris, groups = Species,

        pch = 1:3,col= 1:3, # 点颜色及样式

        screen = list(z = 20, x = -70, y =0), # 调节三维散点图的展示角度

        par.settings = par.set,

        scales = list(col = "black"), # 加箭头指示

        key=list(column=3, text=list(levels(iris$Species)), points = list(pch = 1:3, col = 1:3)))


Eg.24:三维曲面图

par.set <-list(axis.line = list(col = "transparent"), clip = list(panel = "off")) # 去除边框,不削减面板范围

wireframe(volcano, shade = TRUE, par.settings = par.set, aspect = c(61/87, 0.4))

【ggplot2包绘图】

Eg.1:qplot函数

library(ggplot2)

qplot(Species, Sepal.Length, data = iris, geom = "boxplot", fill = Species,main = "依据种类分组的花萼长度箱线图")

qplot(Species, Sepal.Length, data = iris, geom = c("violin", "jitter"), fill = Species,main = "依据种类分组的花萼长度小提琴图")

qplot(Sepal.Length, Sepal.Width, data = iris, colour = Species, shape = Species,main = "绘制花萼长度和花萼宽度的散点图")

qplot(Sepal.Length, Sepal.Width, data = iris, geom = c("point", "smooth"), facets = ~Species,colour = Species, main = "绘制分面板的散点图")

Eg.2:语言逻辑

plot(iris$Sepal.Length, iris$Sepal.Width)

library(ggplot2)

ggplot(data= iris, aes(x = Sepal.Length, y = Sepal.Width)) + #绘制底层画布

geom_point(color = "darkred") #在画布上添加点

Eg.3:绘制画布

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species))

Eg.4:几何对象

#  方法1

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species))+geom_point()

# 方法2

ggplot(data = iris) + geom_point(aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species))

Eg.5:统计变换

# 方法1

ggplot(iris) + geom_bar(aes(x=Sepal.Length), stat="bin", binwidth = 0.5)

# 方法2

ggplot(iris) + stat_bin(aes(x=Sepal.Length), geom="bar", binwidth = 0.5)

Eg.6:标尺设置

set.seed(1234) # 设置随机种子

my_iris <- iris[sample(1:150, 100, replace = FALSE),] # 随机抽样

p <- ggplot(my_iris) + geom_bar(aes(x = Species, fill = Species))

p # 左图

p$scales # 查看p的标尺参数

p + scale_fill_manual(

  values = c("orange", "olivedrab", "navy"), # 颜色设置

  breaks = c("setosa", "versicolor", "virginica"), # 图例和轴要显示的分段点

  name = "my_Species", # 图例和轴使用的名称

  labels = c("set", "ver", "vir") # 图例使用的标签

  ) # 右图

Eg.7:修改图形的颜色

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species))+

  scale_color_manual(values = c("orange", "olivedrab", "navy"))+

  geom_point(size = 2)

ggplot(iris,aes(x = Sepal.Length, y = Sepal.Width, colour = Species))+

  scale_color_brewer(palette = "Set1")+

  geom_point(size=2)

Eg.8:坐标系转换

# 饼图 = 堆叠长条图 + polar coordinates

pie <- ggplot(my_iris, aes(x = factor(1), fill = Species)) +geom_bar(width = 1)

pie + coord_polar(theta = "y")

# 靶心图 = 饼图 + polar coordinates

pie + coord_polar()

#锯齿图 = 柱状图 + polar coordinates

cxc <- ggplot(my_iris, aes(x = Species)) +geom_bar(width = 1, colour = "black")

cxc + coord_polar()

 

Eg.9:分面

library(ggplot2)

library(tidyr)

library(dplyr)

my_iris1 <- iris %>% gather(feature_name, feature_value, one_of(c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))) # 数据变换

ggplot(my_iris1) +geom_violin(aes(x = Species, y = feature_value)) + facet_grid(feature_name ~ Species, scales = "free") # 分面

Eg.10:facet-wrap函数

ggplot(my_iris1) + geom_violin(aes(x = Species, y = feature_value)) + facet_wrap(~ feature_name + Species, scales = "free")

Eg.11:保存图形

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species))+geom_point(size = 2)

ggsave(file = "mygraph.pdf", width = 5, height = 4)

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

软考 - 系统架构设计师 - 面向对象架构设计案例

问题1&#xff1a; 解决该题&#xff0c;用例和参与者要一起进行分析&#xff0c;首先看到用例 U1 和 U2 是 U3 的扩展&#xff0c;分析用例列表中的用例&#xff0c;可以分析出 U1 和 U2 是Underpaid transaction 和 Record lllegal use&#xff0c;顺序可以颠倒&#xff0c;…

QAnything-1.3.0,支持纯python笔记本运行,支持混合检索

QAnything 1.3.0 更新了&#xff0c;这次带来两个主要功能&#xff0c;一个是纯python的安装&#xff0c;另一个是混合检索。更多详情见&#xff1a; https://github.com/netease-youdao/QAnything/releases 纯python安装 我们刚发布qanything开源的时候&#xff0c;希望用户…

rspack 使用构建vue3脚手架

基于 Rust 的高性能 Web 构建工具。rspack 主要适配 webpack 生态&#xff0c;对于绝大多数 webpack 工具库都是支持的。 启动速度快&#xff1b;增量热更新快。兼容 webpack 生态&#xff1b;内置了 ts、jsx、css、css modules 等开箱即用。生产优化&#xff0c;tree shaking…

JVM修炼之路【12】- GC调优 、性能调优

上一篇中 我们详细讲了内存溢出 内存泄漏 还有相关的案例。 这篇博客中我们主要了解一下GC调优。 有些新手可能会有一点 疑问—— 这两者不是一回事吗&#xff1f;&#xff1f; 其实说一回事 也没错 因为GC调优本质上还是针对 堆上的内存 只不过前面我们关注的侧重点在于 不合…

MATLAB绘制地球仪

clc;close all;clear all;warning off;%清除变量% 地球半径&#xff08;单位&#xff1a;千米&#xff09; R 6371;% 定义角度范围 theta linspace(0, 2*pi, 100); % 经度范围 phi linspace(0, pi, 100); % 纬度范围&#xff08;从北极到南极&#xff0c;0到pi&#xff09;%…

thinkphp6入门(23)-- 如何导入excel

1. 安装phpexcel composer require phpoffice/phpexcel composer update 2. 前端 <form class"forms-sample" action"../../xxxx/xxxx/do_import_users" method"post" enctype"multipart/form-data"><div class"cont…

【蓝桥杯】第十五届蓝桥杯大赛软件赛省赛(Java研究生组)个人解题思路及代码分享

文章目录 试题A&#xff1a;劲舞团试题B&#xff1a;召唤数字精灵试题C&#xff1a;封闭图形的个数试题D&#xff1a;商品库存管理试题E&#xff1a;砍柴试题F&#xff1a;回文字符串试题G&#xff1a;最大异或节点试题H&#xff1a;植物生命力 试题A&#xff1a;劲舞团 【问题…

阿里面试总结 一

写了这些还是不够完整&#xff0c;阿里 字节 卷进去加班&#xff01;奥利给 ThreadLocal 线程变量存放在当前线程变量中&#xff0c;线程上下文中&#xff0c;set将变量添加到threadLocals变量中 Thread类中定义了两个ThreadLocalMap类型变量threadLocals、inheritableThrea…

YOLO系列 | 正负样本分配策略

文章目录 1 Max-IoU matching(YOLOv1~V3)2 Multi-Anchor策略(YOLOv4)3 基于宽高比的领域匹配策略(YOLOv5)4 simOTA(Simple Optimal Transport Assignment)匹配策略(YOLOX, YOLOv6)5 领域匹配simOTA(YOLOv7)6 TaskAlignedAssigner匹配策略(YOLOv8, YOLOv9)参考资料 1 Max-IoU ma…

Redis:发布和订阅

文章目录 一、介绍二、发布订阅命令 一、介绍 Redis的发布和订阅功能是一种消息通信模式&#xff0c;发送者&#xff08;pub&#xff09;发送消息&#xff0c;订阅者&#xff08;sub&#xff09;接收消息。这种功能使得消息发送者和接收者不需要直接建立连接&#xff0c;而是通…

STC89C52学习笔记(十一)

STC89C52学习笔记&#xff08;十一&#xff09; 综述&#xff1a;本文讲述了直流电机以及PWM调速。 一、直流电机 1、特点 &#xff08;1&#xff09;直流电机能将电能转化位机械能。 &#xff08;2&#xff09;直流电机有两个电极&#xff0c;电极正接时&#xff0c;电机…

Stable Diffusion文生图技术详解:从零基础到掌握CLIP模型、Unet训练和采样器迭代

文章目录 概要Stable Diffusion 底层结构与原理文本编码器&#xff08;Text Encoder&#xff09;图片生成器&#xff08;Image Generator&#xff09; 那扩散过程发生了什么&#xff1f;stable diffusion 总体架构主要模块分析Unet 网络采样器迭代CLIP 模型 小结 概要 Stable …

WebLogic-XMLDecoder(CVE-2017-10271)反序列化漏洞分析及复现

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【Java、PHP】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收…

C++string类(个人笔记)

string类 1.认识string的接口以及熟练使用常用接口1.1string类对象的常见构造1.2string类对象的容量操作1.3string类对象的访问及遍历操作1.4string类对象的修改操作 2.vs 和g下string结构的说明3.string类运用的笔试题4.string类的模拟实现 1.认识string的接口以及熟练使用常用…

NPM 命令备忘单

NPM 简介 Node Package Manager (NPM) 是 Node.js 环境中不可或缺的命令行工具&#xff0c;充当包管理器来安装、更新和管理 Node.js 应用程序的库、包和模块。对于每个 Node.js 开发人员来说&#xff0c;无论他们的经验水平如何&#xff0c;它都是一个关键工具。 NPM 的主要…

Day19-【Java SE进阶】网络编程

一、网络编程 1.概述 可以让设备中的程序与网络上其他设备中的程序进行数据交互(实现网络通信的)。java.net,*包下提供了网络编程的解决方案! 基本的通信架构 基本的通信架构有2种形式:CS架构(Client客户端/Server服务端)、BS架构(Browser浏览器/Server服务端)。 网络通信的…

FourCastNet 论文解析

气象基础模型/气象大模型论文速递 论文链接基于arXiv Feb. 22, 2022版本阅读 几乎是第一篇气象大模型的工作&#xff0c;同时也是为数不多的对precipitation进行预测的模型。 文章目录 PerformanceStructureFourier transformToken mixing TrainingPrecipitation Model Ensembl…

Android开发:Camera2+MediaRecorder录制视频后上传到阿里云VOD

文章目录 版权声明前言1.Camera1和Camera2的区别2.为什么选择Camera2&#xff1f; 一、应用Camera2MediaPlayer实现拍摄功能引入所需权限构建UI界面的XMLActivity中的代码部分 二、在上述界面录制结束后点击跳转新的界面进行视频播放构建播放界面部分的XMLActivity的代码上述代…

C++如何排查并发编程死锁问题?

C如何排查并发编程死锁问题&#xff1f; 最近在Apache arrow里面写一个支持并行的算子&#xff1a;nested loop join&#xff0c;然后既然涉及到并行&#xff0c;这里就会遇到大家常说的死锁问题&#xff0c;假设你碰到了死锁问题&#xff0c;如何调试与定位呢&#xff1f; 那这…

C程序的编译

经过预处理后的源文件,退去一切包装,注释被删除,各种预处理命令也基本上被处理掉,剩下的就是原汁原味的C代码了。接下来的第二步,就开始进入编译阶段。编译阶段主要分两步:第一步,编译器调用一系列解析工具,去分析这些C代码,将C源文件编译为汇编文件;第二步,通过汇编…