R语言的基本图形

news2024/11/26 23:54:17

一,条形图

安装包

install.packages("vcd")

绘制简单的条形图

barplot(c(1,2,4,5,6,3))

 水平条形图

barplot(c(1,2,4,5,6,3),horiz = TRUE)

 堆砌条形图

> d1<-c("Placebo","Treated")
> d2<-c("None","Some","Marked")

> c<-c(29,13,7,7,7,21)

> a<-matrix(c,nrow=3,ncol = 2,byrow = TRUE,dimnames = list(d2,d1))
> a
       Placebo Treated
None        29      13
Some         7       7
Marked       7      21
> barplot(a)

 分组

barplot(a,beside = TRUE)

 均值

> state.region
 [1] South         West          West          South         West          West          Northeast    
 [8] South         South         South         West          West          North Central North Central
[15] North Central North Central South         South         Northeast     South         Northeast    
[22] North Central North Central South         North Central West          North Central West         
[29] Northeast     Northeast     West          Northeast     South         North Central North Central
[36] South         West          Northeast     Northeast     South         North Central South        
[43] South         West          Northeast     South         West          South         North Central
[50] West         
Levels: Northeast South North Central West
> state.x77
               Population Income Illiteracy Life Exp Murder HS Grad Frost   Area
Alabama              3615   3624        2.1    69.05   15.1    41.3    20  50708
Alaska                365   6315        1.5    69.31   11.3    66.7   152 566432
Arizona              2212   4530        1.8    70.55    7.8    58.1    15 113417
Arkansas             2110   3378        1.9    70.66   10.1    39.9    65  51945
California          21198   5114        1.1    71.71   10.3    62.6    20 156361
Colorado             2541   4884        0.7    72.06    6.8    63.9   166 103766
Connecticut          3100   5348        1.1    72.48    3.1    56.0   139   4862
Delaware              579   4809        0.9    70.06    6.2    54.6   103   1982
Florida              8277   4815        1.3    70.66   10.7    52.6    11  54090
Georgia              4931   4091        2.0    68.54   13.9    40.6    60  58073
Hawaii                868   4963        1.9    73.60    6.2    61.9     0   6425
Idaho                 813   4119        0.6    71.87    5.3    59.5   126  82677
Illinois            11197   5107        0.9    70.14   10.3    52.6   127  55748
Indiana              5313   4458        0.7    70.88    7.1    52.9   122  36097
Iowa                 2861   4628        0.5    72.56    2.3    59.0   140  55941
Kansas               2280   4669        0.6    72.58    4.5    59.9   114  81787
Kentucky             3387   3712        1.6    70.10   10.6    38.5    95  39650
Louisiana            3806   3545        2.8    68.76   13.2    42.2    12  44930
Maine                1058   3694        0.7    70.39    2.7    54.7   161  30920
Maryland             4122   5299        0.9    70.22    8.5    52.3   101   9891
Massachusetts        5814   4755        1.1    71.83    3.3    58.5   103   7826
Michigan             9111   4751        0.9    70.63   11.1    52.8   125  56817
Minnesota            3921   4675        0.6    72.96    2.3    57.6   160  79289
Mississippi          2341   3098        2.4    68.09   12.5    41.0    50  47296
Missouri             4767   4254        0.8    70.69    9.3    48.8   108  68995
Montana               746   4347        0.6    70.56    5.0    59.2   155 145587
Nebraska             1544   4508        0.6    72.60    2.9    59.3   139  76483
Nevada                590   5149        0.5    69.03   11.5    65.2   188 109889
New Hampshire         812   4281        0.7    71.23    3.3    57.6   174   9027
New Jersey           7333   5237        1.1    70.93    5.2    52.5   115   7521
New Mexico           1144   3601        2.2    70.32    9.7    55.2   120 121412
New York            18076   4903        1.4    70.55   10.9    52.7    82  47831
North Carolina       5441   3875        1.8    69.21   11.1    38.5    80  48798
North Dakota          637   5087        0.8    72.78    1.4    50.3   186  69273
Ohio                10735   4561        0.8    70.82    7.4    53.2   124  40975
Oklahoma             2715   3983        1.1    71.42    6.4    51.6    82  68782
Oregon               2284   4660        0.6    72.13    4.2    60.0    44  96184
Pennsylvania        11860   4449        1.0    70.43    6.1    50.2   126  44966
Rhode Island          931   4558        1.3    71.90    2.4    46.4   127   1049
South Carolina       2816   3635        2.3    67.96   11.6    37.8    65  30225
South Dakota          681   4167        0.5    72.08    1.7    53.3   172  75955
Tennessee            4173   3821        1.7    70.11   11.0    41.8    70  41328
Texas               12237   4188        2.2    70.90   12.2    47.4    35 262134
Utah                 1203   4022        0.6    72.90    4.5    67.3   137  82096
Vermont               472   3907        0.6    71.64    5.5    57.1   168   9267
Virginia             4981   4701        1.4    70.08    9.5    47.8    85  39780
Washington           3559   4864        0.6    71.72    4.3    63.5    32  66570
West Virginia        1799   3617        1.4    69.48    6.7    41.6   100  24070
Wisconsin            4589   4468        0.7    72.48    3.0    54.5   149  54464
Wyoming               376   4566        0.6    70.29    6.9    62.9   173  97203
> states<-data.frame(state.region,state.x77)
> states
                state.region Population Income Illiteracy Life.Exp Murder HS.Grad Frost   Area
