0

enter image description hereI'm having a function to plot a graph. I'm having value zero, it is similar to NA. I dont want to include that in calculating mean. Also I dont want to include in the graph. Instead of zero value we can give some space. Please advise.

    x <- c(579, 0, 581, 610, 830, 828, 592, 651, 596, 596, 591, 581, 587, 594, 604, 606, 447, 434, 445)
    drawGraph <- function(x, y, z) {
    g_range <- range(0,x)
    #is.na(x) <- (x == 0)
    plot(x, type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE)
    box()
    axis(1, at=1:length(x), lab=FALSE)
    text(1:length(x), par("usr")[3] - 2, srt=45, adj=1.2, labels=y, xpd=T, cex=0.3)
    scale=round(max(x)/10,digits=0)
    axis(2, las=1, at=scale*0:g_range[2], cex.axis=0.3)
    main_title<-as.expression(z)
    MEANLIMIT <- seq(length=length(x), from=mean(x), by=0)
    ULIMIT <- seq(length=length(x), from=(mean(x)*2.66), by=0)
    LLIMIT <- seq(length=length(x), from=(mean(x)/2.66), by=0)
    lines(MEANLIMIT, type="l", pch=2, lty=2, col="grey")
    lines(ULIMIT, type="l", pch=2, lty=2, col="red")
    lines(LLIMIT, type="l", pch=2, lty=2, col="grey")
    title(main=main_title, col.main="red", font.main=4)
    title(xlab="Build", col.lab=rgb(0,0.5,0))
    title(ylab="MS", col.lab=rgb(0,0.5,0))
    legend("topright", g_range[2], main_title, cex=0.8, col=c("blue"), pch=21, lty=1);
    }
Tamilan
  • 951
  • 3
  • 11
  • 23
  • 1
    If you want `0` to behave like `NA` then why not make it `NA`? Its the proper way to code missing data. `x[x==0] <- NA` – Sacha Epskamp Nov 17 '11 at 16:51

2 Answers2

3

For a part of the code that is reproducible first encode missing values to NA:

x <- c(579, 0, 581, 610, 830, 828, 592, 651, 596, 596, 591, 581, 587, 594, 604, 606, 447, 434, 445)
x[x==0] <- NA

Next to plot it with overplotted lines that aren't broken up you need to index out the missing values from the result as well as the x-axis:

plot(which(!is.na(x)),x[!is.na(x)], type="o", col="blue",axes=FALSE, ann=FALSE)

Your example isn't very reproducible though. You give us an x object and a function that also needs an y and z object. For instructions on making a proper reproducible example please see: How to make a great R reproducible example?

Community
  • 1
  • 1
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • I did as Sacha advised.. it is working for me.. x[x==0] <- NA plot(which(!is.na(x)),x[!is.na(x)], type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE) x <- x[!is.na(x)] – Tamilan Nov 18 '11 at 14:04
  • Thank you very much Sacha and Nick – Tamilan Nov 18 '11 at 14:08
1

I believe you mean:

x[x==0]<-NA

Where calculating the mean, you then need to include na.rm=TRUE as an extra parameter.

Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57