I'm trying to graph a simulated model of infectious disease spread.
My data has three columns, x, y, and infection time. I need to plot each point (x coordinate, y coordinate) at each time (t), where t runs between 1 and 4. SO I'm looking for four graphs, one at each time, where the first graphs plots the first infected point, the second plots the infected points at time 1 and 2, etc.
I know I can get multiple graphs using par(mfrow=c(2,2)), but I'm not sure how to incorporate each time into the code. Any suggestions?
There are about a thousand ways to do this, depending on your original data set. Learning ggplot2
is probably the best in the long run. Using base graphics, though, you can make a plot for each subset of data (and can also use the subset
command instead of t<=Ti
, customize colors, etc):
par(mfrow=c(2,2))
for (Ti in 1:4){
plot(x[t <= Ti], y[t <= Ti])
}
If you are trying to convey the passage of time, you might want to plot the entire range of data invisibly on each plot, to set up the same axes. Then use points
or lines
to plot the data into each of those identical frames...
par(mfrow=c(4,1))
for (Ti in 1:4){
plot(x, y, type="n")
points(x[t <= Ti], y[t <= Ti])
}