As others have noted, this is due to the lineend specification, which can be found in environment(GeomTile$draw_panel)$f
:
function (self, data, panel_params, coord)
{
if (!coord$is_linear()) {
... #omitted for space
}
else {
coords <- coord$transform(data, panel_params)
ggname("geom_rect",
rectGrob(coords$xmin, coords$ymax,
width = coords$xmax - coords$xmin, height = coords$ymax -
coords$ymin, default.units = "native", just = c("left", "top"),
gp = gpar(col = coords$colour,
fill = alpha(coords$fill, coords$alpha),
lwd = coords$size * .pt,
lty = coords$linetype,
lineend = "butt"))) # look here
}
}
The creation of a geom_tile
layer is powered by rectGrob
, with a hard-coded lineend
parameter value of "butt". The graphic below (found here) illustrates the difference between the 3 lineend
values nicely:
If you feel like digging into the underlying GeomTile
's functions and changing the graphics parameters for all geom_tile
layers in your code, you can do that. (I answered a similar question recently with that solution.) For a single plot, though, I'd just convert the ggplot to a grob object, & mess with the gp
parameters there instead:
library(grid)
gp <- ggplotGrob(p)
grid.draw(gp)
# this "sharpens" the top left corner
gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$lineend <- "square"
grid.draw(gp)
# this further "sharpens" the other three corners
gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$linejoin <- "mitre"
grid.draw(gp)
Note: the actual location of the correct grob corresponding to geom_tile
is not necessarily going to be gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$linejoin
. It's children[[3]]
here, but having other geom layers in the ggplot object, either under or above the geom_tile
layer, can shift its relative position. In that case, you may want to check the output from gp$grobs[[which(grepl("panel", gp$layout$name))]]$children
in the console to identify the correct position number.