adjust ggplot2 geom tile height and width

2019-09-09 02:43发布

I wanted to shrink the height and width of my geom_tile.

I realized my question is similar to How to adjust the tile height in geom tile?, but my y values are strings instead of coordinates so I am not sure how to adjust my y values based on height.

I shrink height and width by 0.5 but it creates "grey spaces" between the tiles. Is there any way to remove the "grey spaces" so that the tiles are adjacent to each other?

# data frame
fd=data.frame(x = rep(c("x","y","z"),3), 
              y=c("a","b","c","b","c","a","c","a","b"),
              z=c(0,1,0,1,1,1,0,0,1))

# plot
(p <- ggplot(fd, aes(x, y, height=.5, width=.5)) + geom_tile(aes(fill = z)) 
 + scale_fill_gradient(low = "white",high = "steelblue", limits=c(0,1)) 
 + theme_grey() 
 + labs(x = "", y= "") 
 + scale_x_discrete(expand = c(0, 0)) 
 + scale_y_discrete(expand = c(0, 0)) 
 + theme(legend.position = "none", axis.ticks = element_blank(), axis.text.x = element_text(size=12, angle=90, hjust=0, colour="black")))

Fig.1

标签: r ggplot2
1条回答
Explosion°爆炸
2楼-- · 2019-09-09 03:16

Just remove the aes(..., width=.5, height=.5)

(p <- ggplot(fd, aes(x, y)) + geom_tile(aes(fill = z)) 
 + scale_fill_gradient(low = "white",high = "steelblue", limits=c(0,1)) 
 + theme_grey() 
 + labs(x = "", y= "") 
 + scale_x_discrete(expand = c(0,0)) 
 + scale_y_discrete(expand = c(0,0)) 
 + coord_fixed(ratio=1)
 + theme(legend.position = "none", 
axis.ticks = element_blank(), 
axis.text.x = element_text(size=12, angle=90, hjust=0, colour="black")))

enter image description here

查看更多
登录 后发表回答