当前位置:网站首页>R语言作图:坐标轴设置
R语言作图:坐标轴设置
2022-07-23 17:56:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
要绘制一张赏心悦目的统计图表,坐标轴的设置至关重要。在R语言底层作图中,对坐标轴的调整主要通过调整plot函数、axis函数和title函数的一系列参数完成。
plot(x,y, …)
axis(side,at = NULL, labels = TRUE, tick = TRUE, line = NA,
pos= NA, outer = FALSE, font = NA, lty = “solid”,
lwd = 1, lwd.ticks = lwd, col = NULL,col.ticks = NULL,
hadj = NA, padj = NA, …)
title(main= NULL, sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE, …)
一、plot函数的准备
在个性化设置坐标轴之前中,通常需调整plot函数中的ann、bty、xaxt、yaxt、xaxs和yaxs参数:
ann取FALSE时将不会画出标题(包括主、副标题及坐标轴标题);
bty用来设置边框形式,默认值为”o”,表示四面边框都画出,其余可选值包括”l”(左下)、”7″(上右)、”c”(上下左)、”u”(左下右)、”]” (上下右)和”n”(无,即不画边框),在很多个性化绘图中,bty设为”n”,后期的边框线再使用其他函数(如axis)自行添加;
xaxs和yaxs 用来设置x轴和y轴的范围,默认值取“r”,表示坐标轴比给定作图范围(参数xlim和ylim给出的范围)稍微大一点儿,取”i”时表示坐标轴范围与给定作图范围完全相同,另外还可取”s”、”e”、”d”;
xaxt和yaxt 取”n”时,坐标轴、刻度线以及刻度值将不会画出。
x <- seq(-4, 4, 0.01)
y <- x^2
par(mfrow = c(2, 2), mar = c(4, 4, 1, 1))
plot(x, y) # 未作处理
plot(x, y, xaxs = "i", yaxs ="i") # 绘图边框未留白
plot(x, y, bty = 'l') # 只保留左和下两条边框
plot(x, y, ann = F, bty = "n", xaxt = "n", yaxt ="n") # 边框、坐标轴都去掉二、axis函数的用法
1、基本操作
side表示要操作的坐标轴,取值1、2、3、4分别代表下、左、上、右;
at表示刻度线及刻度值所在位置;
labels表示刻度值;
las表示坐标刻度值文字方向,las=0表示文字方向与坐标轴平行,1表示始终为水平方向,2表示与坐标轴垂直,3表示终为垂直方向。
x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(1, 2), mar = c(4, 4, 1, 1))
#未设置文字方向
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
axis(2,seq(0, 16, 4), seq(0, 16, 4))
#文字方向水平
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)2、字体字号
cex.axis表示坐标轴刻度值的字号大小,
font.axis表示坐标轴刻度值的字体,font=1表示正体,2表示黑体,3表示斜体,4表示黑斜体。
x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
#未设置字体字号
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
#cex.axis = 2
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, cex.axis = 2)
#font.axis = 2
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, font.axis = 2)
#font.axis = 3
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, font.axis = 3)3、颜色
col表示图的颜色,用在axis函数中表示坐标轴线和坐标刻度线的颜色;
col.axis表示坐标轴刻度值的颜色;
col.ticks表示坐标轴刻度线的颜色。
x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
#未设置颜色
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
#col = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col = 2)
#col.axis = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col.axis = 2)
#col.ticks = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col.ticks = 2)4、位置
line表示坐标轴线位置与图像边框的距离,取负数时会画在图像边框以内;
mgp默认值为c(3, 1, 0),三个数字分别代表坐标轴标题、刻度值和轴线与绘图边框的距离;
tcl默认值为-0.5,数值表示刻度线长度,负值表示刻度线朝外,正值朝里;
pos 表示轴线所在的位置;
line.outer取TRUE时,坐标轴将画在画布边缘处;
hadj指将刻度值沿平行坐标轴方向调整的距离;
padj指将刻度值沿垂直坐标轴方向调整的距离。
x <- seq(-4, 4, 0.01)
y <- x^2
par(mfrow= c(2, 2), mar = c(4, 4, 1, 1))
# 未设置刻度值位置
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4)
# 使用line调整刻度值位置
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, line = 2)
# 使用mgp调整刻度值位置
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, mgp = c(3, 2, 0))
# 使用padj调整刻度值位置
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, padj = 1)5、其他
tick取FALSE时,坐标轴线和刻度线不画出;
lty表示线型,用在axis函数中表示坐标轴线型;
lwd表示线的粗细,用在axis函数中表示坐标轴线粗细;
lwd.ticks表示刻度线粗细。
三、title函数的用法
main、sub、xlab和ylab分别表示主标题、副标题、x轴标题和y轴标题;
cex.lab表示坐标轴标题的字号大小;
font.lab表示坐标轴标题的字体;
col.lab表示坐标轴标题的颜色;
其余参数和axis中用法一致。
x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
#无标题
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
axis(2,seq(0, 16, 4), seq(0, 16, 4))
#使用title设置标题
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y')
#使用line调整位置
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y', line = 2)
#调整颜色字体字号
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y', col.lab = 2, font.lab = 4, cex.lab = 2)四、刻度间隔
plot函数会自动给出比较合理的刻度间隔,本质上是调用了pretty函数。
pretty(x, n = 5, min.n = n %/% 3, shrink.sml = 0.75, high.u.bias = 1.5, u5.bias = .5 + 1.5*high.u.bias, eps.correct = 0, …)
x是一个序列,pretty函数会先对x取一个range,然后等分为大约n+1个间隔。如果在作图时对系统自动给出的间隔不满意,可以调整n的值,用pretty函数自定义合适的间隔。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126792.html原文链接:https://javaforall.cn
边栏推荐
- 树莓派ssh登录
- Little fish sends lidar | just dinner is the first lottery
- Four principles of interface design
- Sui of the public chain (New Public chain project established by former Facebook /meta employees)
- TCL scripting language foundation (2)
- R语言使用tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数指定不变的列名称列表
- @JPA annotation in entity
- 当代励志“女”录
- 【luogu P6656】【LOJ 173】【模板】Runs(字符串)(Lyndon 串)
- Monotonic queue optimization DP
猜你喜欢

