imageName = function(city, full = FALSE, mini = FALSE, ext = "png") { id = gsub("[ ,]", "_", city) if(is.logical(full)) { if(full) id = paste("file://", getwd(), id, sep = .Platform$file.sep) } else id = paste(full, id, sep = .Platform$file.sep) if(mini) id = paste(id, "mini", sep = "_") id = paste(id, ext, sep = ".") id } makeHist = function(z, device = png, ylim = range(z$temp), full = FALSE, mini = FALSE, cols = temperatureColors) { # cols = rainbow(length(levels(z$level))) id = imageName(z[1, "city"], full, mini = mini) if(!is.null(device)) { device(id, width = 300, height = 300, bg = "transparent") on.exit(dev.off()) } plot(z$temp, xlab = "", ylab = "", axes = FALSE, ylim = ylim, col = cols[z$level]) segments(1:3, z$temp[1:3], 2:4, z$temp[2:4], col = cols[z$level], lwd = if(mini) 2 else 30) axis(2) id } createHist = function(data = temperature, f = makeHist, full = "/tmp", mini = FALSE) { by(data, factor(data$city), f, ylim = range(data$temp), full = full, mini = mini) } createLegend = function(values, filename = "/tmp/legend.png", colors = temperatureColors) { key.width = 8 levels = values$level if(length(filename) && nchar(filename) > 0) { trellis.device(png, filename = filename, bg = "transparent") on.exit(dev.off()) } trellis.par.set(axis.text = list(col = "white", cex=1.2, font=2)) # print(simpleKey(levels(levels?dra), rectangles = list(TRUE, col = rainbow(4))) breaks = c(min(values$temp), by(values$temp, values$level, max)) k = list(at = breaks, labels = as.character(breaks), col= colors, width= key.width ) draw.colorkey(key = k, draw = TRUE, viewport(height = unit(.9, "npc"))) }