This is the code that produces the correct output, except for the color, which comes from "fill=paste0(i)" in poly_hex.
for (i in 1:length(hexagon$xl)){
centerx = hexagon$xl[i]
centery = hexagon$yl[i]
radius = hexagon$radii[i]
poly_hex <- data.table(
x=c(centerx, centerx - sqrt(3)*radius/2, centerx - sqrt(3)*radius/2, centerx , centerx + sqrt(3)*radius/2, centerx + sqrt(3)*radius/2 ),
y=c(centery + radius, centery + 1/2*radius, centery - 1/2*radius, centery-radius , centery - 1/2*radius, centery + 1/2*radius ), fill=rep(paste0(i), 6)
)
poly = rbind(poly, poly_hex)
}
ggplot() + geom_polygon(data=poly, aes(x=x, y=y, fill = fill)) + theme(legend.position = "none") + coord_fixed(ratio = 1) + xlab("Independent Variable") + ylab("Dependent Variable")
This gives this picture: correct, except for color
But if I just set the fill to be black, everything messes up.
for (i in 1:length(hexagon$xl)){
centerx = hexagon$xl[i]
centery = hexagon$yl[i]
radius = hexagon$radii[i]
poly_hex <- data.table(
x=c(centerx, centerx - sqrt(3)*radius/2, centerx - sqrt(3)*radius/2, centerx , centerx + sqrt(3)*radius/2, centerx + sqrt(3)*radius/2 ),
y=c(centery + radius, centery + 1/2*radius, centery - 1/2*radius, centery-radius , centery - 1/2*radius, centery + 1/2*radius ),
fill=rep("red", 6)
)
poly = rbind(poly, poly_hex)
}
ggplot() + geom_polygon(data=poly, aes(x=x, y=y, fill = fill)) + theme(legend.position = "none") + coord_fixed(ratio = 1)+ xlab("Independent Variable") + ylab("Dependent Variable")
Right color, messed up plotting
What's the way to fix this?