0
votes

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() 
1
When using apply and there is a string somewhere in your data.frame, then all numbers are up-converted into strings, so your call to rect is being passed strings not numbers. Replace your call with apply(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

1 Answers

1
votes

You have a few problems with your code:

  1. I think you are mis-subsetting your code when assigning to xx and yy. I think your y-axis is based on $SAMPLES[12] and your x-axis on $YEAR[12], but you are assigning $YEAR1 and $SAMPLE1 to xx, and similarly to yy. Fix the column references and your data will likely be ordered/ranged correctly.

  2. The use of apply converts the row to a vector; normally this is fine, but since one of the elements ($GROUP) is a string, all other elements get up-converted to strings, so your mid-apply call to rect is using strings, not numbers. Subset the data.frame accordingly, for example i[,-1].

  3. Your y-axis is logarithmic but you are using zero in the call to rect (log(0) is undefined). Change references to zero in rect to a one, and these may start working.

Using your xy data as defined above, this code produces something, over to you if it is correct and usable:

ind <- split(x = xy,f = xy[,'RANK'])
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, 3)])
  yy = unlist(i[, c(4, 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], 1, function(y)
    rect(min(y[1:2]), min(y[3], 1), max(y[1:2]), max(y[3], 1), 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)
}