Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基线R中的绘图存在小问题-绘图顶部显示不需要的线_R_Plot_Border_Base - Fatal编程技术网

基线R中的绘图存在小问题-绘图顶部显示不需要的线

基线R中的绘图存在小问题-绘图顶部显示不需要的线,r,plot,border,base,R,Plot,Border,Base,我已经创建了一个以R为底的绘图,包括3个剪辑的“abline”。尽管使用了“frame.plot=FALSE”函数,该函数删除了绘图周围的框(参见图像示例1),但当我添加剪裁的ABLINE[使用ablineclip]时,新的帧线会出现在它们的上方(参见图像示例2) 我使用的代码如下所示: library(plotrix) op <- par(mar=c(5, 6, 4, 2) + 0.1) plot(dif2$land_area ~ dif2$Year_no, ylim = c(1,40

我已经创建了一个以R为底的绘图,包括3个剪辑的“abline”。尽管使用了“frame.plot=FALSE”函数,该函数删除了绘图周围的框(参见图像示例1),但当我添加剪裁的ABLINE[使用ablineclip]时,新的帧线会出现在它们的上方(参见图像示例2)

我使用的代码如下所示:

library(plotrix)

op <- par(mar=c(5, 6, 4, 2) + 0.1)
plot(dif2$land_area ~ dif2$Year_no, ylim = c(1,4000), col.axis = rgb(68, 84, 106,max=255),xaxt='n', type='o', pch=16, col='red', font.axis=2, font.lab=2, col.lab=rgb( 113, 113, 113, max=255), xlab = 'Year', ylab = 'Total Land Area Changed to \nResidential Development (Ha)', frame.plot = FALSE, cex=1.3) 
rect(23.2,0,25.8,4000,col='grey',density = 8,border=T) 
rect(10.2,0,11.8,4000,col='grey',density = 8,border=T)
xlim(0,30)
axis(1,at= 1:30,labels=F)
axis(1,at= 1:30,tick=F, font.axis=2, col.axis = rgb(68, 84, 106, max=255),labels= c(1989:2018))
# the below section is that which seems to create the issue #
ablineclip(lm(land_area ~ Year_no, data = subset(dif2, int==0)), col='blue', lty=2, x1=1,x2=10, lwd=0.8)
ablineclip(lm(land_area ~ Year_no, data = subset(dif2, int==0)), col='blue', lty=2, x1=12,x2=23, lwd=0.8)
ablineclip(lm(land_area ~ Year_no, data = subset(dif2, int==1)), col='blue', lty=2, x1=26,x2=30, lwd=0.8)
库(plotrix)

op您只需使用参数
y1
y2
将y限制设置为
ablineclip
剪裁的区域,确保
y2
位于绘图顶部以下

ablineclip(lm(土地面积~年份编号,数据=子集(dif2,int==0)),
col=‘蓝色’,lty=2,x1=1,x2=10,y1=1,y2=3500,lwd=0.8)
ablineclip(lm(土地面积~年份编号,数据=子集(dif2,int==0)),
col=‘蓝色’,lty=2,x1=12,x2=23,y1=1,y2=3500,lwd=0.8)
ablineclip(lm(土地面积~年份编号,数据=子集(dif2,int==1)),
col=‘蓝色’,lty=2,x1=26,x2=30,y1=1,y2=3500,lwd=0.8)
结果:

当然,我没有你的数据来处理,所以我必须建立一个相似的集合(这就是为什么图形的形状与你的不同)。我使用的数据是:


dif2这是代码中的内容,请参见下文,当您提供x1和x2时,它会在y限制+1上画一条线:

> ablineclip
function (a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL, 
    coef = NULL, untf = FALSE, x1 = NULL, x2 = NULL, y1 = NULL, 
    y2 = NULL, ...) 
{
    if (!is.null(c(x1, x2, y1, y2))) {
        oldclip <- par("usr")
        if (is.null(x1)) 
            x1 <- oldclip[1]
        if (is.null(x2)) 
            x2 <- oldclip[2]
        if (is.null(y1)) 
            y1 <- oldclip[3]
        if (is.null(y2)) 
            y2 <- oldclip[4]
        clip(x1, x2, y1, y2)
        abline(h = oldclip[4] + 1)
并绘制:

library(plotrix)
op <- par(mar=c(5, 6, 4, 2) + 0.1)
plot(dif2$Year_no, dif2$land_area,ylim = c(1,4000), col.axis = rgb(68, 84, 106,max=255),xaxt='n', type='o', pch=16, col='red', font.axis=2, font.lab=2, col.lab=rgb( 113, 113, 113, max=255), xlab = 'Year', ylab = 'Total Land Area Changed to \nResidential Development (Ha)', frame.plot = FALSE, cex=1.3) 
rect(23.2,0,25.8,4000,col='grey',density = 8,border=T) 
rect(10.2,0,11.8,4000,col='grey',density = 8,border=T)
axis(1,at= 1:30,labels=F)
axis(1,at= 1:30,tick=F, font.axis=2, col.axis = rgb(68, 84, 106, max=255),labels= c(1989:2018))

谢谢-以上是两种解决方案中比较简单的一种。真是太棒了@很高兴知道。如果这解决了你的问题,请考虑接受答案,以便同一个问题的其他人也能找到解决的办法。谢谢
library(plotrix)
op <- par(mar=c(5, 6, 4, 2) + 0.1)
plot(dif2$Year_no, dif2$land_area,ylim = c(1,4000), col.axis = rgb(68, 84, 106,max=255),xaxt='n', type='o', pch=16, col='red', font.axis=2, font.lab=2, col.lab=rgb( 113, 113, 113, max=255), xlab = 'Year', ylab = 'Total Land Area Changed to \nResidential Development (Ha)', frame.plot = FALSE, cex=1.3) 
rect(23.2,0,25.8,4000,col='grey',density = 8,border=T) 
rect(10.2,0,11.8,4000,col='grey',density = 8,border=T)
axis(1,at= 1:30,labels=F)
axis(1,at= 1:30,tick=F, font.axis=2, col.axis = rgb(68, 84, 106, max=255),labels= c(1989:2018))
fit1=lm(land_area ~ Year_no, data = subset(dif2, int==0))
fit2=lm(land_area ~ Year_no, data = subset(dif2, int==1))
lines(1:10,predict(fit1,data.frame(Year_no=1:10)),lty=8,col="blue")
lines(12:23,predict(fit1,data.frame(Year_no=12:23)),lty=8,col="blue")
lines(26:30,predict(fit2,data.frame(Year_no=26:30)),lty=8,col="blue")