Search code examples
rgraphing

R - How to make stacked bargraph for data over years with missing years


Here is my plot in R: enter image description here

Here is the code that made the plot:

years <- c(1980, 1984, 1984, 1986, 1988)
pubs  <- c("book","book","poem","poem","book")
mypubs <- data.frame(years,pubs)
counts <- table(mypubs$pubs,mypubs$years)
barplot(counts,main="pubs per year",xlab="year",col=c("darkblue","red"),legend=rownames(counts))

This is time-series data, and there is no data for 1981, 1982, 1983, 1985 and 1987. The data is whether I read a book or a poem at any given time.

Is there a straightforward way to take mypubs and add holes for the missing years? I've tried doing a histogram plot, but I can't figure out how to make it reliably one bar for each year, and I can't figure out how to make a histogram do stacked colors.


Solution

  • Extending Greg's answer from above, try this:

    years <- c(1980, 1984, 1984, 1986, 1988)
    pubs  <- c("book","book","poem","poem","book")
    mypubs <- data.frame(years,pubs)
    
    mypubs$years<-factor(mypubs$years, levels = as.character(1980:1990))
    
    counts <- table(mypubs$pubs,mypubs$years)
    barplot(counts,main="pubs per year",xlab="year",col=c("darkblue","red"),legend=rownames(counts))
    

    The "factor" command with convert the years column into factors, while the optional "levels" will set the available levels