4
votes

I have a simple (yet very large) data set of counts made at different sites from Apr to Aug. Between mid Apr and July there are no zero counts - yet a line at zero extends from the earliest to latest date.

enter image description here

Here is the part of the data used to make the above chart (columns are- Site.ID, DATE, Visible Number):

data=structure(list(Site.ID = c(302L, 302L, 302L, 302L, 302L, 302L, 302L, 302L, 302L, 302L, 302L, 302L, 304L, 304L, 304L, 304L, 304L, 304L, 304L, 304L, 304L, 304L, 304L, 304L), DATE = structure(c(1L, 2L, 5L, 3L, 4L, 6L, 8L, 7L, 9L, 10L, 11L, 12L, 1L, 2L, 5L, 3L, 4L, 6L, 8L, 7L, 9L, 10L, 11L, 12L), .Label = c("3/21/2014", "3/27/2014", "4/17/2014", "4/28/2014", "4/8/2014", "5/13/2014", "6/17/2014", "6/6/2014", "7/10/2014", "7/22/2014", "7/29/2014", "8/5/2014" ), class = "factor"), Visible.Number = c(0L, 0L, 5L, 14L, 20L, 21L, 6L, 8L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 7L, 7L, 7L, 7L, 5L, 0L, 0L, 0L, 0L)), .Names = c("Site.ID", "DATE", "Visible.Number" ), class = "data.frame", row.names = c(NA, -24L))

attach(data)
DATE<-as.Date(DATE,"%m/%d/%Y")
plot(data$Visible.Number~DATE, type="l", ylab="Visible Number")

I have two sites but there are three lines. How to make R not plot a line along zero? Thank you for your help!

1
Nice job on having your first question be reasonable, clear, and above all reproducible! Welcome to SO.Gregor Thomas

1 Answers

3
votes

Your problem is with the multiple site ID's. It plots the first one, then goes back (drawing a line) to draw the second one. Essentially, base plots tries to draw all the lines without "lifting the pen". With base plotting, your option is to plot them separately with lines, perhaps in a for loop. I think stuff like this is easier with ggplot2

library(ggplot2)
ggplot(data, aes(x = DATE, y = Visible.Number, group = Site.ID)) + geom_line()

# if you prefer more base-like styling
ggplot(data, aes(x = DATE, y = Visible.Number, group = Site.ID)) +
  geom_line() +
  theme_bw()

In base:

plot(data$DATE, data$Visible.Number, type = "n",
     ylab = "Visible Number", xlab = "Date")

for(site in unique(data$Site.ID)) {
    with(subset(data, Site.ID == site),
         lines(Visible.Number ~ DATE)
    )
}

N.B. I did not attach my data as you did, so I don't know if the subsetting in the base solution will work properly for you if you do attach. In general, avoid attach; with is a nice way to save typing without attaching, and is much less "risky" in that it doesn't copy your data columns into isolated vectors, thus making them more difficult to keep track of as you subset or otherwise work with your data.