Alabama                South       3615   3624        2.1    69.05   15.1    41.3    20  50708
Alaska                  West        365   6315        1.5    69.31   11.3    66.7   152 566432
Arizona                 West       2212   4530        1.8    70.55    7.8    58.1    15 113417
Arkansas               South       2110   3378        1.9    70.66   10.1    39.9    65  51945
California              West      21198   5114        1.1    71.71   10.3    62.6    20 156361
Colorado                West       2541   4884        0.7    72.06    6.8    63.9   166 103766
Connecticut        Northeast       3100   5348        1.1    72.48    3.1    56.0   139   4862
Delaware               South        579   4809        0.9    70.06    6.2    54.6   103   1982
Florida                South       8277   4815        1.3    70.66   10.7    52.6    11  54090
Georgia                South       4931   4091        2.0    68.54   13.9    40.6    60  58073
Hawaii                  West        868   4963        1.9    73.60    6.2    61.9     0   6425
Idaho                   West        813   4119        0.6    71.87    5.3    59.5   126  82677
Illinois       North Central      11197   5107        0.9    70.14   10.3    52.6   127  55748
Indiana        North Central       5313   4458        0.7    70.88    7.1    52.9   122  36097
Iowa           North Central       2861   4628        0.5    72.56    2.3    59.0   140  55941
Kansas         North Central       2280   4669        0.6    72.58    4.5    59.9   114  81787
Kentucky               South       3387   3712        1.6    70.10   10.6    38.5    95  39650
Louisiana              South       3806   3545        2.8    68.76   13.2    42.2    12  44930
Maine              Northeast       1058   3694        0.7    70.39    2.7    54.7   161  30920
Maryland               South       4122   5299        0.9    70.22    8.5    52.3   101   9891
Massachusetts      Northeast       5814   4755        1.1    71.83    3.3    58.5   103   7826
Michigan       North Central       9111   4751        0.9    70.63   11.1    52.8   125  56817
Minnesota      North Central       3921   4675        0.6    72.96    2.3    57.6   160  79289
Mississippi            South       2341   3098        2.4    68.09   12.5    41.0    50  47296
Missouri       North Central       4767   4254        0.8    70.69    9.3    48.8   108  68995
Montana                 West        746   4347        0.6    70.56    5.0    59.2   155 145587
Nebraska       North Central       1544   4508        0.6    72.60    2.9    59.3   139  76483
Nevada                  West        590   5149        0.5    69.03   11.5    65.2   188 109889
New Hampshire      Northeast        812   4281        0.7    71.23    3.3    57.6   174   9027
New Jersey         Northeast       7333   5237        1.1    70.93    5.2    52.5   115   7521
New Mexico              West       1144   3601        2.2    70.32    9.7    55.2   120 121412
New York           Northeast      18076   4903        1.4    70.55   10.9    52.7    82  47831
North Carolina         South       5441   3875        1.8    69.21   11.1    38.5    80  48798
North Dakota   North Central        637   5087        0.8    72.78    1.4    50.3   186  69273
Ohio           North Central      10735   4561        0.8    70.82    7.4    53.2   124  40975
Oklahoma               South       2715   3983        1.1    71.42    6.4    51.6    82  68782
Oregon                  West       2284   4660        0.6    72.13    4.2    60.0    44  96184
Pennsylvania       Northeast      11860   4449        1.0    70.43    6.1    50.2   126  44966
Rhode Island       Northeast        931   4558        1.3    71.90    2.4    46.4   127   1049
South Carolina         South       2816   3635        2.3    67.96   11.6    37.8    65  30225
South Dakota   North Central        681   4167        0.5    72.08    1.7    53.3   172  75955
Tennessee              South       4173   3821        1.7    70.11   11.0    41.8    70  41328
Texas                  South      12237   4188        2.2    70.90   12.2    47.4    35 262134
Utah                    West       1203   4022        0.6    72.90    4.5    67.3   137  82096
Vermont            Northeast        472   3907        0.6    71.64    5.5    57.1   168   9267
Virginia               South       4981   4701        1.4    70.08    9.5    47.8    85  39780
Washington              West       3559   4864        0.6    71.72    4.3    63.5    32  66570
West Virginia          South       1799   3617        1.4    69.48    6.7    41.6   100  24070
Wisconsin      North Central       4589   4468        0.7    72.48    3.0    54.5   149  54464
Wyoming                 West        376   4566        0.6    70.29    6.9    62.9   173  97203
> x<-aggregate(states$Illiteracy,by=list(state.region),FUN=mean)
> x
        Group.1        x
