在方块内绘制geom_tile边框以防止重叠

时间:2016-02-11 17:28:04

标签: r ggplot2

我希望能够在geom_tile上绘制不重叠的边框,以便边框可以传达自己的信息,而不会让观看者与边框消失。

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

我相信你可以看到重叠的边界有多混乱。

1 个答案:

答案 0 :(得分:14)

使用width height参数创建空格并防止边框重叠。

geom_tile

我在图块之间创建了空格,但您也可以调整ggplot(state, aes(x, y)) + geom_tile(aes(fill = p, color=as.factor(z), width=0.7, height=0.7), size=2) width,以便边框相互接触。此外,请注意,在我的图形版本中,图块的颜色图例没有任何颜色。这是因为height is broken in ggplot2 2.0的边框颜色图例。该问题已在ggplot2的开发版本中得到修复,但修复程序尚未推广到CRAN。要获得开发版本,您可以执行geom_tile

(顺便说一句,如果您遇到视错觉,下图会创建 grid illusion )。

enter image description here