R语言可视化的图例设置和绘图技巧设置总结

77 篇文章 17 订阅
14 篇文章 10 订阅
坐标轴的间距刻度范围设置

1.区间随机生成坐标刻度间距,坐标轴的间距刻度是自动随机设置:xlim和ylim的区间进行设置在[-20.20]

plot(1:30,xlim=c(-20,20),ylim=c(-20,20))

在这里插入图片描述

2.自定义设置坐标刻度间距

自己设置刻度步骤:
1.首先把plot里面默认的坐标轴关掉,参数是xaxt=‘n’;
2.其次,做完plot以后通过axis函数手动添加坐标轴;

例举原图像绘制效果如下:

a<-16:25
b<-c(57,81,86,89,91,126,139,155,200,211)
plot(a,b,type="b",xlab="网络节点数目 x1000",ylab="算法时间消耗",col="blue",xlim=c(16,25),ylim=c(57,211),pch=15)

在这里插入图片描述
用xaxt=“n”,yaxt="n"关掉之前存在的坐标轴的刻度

plot(a,b,type="b",xlab="网络节点数目 x1000",ylab="算法时间消耗",col="blue",xlim=c(16,25),ylim=c(57,211),pch=15,xaxt="n",yaxt="n")

在这里插入图片描述
然后,自己设置X轴,Y轴坐标刻度

axis(1,a)    #设置X轴坐标刻度
axis(2,b)    #设置Y轴坐标刻度

等间距的设置坐标轴的长度:如果想要将16到25,用10个数字刻度描述,每个刻度间的间隔是2.5一个单位长度,方法如下所示:

m=seq(from = 16, to = 25, by=2.5)  ## 先生成等差数列的刻度,间距为2.5
plot(a,b,type="b",xlab="网络节点数目 x1000",ylab="算法时间消耗",col="blue",xlim=c(16,25),ylim=c(57,211),pch=15)
plot(a,b,type="b",xlab="网络节点数目 x1000",ylab="算法时间消耗",col="blue",xlim=c(16,25),ylim=c(57,211),pch=15,xaxt="n",yaxt="n")
axis(1,m)    #设置X轴坐标刻度
axis(2,b)    #设置Y轴坐标刻度

**加粗样式**

cex.lab参数对坐标轴的标签大小设定、轴的数字大小设定
par(mfrow = c(1, 2))
plot(1:30,xlim=c(-20,20),ylim=c(-20,20),xlab="X轴",ylab="Y轴")
plot(1:30,xlim=c(-20,20),ylim=c(-20,20),cex.lab=1.8,xlab="X轴",ylab="Y轴")

在这里插入图片描述

par(mfrow = c(1, 2))
plot(1:30,xlim=c(-20,20),ylim=c(-20,20))
plot(1:30,xlim=c(-20,20),ylim=c(-20,20),cex.axis=2)

在这里插入图片描述

标题增加和加粗
par(mfrow = c(1, 2))
plot(1:30,xlim=c(-20,20),ylim=c(-20,20),xlab="X轴",ylab="Y轴",main="测试")
plot(1:30,xlim=c(-20,20),ylim=c(-20,20),cex.main=4,xlab="X轴",ylab="Y轴",main="测试")

在这里插入图片描述

绘图边框和坐标轴显示隐藏设置
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")   # 边框、坐标轴都去掉

在这里插入图片描述

同一图形中增加新的曲线
x <- c(1:10)  
y <- x  
z <- 10/x  
z
opar <- par(no.readonly=TRUE)  
par(mar = c(5,4,4,8)+0.1)  
plot(x, y, type="b",  
     pch = 21, col = "red",  
     yaxt = "n", lty = 3, ann = FALSE)  
lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)  

在这里插入图片描述

绘图给设置增加双坐标轴
x <- c(1:10)  
y <- x  
z <- 10/x  
z
opar <- par(no.readonly=TRUE)  
par(mar = c(5,4,4,8)+0.1)  
plot(x, y, type="b",  
     pch = 21, col = "red",  
     yaxt = "n", lty = 3, ann = FALSE)  
lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)  
axis(2, at = x, labels = x, col.axis = "red", las = 2)  
axis(4, at = x, labels = round(z, digits = 2),  
     col.axis = "blue", las = 2, cex.axis = 0.7, tck = -0.03)  

在这里插入图片描述

mgp参数调整坐标轴的距离位置移动
x=c(1:10)
y=c(1:10)
plot(x,y,type="b",frame=FALSE,mgp=c(3,2,1))

在这里插入图片描述

坐标轴上单独加函数:y = 1/x

a<-16:25
b<-c(57,81,86,89,91,126,139,155,200,211)
opar <- par(no.readonly=TRUE)  
par(mar = c(5,4,4,8)+0.1)  
plot(a,b,type="b",xlab="网络节点数目 x1000",ylab="算法时间消耗",col="blue",xlim=c(16,25),ylim=c(57,211),pch=15,ann = FALSE)
mtext("y = 1/x", side = 4, line = 3, cex.lab = 2, las = 2, col = "blue")  

在这里插入图片描述

图形加外框并注解该图含义
par(oma=c(3,3,3,3),mar=c(5,10,5,5),mgp=c(3,2,1),bty="o",xaxt='n')
plot(1:10,1:10,xlab="x",ylab="y",main="ILoveLittree",cex.axis=0.8,cex.lab=2)
box("inner", lty="dotted", col="red")
box("outer", lty="solid", col="blue")
mtext("注解:I am happy", side=1, cex.lab=1.3, line=1, cex=1.2,font.axis=2,outer=TRUE,adj=0)

在这里插入图片描述

自定义函数的轨迹曲线
x=seq(from=-5,to=5,by=0.01)
h=function(b){
  w=(1+b^2)/(1+(b-1)^2)
  2*log(w)
}
y=h(x)
plot(x,y,type='l', axes = FALSE)

在这里插入图片描述

添加水平虚线、法线和标记轴线
h=function(b){
  w=(1+b^2)/(1+(b-1)^2)
  2*log(w)
}
x=seq(from=-5,to=5,by=0.01)
y=h(x)
plot(x,y,type='l', axes = FALSE)
arrows(-0.25,-1.76, -5, -1.76, length = 0,lty = 2)
arrows(1.25,1.76, 5, 1.76, length = 0,lty = 2)
arrows(-0.25,-2.1, -0.25, 2, length = 0,lty = 3)
arrows(1.25, -2.1, 1.25, 2, length = 0,lty = 3)
axis(2)
axis(1, at = c(-5, -0.25, 1.25, 5),
     labels = c("Y1", "Xo", "X1", "Y2"))

在这里插入图片描述

添加水平虚线和文本
plot(1:5, 1:5, type = "n", xlab = "X", ylab = "Y")
abline(h=c(3,4),v=c(3,4),lty=3,col="lightgray")
text(c(3,4),c(3,4),c("焦点中心1","焦点中心2"))

在这里插入图片描述

图形添加文字方法

给图形中的点加上文本标签。

par(mfrow = c(1, 2))
attach(mtcars)
plot(wt,mpg,main = 'Mileage vs. Car Weight',xlab = 'Weight',ylab = 'Mileage',pch=18,col='blue')
plot(wt,mpg,main = 'Mileage vs. Car Weight',xlab = 'Weight',ylab = 'Mileage',pch=18,col='blue')
text(wt,mpg,row.names(mtcars),cex = 0.6,pos=4,col='red')
detach(mtcars)

在这里插入图片描述

图形上添加数字标签
data1 <- data.frame(A = 1:2, B = 1:2, C = 1:2)
data <- data.matrix(data1)
par(font = 2, lwd = 2)
bar=barplot(data, col = c("black", "pink"), beside = T, ylim = c(0, 2.5), font = 2)
text( bar, y = 0.05 + data[,1], adj = c(0.5, 0))

在这里插入图片描述

  • 12
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值