Codeforces Round #805-#808【部分题解】

Alibaba最新神作!耗时187天肝出来1015页分布式全栈手册太香了

lendingclub贷款状态loan status业务详解-current,charge off,issued,Fully Paid,grace period

AE tutorial, how to animate illustrator layered documents in after effects?

吃透Chisel语言.21.Chisel时序电路(一)——Chisel寄存器(Register)详解

将本地镜像发布到阿里云仓库

去中心化存储面临的挑战

Type-C Bluetooth speaker single C-Port rechargeable OTG solution

Weights & Biases (一)

PowerCLi 将虚拟机从Host01主机移动到Host02主机
随机推荐
Shell
【C语言】程序环境和预处理
How can mysqldump export content without comments?
移动语义和完美转发浅析
What content does the software test plan include and how to write it. Share test plan template
【leetcode天梯】链表 · 206 反转链表
小鱼送激光雷达啦 | 恰饭即抽奖第一期
Monotonic queue optimization DP
数据链路层 -------- 以太网 和 ARP
TCL scripting language foundation (2)
not all arguments converted during string formatting
安全停止nodeos
Little fish sends lidar | just dinner is the first lottery
入门数据库Days3
Shell
R语言使用dwilcox函数生成Wilcoxon秩和统计分布密度函数数据、使用plot函数可视化Wilcoxon秩和统计分布密度函数数据
Fragment
LeetCode刷题:回文数
elk筆記25--快速體驗APM
Four principles of interface design