Plot series of filled hexagons in ggplot2

ggplot2, plot, r

Solution

As with most things in ggplot, the plotting is actually extremely straightforward, most of the work is getting your data in the right shape so that it makes sense. A `for` loop is entirely unnecessary, `geom_polygon()` just needs a dataframe with the x and y coordinates, and a variable defining which group they belong to. With your data:

library(ggplot2)
library(reshape2)

#Get your coordinates in long format with an id
hexdat.x <- melt(cbind(id = 1:length(hex.x), as.data.frame(hex.x)), id.vars = "id", value.name = "x")
hexdat.y <- melt(cbind(id = 1:length(hex.y), as.data.frame(hex.y)), id.vars = "id", value.name = "y")

#Merge them into the same dataframe
hexdat <- merge(hexdat.x, hexdat.y)

head(hexdat)
#   id variable    x          y
# 1  1       V1  0.0 -0.5773503
# 2  1       V2  0.5 -0.2886751
# 3  1       V3  0.5  0.2886751
# 4  1       V4  0.0  0.5773503
# 5  1       V5 -0.5  0.2886751
# 6  1       V6 -0.5 -0.2886751

Now to plot the hexagons, you just need to give `ggplot` the x and y coordinates, and specify the group each one belongs to:

ggplot(hexdat, aes(x, y)) + 
  geom_polygon(aes(group = id), fill = "wheat", colour = "black")

Problem

I am trying to create a tesselation of filled hexagons (polygons centered around a hexagonally-spaced lattice) in ggplot2. I have accomplished this using the 'plot' command but am struggling transitioning this to ggplot. Here is the code for the set-up: ``` # Generate a lattice of points equally spaced in the centers of a hexagonal lattice dist = 1 # distance between the centers of hexagons nx = dist*15 # horizontal extent ny = dist*15 # vertical extent MakeHexLattice = function(nx, ny, dist, origin=c(0,0)) { locations = cbind(location = 1:(nx*ny), x = sort(c(rep(seq(from=0, by=dist, length.out=nx),each=ceiling(ny/2)), rep(seq(from=dist/2, by=dist, length.out=nx), each=floor(ny/2)))) + origin[1], y = rep(c(seq(from=0, by = dist*sqrt(3), length.out=ceiling(ny/2)), seq(from=dist*sqrt(3)/2, by=dist*sqrt(3), length.out=floor(ny/2))) + origin[2], times=nx)) class(locations) = c(class(locations), "lattice") attr(locations, "gridsize") = dist return(locations) } ``` Here is the code for creating the image using 'plot', which looks very nice: ``` landscape = MakeHexLattice(nx=nx,ny=ny,dist=dist,origin=c(0,0)) # Plot hexagonal lattice as points plot(x=landscape[,2],y=landscape[,3], pch=19, col="black", cex=0.5, asp=1/1) # Separate x and y coordinates lx = landscape[,2] # x-coordinates ly = landscape[,3] # y-coordinates # Plot hexagonal lattice as filled hexagons hex.x = cbind(lx + 0, lx + 0.5, lx + 0.5, lx + 0, lx - 0.5, lx - 0.5) hex.y = cbind(ly - 1/(sqrt(3)), ly - 1/(2*sqrt(3)), ly + 1/(2*sqrt(3)), ly + 1/(sqrt(3)), ly + 1/(2*sqrt(3)), ly - 1/(2*sqrt(3))) hex.vectors = cbind(hex.x, hex.y) for(i in 1:(length(hex.vectors)/12)){ polygon(x=hex.vectors[i,1:6], y=hex.vectors[i,7:12], angle = 120, border=NULL, col="wheat", lty = par("lty"), fillOddEven = FALSE) } ``` Any tips on how to accomplish this same thing using ggplot2 (which I am transitioning to using)? I have tried using geom_polygon but can't seem to work out the for-loop. (Also, please don't tell me to use 'hexbin' -- not the goal that I am trying to accomplish!) Thank for the help!

Original source