1

I'm trying to plot the location of an incinerator on another plot.

# The first plot
hexbinplot(Easting~Northing | Bclass4, 
    BIRTH_NO68, las=1, scales =list(x = list(log = 10, equispaced.log = FALSE)), 
    aspect = 1, bins=50, style="nested.lattice", 
    main="Spatial distribution of birthweights by quartile")
# The second plot
ppp=xyplot(173098~319444, data=BIRTH_NO68, pch=17, cex=15, col="Black")
# Together
hexbinplot(Easting~Northing | Bclass4, BIRTH_NO68, las=1, 
    scales = list(x = list(log = 10, equispaced.log = FALSE)), aspect = 1, 
    bins=50, style="nested.lattice", 
    main="Spatial distribution of birthweights by quartile") + pop

All that comes up is the first plot. Here's the map I'm trying to mark the location of an incinerator on.

fdetsch
  • 5,239
  • 3
  • 30
  • 58
  • I don't know if its made more difficult by needing the incinerator location to show on all 4 plots? – Tom Gribbin Apr 16 '16 at 22:45
  • please list the package you are using to for the `hexbinplot` function. It is not part of base R. R has two processes for creating figures: base graphics and grid. If the package you are using uses base graphics, then add `par(mfrow=c(1, 2))` above your first graph for a one row, two column graph panel, or `par(mfrow=c(2, 1))` for a two row, one column panel. See `?par` for a lot of graphing parameters that you can adjust. For `grid` figures, it a bit more involved. – lmo Apr 16 '16 at 23:03
  • the package is hexbin, does that help? – Tom Gribbin Apr 18 '16 at 00:10
  • Looking at the package notes on CRAN, hexbin is based on the `lattice` package, which uses the `grid` graphics engine. http://cran.mirrors.hoobly.com/web/packages/hexbin/index.html In this case, take a look at `?grid.layout` after loading grid. There are likely questions about this function on SO. – lmo Apr 18 '16 at 12:49

1 Answers1

1

Have a look at as.layer from latticeExtra which lets you easily combine single lattice plots. Here is some sample code based on the first example provided in ?hexbinplot.

library(hexbin)
library(latticeExtra)

## example taken from ?hexbinplot
mixdata <- data.frame(x = c(rnorm(5000),rnorm(5000,4,1.5)),
                      y = c(rnorm(5000),rnorm(5000,2,3)),
                      a = gl(2, 5000))

p1 <- hexbinplot(y ~ x, mixdata, aspect = 1,
                 trans = sqrt, inv = function(x) x^2)

## add points plot to existing hexbinplot
p2 <- xyplot(2.5 ~ 3.5, pch = 24, cex = 3, 
             col = "white", fill = "darkred", lwd = 2)

p1 + as.layer(p2)

enter image description here

Note that you can also accomplish this task in one go without being required to employ latticeExtra by simply defining two different panel functions inside hexbinplot, i.e.

hexbinplot(y ~ x, mixdata, aspect = 1,
                 trans = sqrt, inv = function(x) x^2, 
                 panel = function(...) {
                   panel.hexbinplot(...)
                   panel.xyplot(3.5, 2.5, pch = 24, cex = 3, 
                                col = "white", fill = "darkred")
                 })
fdetsch
  • 5,239
  • 3
  • 30
  • 58