调整ggplot2几何图块的高度和宽度 [英] adjust ggplot2 geom tile height and width

查看:1820
本文介绍了调整ggplot2几何图块的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缩小我的geom_tile的高度和宽度。

我意识到我的问题类似于
,但我的y值是字符串,而不是坐标,所以我不知道如何调整我的y值根据高度。

我把高度和宽度缩小了0.5,但是它在瓷砖之间产生了灰色空间。有什么方法可以去除灰色空间,以便瓷砖相邻?

 #数据框
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))
$ b $#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,color =black)))

只要删除 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,color =black)))


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")))

解决方案

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")))

这篇关于调整ggplot2几何图块的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