1     Northeast 1.000000
2         South 1.737500
3 North Central 0.700000
4          West 1.023077
> barplot(x$x,names.arg = x$Group.1)

 条形图的微调

 par(mar=c(5,8,4,2))

> a
       Placebo Treated
None        29      13
Some         7       7
Marked       7      21

> barplot(a,horiz = TRUE,cex.names = 0.8,names.arg = c("Noimproved","someimproved"))

标签水平

> par(las=2)
> barplot(a,horiz = TRUE,cex.names = 0.8,names.arg = c("Noimproved","someimproved"))

 二,饼图

绘制简单的饼图

> par(mfrow=c(2,2))
> x<-c(10,12,4,16,8)
> lab<-c("US","UK","AUS","GMY","FRC")
> pie(x,lab,main="袁震")

有百分比的饼图

> pct<-round(x/sum(x)*100)


> pct
[1] 18 22  7 29 15

> lab1<-paste(lab," ",pct,"%",seq=" ")
> lab1
[1] "US   18 %  "  "UK   22 %  "  "AUS   7 %  "  "GMY   29 %  " "FRC   15 %  "
> pie(x,lab1,col=rainbow(length(lab1)),main = "袁震2")

3D饼图

> library(plotrix)
> pie3D(x,explode = 0.1,main="袁震3")

扇形

> fan.plot(x,labels = lab,main = "袁震4")

三,直方图

> x<-mtcars$mpg
> x
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9
[21] 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> hist(x)

> hist(x)
> hist(x,breaks = 12,col = "red",xlab = "袁震")

 

> hist(x,freq = FALSE,breaks = 12,col="green",xlab = "袁震")
> rug(jitter(x))
> lines(density(x),col="red",lwd=2)

 四,核密度图

> x<-density(mtcars$mpg)
> mtcars$mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9
[21] 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> x

Call:
	density.default(x = mtcars$mpg)

Data: mtcars$mpg (32 obs.);	Bandwidth 'bw' = 2.477

       x               y            
 Min.   : 2.97   Min.   :6.481e-05  
 1st Qu.:12.56   1st Qu.:5.461e-03  
 Median :22.15   Median :1.926e-02  
 Mean   :22.15   Mean   :2.604e-02  
 3rd Qu.:31.74   3rd Qu.:4.530e-02  
 Max.   :41.33   Max.   :6.795e-02  
