# Based on code from Simon Potter's Masters thesis # (~/Files/Teaching/Simon2012/thesis-notes/thesis/tex/referenced-content.Rnw) library(gridSVG) plotdat <- aggregate(hp ~ gear + vs, data = mtcars, mean) plotdat$gear <- factor(plotdat$gear) plotdat$vs <- factor(plotdat$vs) # Let's have a look at the data plotdat # Plot a barchart library(lattice) # From trellis.par.get("superpose.polygon") fills <- c("#CCFFFF", "#FFCCFF") val0pat <- pattern(gTree(children=gList( rectGrob(gp=gpar(col=NA, fill=fills[1])), linesGrob(gp=gpar(col="black")))), width = unit(2, "mm"), height = unit(2, "mm"), dev.width = 1, dev.height = 1) val1pat <- pattern(gTree(children=gList( rectGrob(gp=gpar(col=NA, fill=fills[2])), linesGrob(0:1, 1:0, gp=gpar(col="black")))), width = unit(2, "mm"), height = unit(2, "mm"), dev.width = 1, dev.height = 1) barchart(hp ~ gear, groups = vs, data = plotdat, xlab = "Number of Gears", ylab = "Mean Horsepower", auto.key = list(space = "right"), horizontal = FALSE) # Registering patterns registerPatternFill("val0pat", val0pat) registerPatternFill("val1pat", val1pat) # Applying pattern fills barNames <- paste0("plot_01.barchart.x.", 1:3, ".rect.panel.1.1") for (i in 1:3) { grid.patternFill(barNames[i], label = c("val0pat", "val1pat"), group = FALSE) } # Applying legend pattern fills legendNames <- paste0("plot_01.key.rect.2.", 1:2) grid.patternFill(legendNames[1], label = "val0pat", group = FALSE) grid.patternFill(legendNames[2], label = "val1pat", group = FALSE) grid.export("barchart-pattern.svg")