首页 > 其他分享 >Plot函数用法详解——R语言

Plot函数用法详解——R语言

时间:2022-10-29 19:44:33浏览次数:69  
标签:图例 Plot plot mtcars lty 用法 pch 详解 main

plot是R中的基本画图工具,直接plot(x),x为一个数据集,就能画出图,soeasy!但是细节往往制胜的关键,所以就详细来看看plot的所有可设置参数及参数设置方法。R语言的基础绘图系统主要由基础包graphics提供,它包含了各式的图形绘制函数,如折线图、直方图、箱形图等,这里主要介绍plot()函数的用法,它主要用于绘制散点图和折线图。

一、plot函数用法

plot()是常见的作图函数,其中的参数是需要熟悉的,以便在作图过程中更加灵活的处理图形的元素。

plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA, xgap.axis = NA, ygap.axis = NA,   ...)
  • x相当于自变量,y相当于因变量;
  • y没缺省时,必须和x同长度,类型是可以向量化的数据结构,如向量、矩阵的行或列、数组的元素、数据框的列、列表的元素等;
  • y缺省时,x为单列时,y默认为c(1:n),其中n为x的长度;
  • y缺省时,x为两列的矩阵或数据框,则该矩阵或数据框的第一、二列分别对应自变量和因变量。

Arguments

参数 描述
x,y the x and y arguments provide the x and y coordinates for the plot. Any reasonable way of defining the coordinates is acceptable
type character string giving the type of plot desired. The following values are possible
pch a vector of plotting characters or symbols: see points
main a main title for the plot, see also title
sub Sub-title (at bottom) using font, size and color par(c("font.sub", "cex.sub", "col.sub"))
xlab ylab a label for the x axis, defaults to a description of x, y
col the foreground color of symbols as well as lines 用于指定颜色的参数,默认的绘图颜色。某些函数(如lines和pie)可以接受一个含有颜色值的向量并自动循环使用
cex a numerical vector giving the amount by which plotting charactersand symbols should be scaled relative to the default
lty 用于线条类型的定义,指定值为整数,lty="1" lty="0"显示为空白,即无线条; lty="1"显示为实线线条;lty="2"显示为虚线线条;lty="3"显示为点状线条 等

二、plot参数精解

plot函数是一个泛型函数,是R基础绘图中的高级函数,不仅在一般图形绘制中需要用到,在模型的可视化也会用的,这里将Plot()常见的参数做一个讲解。

2.1 type参数设置绘图类型

参数 说明
"p" for points 散点图 默认
"l" for lines 线图
"b" for both 描点连线,点与线不相连
"c" for the lines part alone of "b" 线图,点空白
"o" for both ‘overplotted’ 描点连线,线穿过点
"h" for ‘histogram’ like (or ‘high-density’) vertical lines 从0出发的垂线
"s" for stair steps 阶梯图
"S" for other steps, see ‘Details’ below
"n" for no plotting 空白图,只生成适合的横纵坐标轴
dev.off()
par(mfrow=c(2,4))   #2行4列
mtcars<-mtcars[order(mtcars$wt),]
plot(mtcars$wt,mtcars$disp,type='p',main="type='p'")
plot(mtcars$wt,mtcars$disp,type='l',main="type='l'")
plot(mtcars$wt,mtcars$disp,type='b',main="type='b'")
plot(mtcars$wt,mtcars$disp,type='c',main="type='c'")
plot(mtcars$wt,mtcars$disp,type='o',main="type='o'")
plot(mtcars$wt,mtcars$disp,type='h',main="type='h'")
plot(mtcars$wt,mtcars$disp,type='s',main="type='s'")
plot(mtcars$wt,mtcars$disp,type='S',main="type='S'")

2.2 pch参数设置点符号符号类型

plot函数中还有个pch参数是控制点的类型的,取值意义如下:


dev.off()
par(mfrow=c(2,4))#2行4列
plot(mtcars$wt,mtcars$disp,pch=1,main="pch=1")
plot(mtcars$wt,mtcars$disp,pch=3,main="pch=3")
plot(mtcars$wt,mtcars$disp,pch=5,main="pch=5")
plot(mtcars$wt,mtcars$disp,pch=7,main="pch=7")
plot(mtcars$wt,mtcars$disp,pch=9,main="pch=9")
plot(mtcars$wt,mtcars$disp,pch=11,main="pch=11")
plot(mtcars$wt,mtcars$disp,pch=13,main="pch=13")
plot(mtcars$wt,mtcars$disp,pch=15,main="pch=15")

2.3 lty参数设置线条类型

dev.off()
par(mfrow=c(2,3))
plot(mtcars$wt,mtcars$disp,type='l',lty=1,lwd=2,main="lty=1")
plot(mtcars$wt,mtcars$disp,type='l',lty=2,lwd=2,main="lty=2")
plot(mtcars$wt,mtcars$disp,type='l',lty=3,lwd=2,main="lty=3")
plot(mtcars$wt,mtcars$disp,type='l',lty=4,lwd=2,main="lty=4")
plot(mtcars$wt,mtcars$disp,type='l',lty=5,lwd=2,main="lty=5")
plot(mtcars$wt,mtcars$disp,type='l',lty=6,lwd=2,main="lty=6")