> plot(x)

> library(sm)
Package 'sm', version 2.2-6.0: type help(sm) for summary information
> mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9
[21] 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> cyl
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> sm.density.compare(mpg,cyl,xlab="袁震")

 五,箱线图

> boxplot(mtcars$mpg,main="袁震",ylab="Miles per gallon")

boxplot(mpg~cyl,data = mtcars,main="袁震",xlab = "Number of cylinders",ylab = "Miles per gallon")

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

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

相关文章

聚类分析:使用R语言对Iris数据集进行K均值聚类

引言 聚类分析是一种常用的无监督学习技术&#xff0c;旨在将数据集中的样本分成具有相似特征的组。K均值聚类是其中一种常见的方法&#xff0c;它通过将数据点划分为K个簇&#xff0c;并使每个数据点与其所属簇的中心点距离最小化来实现聚类。本文将介绍如何使用R语言执行K均…

matlab求时间序列的时间滞后相关性

matlab求时间序列的时间滞后相关性 自相关、互相关、加权相关、滞后相关等相关性分析&#xff0c;在时间序列分析中经常被用到&#xff0c;可以量化两个时间序列的相关程度&#xff0c;特别对于有季节性趋势的序列中这个分析尤为必要。下面介绍一个Matlab函数&#xff0c;用于进…

FPGA实现图像处理之【直方图均衡-寄存器版】

FPGA实现直方图统计 一、图像直方图统计原理 直方图的全称为灰度直方图&#xff0c;是对图像每一灰度间隔内像素个数的统计。即对一张图片中每隔二灰度值的像素数量做统计&#xff0c;然后以直方图的形式展现出来。图下的亮暗分布在直方图中就可以一目了然&#xff0c;直方图…

分布式系统事务一致性解决方案(基于事务消息)

参考&#xff1a;https://rocketmq.apache.org/zh/docs/featureBehavior/04transactionmessage/ 文章目录 概要错误的方案方案一&#xff1a;业务方自己实现方案二&#xff1a;RocketMQ 事务消息什么是事务消息事务消息处理流程事务消息生命周期使用限制使用示例使用建议 概要 …

WPF —— MVVM 指令执行不同的任务实例

标签页 设置两个按钮&#xff0c; <Button Content"修改状态" Width"100" Height"40" Background"red"Click"Button_Click"></Button><Button Content"测试"Width"100"Height"40&…

java案例-读取xml文件

需求 导入依赖 <dependencies><!-- dom4j --><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency> </dependencies>代码 SAXReader saxReade…

BERT一个蛋白质-季军-英特尔创新大师杯冷冻电镜蛋白质结构建模大赛-paipai

关联比赛: “创新大师杯”冷冻电镜蛋白质结构建模大赛 解决方案 团队介绍 paipai队、取自 PAIN AI&#xff0c;核心成员如我本人IvanaXu(IvanaXu GitHub)&#xff0c;从事于金融科技业&#xff0c;面向银行信用贷款的风控、运营场景。但我们团队先后打过很多比赛&#xf…

Vue后台系统demo小计

创建项目 1.报错 Error: command failed: npm install --loglevel error --legacy-peer-deps 措施1&#xff1a;node.js文件夹属性 》高级 》选择第一个允许 Users(XXX\Users) &#xff08;对我无用&#xff09; 措施2&#xff1a;PowerShell(以管理员身份运行) 》 cd 想存…

C++ | Leetcode C++题解之第55题跳跃游戏

题目&#xff1a; 题解&#xff1a; class Solution { public:bool canJump(vector<int>& nums) {int n nums.size();int rightmost 0;for (int i 0; i < n; i) {if (i < rightmost) {rightmost max(rightmost, i nums[i]);if (rightmost > n - 1) {r…

【Leetcode每日一题】 动态规划 - 简单多状态 dp 问题 - 打家劫舍 II(难度⭐⭐)(67)

1. 题目解析 题目链接&#xff1a;213. 打家劫舍 II 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 这个问题是经典的“打家劫舍”问题的变种&#xff0c;原问题是在单排房屋中进行偷窃&#xff0c;而这个问题则是在…

