0
votes

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.

2
I don't see any years with no data, you probably need to factorize the years in the original df.mtoto
There is no data for the year 1991. The original DF had "Year" as a column, and had a "yes/no" column. I've edited the example to make this more clear.vy32
please dput your data, instead of describing it.mtoto
I have created a dramatically simpler example.vy32
how do you expect to plot data not inside your data.frame ?mtoto

2 Answers

1
votes

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

1
votes

Before you run your code, do something like:

mydata$B <- factor(mydata$B, levels=1990:2015)

Then run your code and you will see 0 counts for the years between 1990 and 2015 without data. Modify the code if you want a different range of years.