三、plot作图图例legend操作

我们在一个图中画多组对象的时候,这个时候就需要图例来帮助我们读图,比如对下面的图,这个图中有两组数据,但却没有图例,我们不知道三角形和圈圈代表谁:

我们想加一个图例,这个时候就需要继续运行legend函数,比如我想圈圈代表‘关注’,三角代表‘Codewar’,这样就可以写出如下代码,这儿的“关注”和“Codewar”你都可以换成你想的任何字符哈,这里仅用它举例

legend(0,800,
       c("关注","Codewar"), pch=c(19,17), col=c("lightblue","blue"))

图例当然也可以改,首先就是改位置,位置的关键字有9个,对应的位置如下图:

图例的位置可以用关键字改,也可以更加的个性化,用坐标改也是可以的,其可以接受的参数如下图:

比如我想将原来的图例换成红色的背景,然后放在(25,800)这个坐标上,我就可以写出如下代码:

legend(25,800,
       bg = 'red',
       c("关注","Codewar"), pch=c(19,17), col=c("lightblue","blue"))

运行后得到下图:

大家可以看到一个红色背景的图例已经在对应位置加上了,但是仔细观察上图,其实我们现在图是没有边框的,这个时候图例加个边框也不合适,所以我还想设置下图例的边框,甚至我还想改图例中的字体,甚至图例整体的大小,甚至是....统统都是可以的哈,就是这么牛!比如,我现在突发奇想,我想给我的图例加一个标题,再将其变小一点,放在右下角,并且让图例中的字水平排列,我就可以写出如下代码:

legend("bottomright", title="欢迎大家",
       c("关注","Codewar"),col=c("lightblue","blue"), horiz=TRUE, cex=0.4,
       box.lty=0,
       bg = 'red',
       text.font=4
       )

依然是给大家解释下上面代码中各个参数的意思:bottomright是图例位置的关键字,title是标题字符,horiz是图例内容水平排列,cex是图例整体大小,box.lty是图例边框(取0就是无框),text.font是字体(取4就是斜体)

四、示例操作

plot(mtcars)

总结

R语言以其精美的图形而著称,它具有丰富的函数集,可用于构建和格式化任何种类的图形以及plot()功能家族之一,可以帮助我们构建这些家族。用R语言画图,plot函数的使用频率应该是最高了,灵活运用会添光添彩。这些参数即可以在plot()中设置,也可以在一些依赖于plot()函数才能生效的其他函数中进行设置,最后都作用于plot()绘制的图形。

参考文献

R语言绘图基础教程
R语言(4) plot函数介绍
R语言绘图基础教程
R可视化:plot函数基础操作,小白教程

标签:图例,Plot,plot,mtcars,lty,用法,pch,详解,main
From: https://www.cnblogs.com/haohai9309/p/16838013.html

相关文章

  • matlab 中fft的用法
    一.调用方法X=FFT(x);X=FFT(x,N);x=IFFT(X);x=IFFT(X,N)用MATLAB进行谱分析时注意:(1)函数FFT返回值的数据结构具有对称性。例:N=8;n=0:N-1;xn=[43267890];Xk=fft(xn)→Xk......
  • python中for in的用法详解
    forin说明:也是循环结构的一种,经常用于遍历字符串、列表,元组,字典等格式:forxiny:循环体执行流程:x依次表示y中的一个元素,遍历完所有元素循环结束。例1:遍历字符串s='......
  • Emmet 使用详解
    title:Emmet语法使用详解date:2022-5-112:21:13index_img:img/post-img/html.jpgtags:[HTML,VScode]categories:软件配置Emmet使用详解初始化!,html:5......
  • 详解决策树-泰坦尼克号幸存者预测【菜菜的sklearn课堂笔记】
    视频作者:菜菜TsaiTsai链接:【技术干货】菜菜的机器学习sklearn【全85集】Python进阶_哔哩哔哩_bilibiliimportpandasaspdfromsklearn.treeimportDecisionTreeClas......
  • JavaScript--详解&&和--
    一、&&和||的简单介绍1、&&(逻辑与)逻辑与操作符由两个和号(&&)表示。2、||(逻辑或)逻辑或操作符由两个管道符(||)表示。二、深入理解&&和||1、用法&&和||运算符的返回值......
  • Modbus详解
    @目录简介协议特点传输方式ASCII模式RTU模式TCP模式校验方式CRC校验LRC校验功能码简介Modbus协议最初由Modicon公司开发出来,在1979年末该公司成为施耐德自动化(Schneider......
  • 图文详解丨iOS App上架全流程及审核避坑指南
    AppStore作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOSapp上架流程开始梳理,详......
  • 图文详解丨iOS App上架全流程及审核避坑指南
    AppStore作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOSapp上架流程开始梳理,......
  • getline与stringstream的用法
    https://www.acwing.com/problem/content/922/#include<bits/stdc++.h>usingnamespacestd;constintN=505;intn,m;intdist[N],stop[N],q[N];boolg[N]......
  • Cesium的HeadingPitchRange 用法
    这个有别于headingpitchroll,headingpitchroll是用在orientation属性上的(比如相机的setView,flyTo,以及entities.add中)HeadingPitchRange一般用在模型加载之后的定位上,比如v......