OP. I noticed that in your response to another answer, you've refined your question a bit. I would recommend you edit your original question to reflect some of what you were looking to do, but here's the overall picture to summarize what you wanted to know:
- How to remove the gray space between tiles
- How to make the tiles smaller
- How to make the tiles more square
Here's how to address each one in turn.
How to remove gray space between tiles
This was already answered in a comment and in the other answer from @dy_by. The tile
geom has the attribute width
which determines how big the tile is relative to the coordinate system, where width=1
means the tiles "touch" one another. This part is important, because the size of the tile is different than the size of the tile relative to the coordinate system. If you set width=0.4
, then the size of the tile is set to take up 40% of the area between one discrete value in x and y. This means, if you have any value other than width=1
, then you will have "space" between the tiles.
How to make the tiles square
The tile
geom draws a square tile, so the reason that your tiles are not square in the output has nothing to do with the geom - it has to do with your coordinate system and the graphics device drawing it in your program. By default, ggplot2
will draw your coordinate system in an aspect ratio to match that of your graphics device. Change the size of the device viewport (the window), and the aspect ratio of your coordinate system (and tiles) will change. There is an easy way to fix this to be "square", which is to use coord_fixed()
. You can set any aspect ratio you want, but by default, it will be set to 1 (square).
How to make the tiles smaller
Again, the size of your tiles is not controlled by the geom_tile()
function... or the coordinate system. It's controlled by the viewport you set in your graphics device. Note that the coordinate system and geoms will resize, but the text will remain constant. This means that if you scale down a viewport or window, your tiles will become smaller, but the size of the text will (relatively-speaking) seem larger. Try this out by calling ggsave()
with different arguments for width=
with your plot.
Putting it together
Therefore, here's my suggestion for how to change your code to fix all of that. Note I'm also suggesting you change the theme to theme_classic()
or something similar, which removes the gridlines by default and the background color is set to white. It works well for tile maps like this.
p <- ggplot(df, aes(x=variable, y=id, label=value, fill=as.factor(value))) +
geom_tile(colour="white", alpha=0.2, width=1) +
scale_fill_manual(values=c("yellow", "orange", "red", "green", "grey")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(x = "Value", y="id") +
scale_x_discrete(expand=c(0,0))+
scale_y_discrete(expand=c(0,0)) +
coord_fixed() +
theme_classic()
p
Now for saving that plot with different width=
settings to show you how things change for sizing. You don't have to specify height=
, since the aspect ratio is fixed at 1.
ggsave("example_big.png", plot=p, width=12)

ggsave("example_small.png", plot=p, width=3)
