Reputation: 4507
I have binary event data in the following form, in a data frame:
year A B C D
1990 0 0 1 0
1991 0 1 1 0
1992 0 0 0 1
1993 1 1 0 0
There may be around 50 series in total, for around 25 years.
My goal is to show all of the series in one plot, to visualize an aggregate view of the series.
I.e. I would like to graph all of the data in one plot, so that for each column there would be a separate panel on top of each other. Each panel could be a simple histogram ( i.e. type="h"
) to visualize the binary data.
There should naturally be no x-axis labels and ticks etc. between the panels on top of each other, except for the bottom-most panel.
Now, I could do this manually with par(mfrow=c(rows, cols))
and then separately do plot(...)
for each of the series, but that is not feasible due to the large number of series to graph. Or I could write a for-loop to handle it, but there must be better ways of achieving this.
How could I do this without doing it manually panel by panel? If there is a better way to visualize an aggregate view of all the series in one plot, suggestions are welcome. For example, if there is a way of doing this without separate panels.
Follow-up question
About formatting the ggplot2
solution in Ben's answer:
1) Currently, the column headers (do not know the exact name, but "the series titles") are at the right hand side at a 90° angle, so they overlap each other as the names are country names. I would like for those to be at the left hand side of each panel at a 0° angle.
2) Also, at the moment each of the panels has its own y-axis tick labels 0 and 1, at the LH side of each panel, and those are not necessary, so I would like to remove them.
Upvotes: 1
Views: 785
Reputation: 226971
The easiest way to do this is to reshape the data to long form (e.g. reshape2::melt()
as below), then use lattice
or ggplot2
.
dd <- read.table(header=TRUE,text="
year A B C D
1990 0 0 1 0
1991 0 1 1 0
1992 0 0 0 1
1993 1 1 0 0")
## make category names longer
names(dd) <- c("year","Aardvark","Behemoth","Cataclysm","Disaster")
library("reshape2")
ddm <- melt(dd,id.var="year")
library("lattice")
## you could use type="h" here, but I like "b" better ...
xyplot(value~year|variable,data=ddm,type="b",
layout=c(1,4), ## stack (1 column, 4 rows); can also
## use a third element of layout to split
## into multiple pages
as.table=TRUE ## stack top-to-bottom
)
updated: remove y-axis tick labels; use this answer to rotate facet labels.
library("ggplot2"); theme_set(theme_bw())
ggplot(ddm,aes(year,value))+geom_point()+geom_line()+
facet_grid(variable~.)+ ## or use facet_wrap(~variable)
theme(panel.margin=grid::unit(0,"lines"))+ ## squash panels together
scale_y_continuous(breaks=c(0,1),labels=rep("",2))+
theme(strip.text.y = element_text(angle = 0))
Upvotes: 5