Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw geom_tile borders inside squares to prevent overlap

Tags:

r

ggplot2

I would like to be able to draw borders on geom_tile that do not overlap so that borders can convey their own information without confusing the viewer with disappearing borders.

library(ggplot2)

state <- data.frame(p=runif(100), x=1:10, y=rep(1:10, each=10), z=rep(1:5, each=20))

ggplot(state, aes(x, y)) + 
  geom_tile(aes(fill = p, color=as.factor(z)), size=2)

geom_tile plot with overlapping borders

I trust you can see how confusing overlapping borders can be.

like image 955
Francis Smart Avatar asked Feb 11 '16 17:02

Francis Smart


1 Answers

Use the width and height arguments to geom_tile to create space and prevent border overlap.

ggplot(state, aes(x, y)) + 
  geom_tile(aes(fill = p, color=as.factor(z), width=0.7, height=0.7), size=2)

I've created space between the tiles, but you can also adjust the width and height so that the borders just touch each other. Also, note that in my version of the graph the color legend for tiles does not have any colors. This is because the border color legend for geom_tile is broken in ggplot2 2.0. The problem has been fixed in the development version of ggplot2, but the fix hasn't been rolled out to CRAN yet. To get the development version, you can do devtools::install_github("hadley/ggplot2").

(Incidentally, if you're into optical illusions, the graph below creates the grid illusion).

enter image description here

like image 92
eipi10 Avatar answered Oct 19 '22 14:10

eipi10