如何调整geom拼贴中的拼贴高度? [英] How to adjust the tile height in geom tile?

查看:141
本文介绍了如何调整geom拼贴中的拼贴高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图用ggplot2做一个情节,但我正在努力与geom拼图。当我第一次使用这个geom时,我仔细查看了Hadley的文档,但是我仍然没有设法得到我所追求的。我想调整瓷砖宽度和瓷砖高度。我发现如何调整文档中的瓦片宽度,但我正在为高度而努力。以下图为出发点:$ b​​
$ b

  test<  -  data.frame(
x = rep(c(1 ,
y = rep(c(1,3),each = 3),
w = rep(c(.5,2,1),2),
z = sample(rep(LETTERS [1:6])))

ggplot(test,aes(x = x,y = y,fill = z))+ geom_tile(aes(width = w))


现在我想调整瓷砖的高度。在第一个列(其中x = 1)的底部拼贴从0运行到1,并且第一列中的顶部拼贴运行从1到4.在第二列中,我想使底部拼贴运行从0到3,顶部从3到4.最后一列我希望底部从0到1.5,顶部从1.5到4.我尝试了很多东西,例如如下:

  test2<  -  data.frame(
x = rep(c(1,3,6) ,2),
y = c(0,0,0,1,3,1.5),
w = rep(c(.5,2,1),2),
z = sample (代表[LETTERS [1:6])),
h = c(1,3,1.5,3,1,2.5))

ggplot(test2,aes(x = x, y = y,fill = z))+ geom_tile(aes(width = w,heigth = h))



<但没有一个产生了我正在寻找的阴谋。



任何帮助将不胜感激。

解决方案

geom_tile c> height = h 并从生成 ymin ymax -h / 2到h / 2 。这就是为什么你没有得到你打算的阴谋。尽管我完全支持@Didzis的解决方案,因为它非常简单并且完成了工作,所以我将使用 geom_tile 显示解决方案,原因有两个。这很有趣,总是很高兴知道:)。/ b>

目标是相应地生成你的 y em>取决于高度,以便剧情符合您的预期。以 test2 data.frame,

  require(plyr)$ b $ (b)计算y坐标,由于h 
test2 < - ddply(test2,。(x),transform,y2 = c(0,head(h,-1))+ h / 2 )
p <-ggplot(test2,aes(x = x,y = y2,fill = z))+
geom_tile(aes(width = w,height = h))
p



现在,您会看到高度整齐地生成(ymin和ymax)

  ggplot_build(p)$ data 

#fill xy面板组xmin xmax ymin ymax
#1#00BFC4 1 0.50 1 4 0.75 1.25 0.0 1.0
#2#619CFF 1 2.50 1 5 0.75 1.25 1.0 4.0
#3#00BA38 3 1.50 1 3 2.00 4.00 0.0 3.0
#4#F8766D 3 3.50 1 1 2.00 4.00 3.0 4.0
#5#B79F00 6 0.75 1 2 5.50 6.50 0.0 1.5
#6#F564E3 6 2.75 1 6 5.50 6.50 1.5 4.0


I am trying to do a plot with ggplot2, but I am struggling with the geom tile. As I am using this geom for the first time I looked closely to Hadley's documentation, but still I did not manage to get what I am after. I want to adjust both the tile width and the tile height. I found how to adjust the tile width in the documentation, but I am struggling with the height. Taking the next plot as starting point:

test <- data.frame(
  x = rep(c(1,3,6),2),
  y = rep(c(1,3), each = 3),
  w = rep(c(.5,2,1), 2),
  z = sample(rep(LETTERS[1:6])))

ggplot(test, aes(x=x, y=y, fill = z)) + geom_tile(aes(width = w))

I would now like to adjust the height of the tiles as well. With the bottom tile in the first 'column' (at which x = 1) to run from 0 to 1, and the top tile in the first column running from 1 to 4. In the second column I want to make the bottom tile run from 0 to 3 and the top tile from 3 to 4. For the last column I want the bottom from 0 to 1.5 and the top from 1.5 to 4. I tried many things, e.g. the following:

test2 <- data.frame(
 x = rep(c(1,3,6),2),
 y = c(0, 0, 0, 1, 3, 1.5),
 w = rep(c(.5,2,1), 2),
 z = sample(rep(LETTERS[1:6])),
 h = c(1, 3, 1.5, 3, 1, 2.5))

ggplot(test2, aes(x=x, y=y, fill = z)) + geom_tile(aes(width = w, heigth = h))

but none produced the plot I was looking for.

Any help will be greatly appreciated. Thanks in advance!

解决方案

geom_tile takes the aesthetic height=h and generates ymin and ymax from -h/2 to h/2. This is why you aren't getting plot you intend. While I fully support @Didzis' solution as it is extremely straightforward and gets the job done, I'll show the solution using geom_tile for two reasons. It's fun and always nice to know :).

The objective is to "generate" your y positions accordingly depending on the height so that the plot is as you expect. Taking test2 data.frame,

require(plyr)
# calculate y coordinate accounting for shift in y due to h
test2 <- ddply(test2, .(x), transform, y2 = c(0, head(h,-1)) + h/2)
p <- ggplot(test2, aes(x=x, y=y2, fill = z)) + 
           geom_tile(aes(width = w, height=h))
p

Now, you see that the heights are neatly generated (ymin and ymax)

ggplot_build(p)$data

#      fill x    y PANEL group xmin xmax ymin ymax
# 1 #00BFC4 1 0.50     1     4 0.75 1.25  0.0  1.0
# 2 #619CFF 1 2.50     1     5 0.75 1.25  1.0  4.0
# 3 #00BA38 3 1.50     1     3 2.00 4.00  0.0  3.0
# 4 #F8766D 3 3.50     1     1 2.00 4.00  3.0  4.0
# 5 #B79F00 6 0.75     1     2 5.50 6.50  0.0  1.5
# 6 #F564E3 6 2.75     1     6 5.50 6.50  1.5  4.0

这篇关于如何调整geom拼贴中的拼贴高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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