在r

时间:2018-08-21 06:42:10

标签: r date plot time-series na

嗨,我有一个用NA值和x轴作图的问题。 我需要点之间的连接以及x轴上较少的数据,就是说每5天看到一次,而且看起来不错。

S_884 = read.csv(file="VA_C_844.csv", header=T)

bar = barplot(S_884$um,plot=F) # para extraer coordenadas

# par(mar=c(7,4,1,4))

###
# IAB
###
par()$mar

png("grafico.png", width = 10, height = 6, units = 'in', res = 250,pointsize=13)
par(mar=c(5,4,4,12))
graf = barplot(S_884$um, axes=T, ylim=c(0,400),
               col="grey",xlim = c(0,round(max(bar),0)))
abline(h=0,col="gold")
mtext(2,line=2, cex=0.7,
      text=expression(Indice~de~aguas~blancas))
title (main="Eventos marzo - abril 2015", line=1)
axis(side =1, at= bar, labels = substr(S_884$Fecha_C,start=1,stop=131),las=2,cex=0.5,lwd = 1)
box()


###
# Oxigeno
###
range(S_884$Oxigeno)
par(new=T)
plot(bar,S_884$Oxigeno,ylim=range(S_884$Oxigeno[-1], na.rm = TRUE),axes=F,xlab="",ylab="",type="o",col="black",pch=21,bg="blue")
axis(4,at = 0:4,labels = 0:4,lwd=1,line=1,las=2,col="blue")
mtext(4,text=expression(S_884$Oxigeno~paste("(",ml,.L^-1,")")),line=3, cex=0.7)


###
# Temperatura
###
range(S_884$Temp)
par(new=T)
plot(bar, S_884$Temp,ylim=c(15,21),axes=F,xlab="",ylab="",type="o",col="black",pch=21,bg="red")
axis(4,at = 15:21,labels = 15:21,lwd=1,line=4,las=2,col="red")
mtext(4,text="Temperatura (°C)",line=6, cex=0.7)


###
# gradiente
###
range(S_884$Chl_C)
par(new=T)
plot(bar, S_884$Chl_C,ylim=c(0,20),axes=F,xlab="",ylab="",type="o",col="black",pch=21,bg="green")
axis(4,at = seq(0,20,by=2),labels = seq(0,20,by=2),lwd=1,line=8,las=2,col="green")
mtext(4,text="Clorofila (mg3/L)",line=10, cex=0.7)


# artificio para legenda

legend(x="topright",
       legend = c("IAB", "Oxígeno","Temperatura","Gradiente"),
       lty=c(1,NA,NA,NA),lwd=c(4,1,1,1),
       pch=c(NA,21,21,21),
       col=c("gray","black","black","black"),
       pt.bg=c("gray","blue","red","green"),bty = "n")   

dev.off()

我得到这个 enter image description here

这是我的数据 enter image description here

救救我!

1 个答案:

答案 0 :(得分:0)

我在x轴上的数据存在相同的问题。为了解决这个问题,我做了一些类似的事情:

###
# IAB
###
par()$mar

png("grafico.png", width = 10, height = 6, units = 'in', res = 250,pointsize=13)
par(mar=c(5,4,4,12))
graf = barplot(S_884$um, axes=T, ylim=c(0,400),
           col="grey",xlim = c(0,round(max(bar),0)))
abline(h=0,col="gold")
mtext(2,line=2, cex=0.7,
  text=expression(Indice~de~aguas~blancas))
title (main="Eventos marzo - abril 2015", line=1)
  axis(side =1, at=seq(0,131, 5), labels = S_884$Fecha_C[seq(0,131, 5)],las=2,cex=0.5,lwd = 1)
box()

--------编辑---------

对于具有NA值的行,您可以使用此方法:

此代码与您的代码相同:

plot(y=c(1,2,3,NA,5),x=c(1:5), type="o")

尽管NA值,该代码也允许整体跟踪行

plot(y=c(1,2,3,5),x=c(1,2,3,5), type="o")

希望对您有帮助