'顶部的标签'带有facet_grid,或'空间选项'带有facet_wrap [英] 'Labels on top' with facet_grid, or 'space option' with facet_wrap

查看:198
本文介绍了'顶部的标签'带有facet_grid,或'空间选项'带有facet_wrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

facet_grid 允许我根据y轴上的项目数( space 参数)来确定每个构面宽度的大小:

  df < -  data.frame(label = c(Variable one,rep(Variable two,2) ,rep(变量三,3)),item = c(A,B,C,D,E,F),value = rnorm(6))
ggplot(df,aes(x = value,y = item))+
geom_point()+
facet_grid(label〜。,scales =free_y,space =free_y)+
ylab()+
主题(strip.text.y = element_text(angle = 0))



<但是我想在顶部使用facet标签,所以我切换到 facet_wrap ,并且失去了空间参数(facet具有相同的宽度):

  ggplot(df,aes(x = value,y = item))+ 
geom_point()+
facet_wrap(〜label,scales =free_y,ncol = 1)+
ylab()



是否有可能获得最佳两个世界?



预先感谢您的帮助。

解决方案

手动完成。所需图中三个面板的高度比大致为1:3:2。通过更改grobs可以调整三个面板的高度:

$ p $ library $ g
library(grid)
df < - data.frame(label = c(Variable one,rep(Variable two,2),rep(Variable three,3)),item = c(A, (b),C,D,E,F),value = item))+
geom_point()+
facet_wrap(〜label,scales =free_y,ncol = 1)+
ylab()

g1 = ggplotGrob(p1)

g1 $ heights [[7]] = unit(1,null)
g1 $ heights [[12]] =单位(3,null )
g1 $ heights [[17]] = unit(2,null)

grid.newpage()
grid.draw(g1)

或者,高度可以设置为与原始图中的高度相同:

  p2 = ggplot(df,aes(x = value,y = item))+ 
geom_point()+
facet_grid(label〜 (strip.text.y = element_text(angle = 0))

g2 = ggplotG rob(p2)

g1 $ heights [[7]] = g2 $ heights [[6]]
g1 $ heights [[12]] = g2 $ heights [[8]]
g1 $ heights [[17]] = g2 $ heights [[10]]

grid.newpage()
grid.draw(g1)

或者,高度可以在不参考原始图的情况下设置。可以根据 df 标签项数 C $ C>。并从 @ baptiste的答案中借用一些代码在这里从布局中选择相应面板中的项目:

 #从'df',获取数字每个'标签'的'项目'。 
#也就是说,每个面板中的数字y-break。
library(plyr)
N = dlply(df,。(label),function(x)length(row.names(x)))

#与面板相对应的g1布局。
panels1< - g1 $ layout $ t [grepl(panel,g1 $ layout $ name)]

#将相对高度的默认面板高度替换为
g1 $高度[panels1]< - unit(N,null)

## Draw g1
grid.newpage()
grid.draw(g1)


facet_grid allow me to size each facet width according to number of items on y axis (space argument):

df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

But I would like facet labels on top, so I switch to facet_wrap, and lost the space argument (facets have all the same width):

ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

Is it possible to obtain the best of both worlds?

Thank you in advance for your help.

解决方案

It can be done manually. The ratio of the heights of the three panels in the desired plot is roughly 1:3:2. The heights of the three panels can be adjusted by changing the grobs:

library(ggplot2)
library(grid)
df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))

p1 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

g1 = ggplotGrob(p1) 

g1$heights[[7]] = unit(1, "null")
g1$heights[[12]] = unit(3, "null")
g1$heights[[17]] = unit(2, "null")

grid.newpage()
grid.draw(g1)

Or, the heights can be set to be the same as those in the original plot:

p2 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

g2 = ggplotGrob(p2) 

g1$heights[[7]] = g2$heights[[6]]
g1$heights[[12]] = g2$heights[[8]]
g1$heights[[17]] = g2$heights[[10]]

grid.newpage()
grid.draw(g1)

Or, the heights can be set without reference to the original plot. They can be set according to the number of items for each label in df. And borrowing some code from @baptiste's answer here to select the items from the layout corresponding to the panels:

# From 'df', get the number of 'items' for each 'label'.
# That is, the number y-breaks in each panel.
library(plyr)
N = dlply(df, .(label), function(x) length(row.names(x)))

# Get the items in the g1 layout corresponding to the panels.
panels1 <- g1$layout$t[grepl("panel", g1$layout$name)]

# Replace the default panel heights with relative heights
g1$heights[panels1] <- unit(N, "null")

## Draw g1
grid.newpage()
grid.draw(g1)

这篇关于'顶部的标签'带有facet_grid,或'空间选项'带有facet_wrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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