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

实验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.mzph.cn/news/814197.shtml

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

相关文章

python-study-day1

ps&#xff1a;前言 可做毕设&#xff0c;html&#xff0c;web&#xff0c;app&#xff0c;小程序&#xff0c;bug修改&#xff0c;可加急 作者自述 作为一名前端开发工程师&#xff0c;这个大环境不好的情况下&#xff0c;我试过我前端接单子但是没有后端&#xff0c…

用java实现单链表的头插,尾插和反转

今天来练习以下单链表的一些操作&#xff0c;以下的操作都是带有头节点的链表。 定义链表节点类 定义了节点中的值&#xff0c;节点的下一个节点&#xff0c;和一些基本的方法。 public static class ListNode{int val;ListNode next;public ListNode() {}public ListNode(in…

NPM 命令备忘单

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

pom.xml显示灰色并被划线

在使用 IDEA 进行开发的过程中&#xff0c;有时候会遇到 pom.xml 显示灰色并被划线的情况&#xff0c;如下图&#xff1a; 这一般是因为该文件被 Maven 忽略导致的&#xff0c;可以进行如下操作恢复&#xff1a; 设置保存后&#xff0c;可以看到 pom.xml 恢复了正常&#xff1a…

计算机网络书籍--《网络是怎样连接的》阅读笔记

第一章 浏览器生成信息 1.1 生成HTTP请求信息 1.1.1 URL Uniform Resource Locator, 统一资源定位符。就是网址。 不同的URL能够用来判断使用哪种功能来访问相应的数据&#xff0c;比如访问Web服务器就要用”http:”&#xff0c;而访问FTP服务器用”ftp:”。 FTP&#xff…

支付系统核心逻辑 — — 状态机(JavaGolang版本)

支付系统核心逻辑 — — 状态机 代码地址&#xff1a;https://github.com/ziyifast/ziyifast-code_instruction/tree/main/state_machine_demo 1 概念&#xff1a;FSM&#xff08;有限状态机&#xff09;&#xff0c;模式之间转换 状态机&#xff0c;也叫有限状态机&#xff08…

基于OptiTrack跟踪系统和Turtlebot机器人的视觉SLAM定位评估

本文旨在介绍使用OptiTrack光学跟踪系统和Turtlebot机器人进行视觉SLAM定位实验的详细流程&#xff0c;包括实验平台搭建过程、数据处理过程以及SLAM估计评估方法。由于涉及知识较多&#xff0c;部分内容只给出了相关参考博文链接。 1 实验平台搭建 实验平台包括OptiTrack光学…

Golang | Leetcode Golang题解之第26题删除有序数组中的重复项

题目&#xff1a; 题解&#xff1a; func removeDuplicates(nums []int) int {n : len(nums)if n 0 {return 0}slow : 1for fast : 1; fast < n; fast {if nums[fast] ! nums[fast-1] {nums[slow] nums[fast]slow}}return slow }

window10轻松使用k8s

Docker Desktop安装篇 1、win10安装 1、下载安装包 https://www.docker.com/products/docker-desktop/ 官网下载安装包 2、配置win10支持虚拟化 不勾选Hyper-V&#xff0c;容易出错 3、安装WSL配置window支持linux Windows Subsystem for Linux (WSL) 安装 Linux 子系统&am…

嵌入式sqlite3交叉编译移植

操作系统:Ubuntu20.04 下载sqlite3代码,下载版本3.30.00 wget https://www.sqlite.org/2019/sqlite-amalgamation-3300000.zip 或者https://download.csdn.net/download/benico/89127678 为什么下载amalgamation版本,不下载autoconf版本? 根据我的编译实验,同版本sql…

python爬取京东商品信息与可视化

项目介绍&#xff1a;使用python爬取京东电商拿到价格、店铺、链接、销量并做可视化 ........................................................................................................................................................... 项目介绍效果展示全部…

数据库系统概论(超详解!!!)第四节 数据库安全性

问题的提出&#xff1a; 数据库的一大特点是数据可以共享 数据共享必然带来数据库的安全性问题 数据库系统中的数据共享不能是无条件的共享。 1.数据库的安全概述 数据库的安全性是指保护数据库以防止不合法使用所造成的数据泄露、更改或破坏 。 系统安全保护措施是否有效…

C语言:约瑟夫环问题详解

前言 哈喽&#xff0c;宝子们&#xff01;本期为大家带来一道C语言循环链表的经典算法题&#xff08;约瑟夫环&#xff09;。 目录 1.什么是约瑟夫环2.解决方案思路3.创建链表头结点4.创建循环链表5.删除链表6.完整代码实现 1.什么是约瑟夫环 据说著名历史学家Josephus有过以下…

C/C++基础----判断和循环

判断 if-elseif-else判断 语句&#xff1a; 条件使用之前的逻辑运算符或者关系运算符 if(条件1){条件1成立时内容 }else if(条件2){条件2成立时内容 }else{所有条件不成立时内容 }#include <iostream>using namespace std;int main() {int age 10;if (age > 18) {c…

【C Hash Map from Redis】

将Redis源码中的哈希表底层逻辑提取&#xff0c;并进行最小demo级测试将对应文件抽出&#xff0c;通过宏替换等方式保证源码编译通过main.c编写测试demo &#xff0c;注册哈希函数和值比较函数&#xff08;必选项&#xff09; /* Hash Tables Implementation.** This file imp…

Java 原生代码获取服务器的网卡 Mac 地址、CPU序列号、主板序列号

1、概述 Java 可以获取服务器的网卡 Mac 地址、CPU 序列号、主板序列号等信息&#xff0c;用来做一些软件授权验证、设备管理等场景。 2、代码实现 package com.study.util;import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Scanner;/*** …

elasticsearch7安全配置--最低安全等级,用户名密码

上一篇博客在centos7上安装了elasticsearch7 接下来对elasticsearch进行安全方面的配置 minimal security 最低安全等级&#xff0c;用户名密码 首先开启xpack vim config/elasticsearch.yml xpack.security.enabled: true由于我是单机配置的&#xff0c;还加了如下配置 d…

SAP HCM GET pernr无法获取到数据二

今天遇到一个比较奇怪的问题&#xff0c;PA30能查到员工主数据&#xff0c;任何信息类型也没有错误&#xff0c;但是核算工资的时候发现无法找到此人。 但是核算工资无法核算 断点到逻辑数据get pernr&#xff0c;也不会进入断点 查看0000数据有间隔 具体错误的代码位置如下&am…

从 iPhone 上的短信中恢复已删除的图片的可靠方法

您可能在浏览消息聊天时不小心删除了一些文本和照片。事实上&#xff0c;如果这些消息对你来说意义重大&#xff0c;那对你来说可能会很麻烦。当发生意外情况时&#xff0c;您可能不想恢复整个聊天&#xff0c;而是恢复其中的附件。 好了&#xff0c;这篇文章主要是讲如何灵活…

Towards Street-Level Client-Independent IP Geolocation(2011年)(第二部分)

被引次数:306 Wang Y, Burgener D, Flores M, et al. Towards {Street-Level}{Client-Independent}{IP} Geolocation[C]//8th USENIX Symposium on Networked Systems Design and Implementation (NSDI 11). 2011. 接着Towards Street-Level Client-Independent IP Geolocati…