I am trying to plot data into split plots with par(mfrow) and am trying to get y logarithmic y axis, the axes are defined alright, but somehow the plot data wouldn't be plotted, and the plot is empty.
My question: Does anyone see what seems to be wrong with my code and why nothing is being plotted (the code executes without an error)?
2nd question: How could I define the tick marks and axis labels of the y axis so that only 10, 100 and 1000 are shown?
Here is my code with some sample data xy
that is of the same structure like my initial data.
xy <- structure(list(GROUP = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Group1", "Group2", "Group3"), class = "factor"), YEAR1 = c(1966L, 1967L, 1968L, 1969L, 1970L, 1971L, 1972L, 1973L, 1964L, 1965L,1966L, 1967L, 1968L, 1971L, 1972L, 1973L, 1974L, 1975L, 1976L), YEAR2 = c(1967L, 1968L, 1969L, 1970L, 1971L, 1972L,1973L, 1974L, 1965L, 1966L, 1967L, 1968L, 1969L, 1972L, 1973L,1974L, 1975L, 1976L, 1977L), SAMPLES1 = c(83L, 86L, 118L, 116L, 114L, 111L, 108L, 106L, 24L, 25L, 26L, 26L, 26L, 87L, 82L, 218L, 203L, 221L, 219L), SAMPLES2 = c(83L, 86L, 118L, 116L, 114L, 111L, 108L, 106L, 24L, 25L, 26L, 26L, 26L, 87L, 82L, 218L, 203L, 221L, 219L), RANK = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("GROUP","YEAR1", "YEAR2", "SAMPLES1", "SAMPLES2", "RANK"), class = "data.frame", row.names = c(NA, -19L))
ind <- split(x = xy,f = xy[,'RANK'])
fname <- 'Plot.png'
png(fname, width=4658, height=6716, res=300)
par(mfrow=c(3,1), oma = c( 2, 2, 2, 2 ) )
# initiate plot
for (i in seq_along(ind)){
i <- ind[[i]]
xx = unlist(i[, c(2, 4)])
yy = unlist(i[, c(3, 5)])
my_x_lim <- range(c(1530, 2015))
my_y_lim=range(c(yy,10,100))
mar.default <- c(2,4,2,4) + 0.1
par(mar = mar.default + c(-1.2, 0, -1.2, 0),mgp = c(0, 1, 0))
plot(xx, yy,log="y",xaxs="i",type='n', xlab=NA,ylab=NA,xlim = my_x_lim, ylim=my_y_lim,las=1,xaxt="n")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col = "grey")
par(mar = mar.default + c(-0.5, 0, -0.5, 0),mgp = c(0, 1, 0))
axis(1, at = seq(1000, 2050, 10), cex.axis=0.7,lty=0, lwd.ticks=0,labels=TRUE, tcl=0)
par(mar = mar.default + c(-1, 0, -1, 0),mgp = c(0, 1, 0))
apply(i, 1, function(y)
rect(y[2], min(y[3], 0), y[4], max(y[3], 0), col= 'seagreen',border=NA))
par(mar = mar.default + c(-1.2, 0, -1.2, 0),mgp = c(0, 1, 0))
axis(1, at = seq(1000, 2050, 5), cex.axis=0.5, labels=NA, tcl=-0.3)
axis(1, at = seq(1000, 2050, 10), cex.axis=0.5, labels=NA, tcl=-0.3,lwd.ticks=0.5)
axis(3, at = seq(1000, 2050, 5), cex.axis=0.5, labels=NA, tcl=-0.3)
axis(3, at = seq(1000, 2050, 10), cex.axis=0.5, labels=NA, tcl=-0.3,lwd.ticks=0.5)
axis(2, at = seq(-100000, 100000, 1000), cex.axis=0.5, labels=NA, tcl=-0.2,lwd.ticks=0.5)
axis(4, cex.axis=0.7, las=1, labels=TRUE)
}
dev.off()
apply
and there is a string somewhere in your data.frame, then all numbers are up-converted into strings, so your call torect
is being passed strings not numbers. Replace your call withapply(i, 1, function(y) { browser(); rect(y[2], min(y[3], 0), y[4], max(y[3], 0), col= 'red',border=NA) })
to see this in effect. – r2evans