评论

收藏

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

编程语言 编程语言 发布于:2021-06-30 10:15 | 阅读数:620 | 评论:0

  坐标轴的间距刻度范围设置
  1.区间随机生成坐标刻度间距,坐标轴的间距刻度是自动随机设置:xlim和ylim的区间进行设置在[-20.20]
plot(1:30,xlim=c(-20,20),ylim=c(-20,20))
DSC0000.png

  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)
DSC0001.png
用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")
DSC0002.png
然后,自己设置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轴坐标刻度
DSC0003.png

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轴")
DSC0004.png
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)
DSC0005.png

标题增加和加粗
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="测试")
DSC0006.png

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

同一图形中增加新的曲线
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)
DSC0008.png

绘图给设置增加双坐标轴
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)
DSC0009.png

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

坐标轴上单独加函数: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")
DSC00011.png

图形加外框并注解该图含义
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)
DSC00012.png

自定义函数的轨迹曲线
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)
DSC00013.png

添加水平虚线、法线和标记轴线
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"))
DSC00014.png

添加水平虚线和文本
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"))
DSC00015.png

图形添加文字方法
  给图形中的点加上文本标签。
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)
DSC00016.png

图形上添加数字标签
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))
DSC00017.png


  
关注下面的标签,发现更多相似文章