利用Triple U.Net结构对冷冻切片HE染色组织学图像进行核实例分割

利用Triple U.Net结构对冷冻切片H&E染色组织学图像进行核实例分割 摘要IntroductionRelated WorksDatasetProposed MethodologyDataset PreparationSegmentation BranchLoss FunctionWatershed Algorithm Nuclei Instance Segmentation of Cryosectioned H&E Stained H…

JavaScript全套检验系统(LIS)源码C# + MVC + SQLserver + Redis 云LIS系统源码 区域医疗云LIS系统源码

JavaScript全套检验系统&#xff08;LIS&#xff09;源码C# MVC SQLserver Redis 云LIS系统源码 区域医疗云LIS系统源码 实验室信息系统&#xff08;Laboratory Information System&#xff0c;缩写LIS&#xff09;是一类用来处理实验室过程信息的软件。这套系统通常与其他信…

ArcGIS基础:便捷分享图层包和地图包

1、分享图层包&#xff1a; 首先&#xff0c;选中要分享的数据&#xff0c;右键创建图层包&#xff0c;修改保存路径。 找到项目描述那一栏&#xff0c;将摘要、标签、描述都填写分享图层包的相关内容。 一切设置好之后&#xff0c;点击右上角的【分析】按钮。 点击分析之后…

vue2集成ElementUI编写登录页面

目录 1. 整理目录文件&#xff1a; a. app.vue文件如下&#xff1a; b. Login.vue文件如下&#xff1a; c. router/index.js文件如下&#xff1a; d. 删除components中的文件&#xff1a; e. 最终项目目录整理如下&#xff1a; 2. 集成ElementUI编写登录页面 a. 安装El…

MCU自动测量单元:自动化数据采集的未来

随着科技的飞速发展&#xff0c;自动化技术在各个领域中的应用日益广泛。其中&#xff0c;MCU(微控制器)自动测量单元以其高效、精准的特性&#xff0c;成为自动化数据采集领域的佼佼者&#xff0c;引领着未来数据采集技术的革新。本文将深入探讨MCU自动测量单元的原理、优势以…

详解进程控制

目录 一、进程创建 fork() 写时拷贝 fork的应用场景 二、进程退出 什么是进程退出码&#xff1f; 退出码的含义 进程退出方法 三、进程等待 进程等待的必要性 进程等待的方法 wait waitpid status 阻塞与非阻塞 四、进程替换 替换原理 替换函数 命名理解 简…

大数据005-hadoop003-了解MR及Java的简单实现

了解MapReduce MapReduce过程分为两个阶段&#xff1a;map阶段、reduce阶段。每个阶段搜键-值对作为输入和输出。 要执行一个MR任务&#xff0c;需要完成map、reduce函数的代码开发。 Hellow World 【Hadoop权威指南】中的以分析气象数据为例&#xff0c;找到每年的最高气温。…

【论文笔记】Language Models are Few-Shot Learners B部分

Language Models are Few-Shot Learners B 部分 回顾一下第一代 GPT-1 &#xff1a; 设计思路是 “海量无标记文本进行无监督预训练少量有标签文本有监督微调” 范式&#xff1b;模型架构是基于 Transformer 的叠加解码器&#xff08;掩码自注意力机制、残差、Layernorm&#…

Spark核心名词解释与编程

Spark核心概念 名词解释 1)ClusterManager&#xff1a;在Standalone(上述安装的模式&#xff0c;也就是依托于spark集群本身)模式中即为Master&#xff08;主节点&#xff09;&#xff0c;控制整个集群&#xff0c;监控Worker。在YARN模式中为资源管理器ResourceManager(国内…

STM32定时器的OC比较和PWM

系列文章目录 STM32单片机系列专栏 C语言术语和结构总结专栏 文章目录 1. 输出比较(OC) 2. PWM 3. PWM的输出 3.1 高级定时器 3.2 通用定时器 4. PWM的输出结构 5. 代码示例 5.1 PWM.c 5.2 PWM.h 5.3 main.c 这篇文章解释了TIM定时器的内部时钟和外部时钟的使用&a…