Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot2翻转面标签和x轴_R_Ggplot2_Axis Labels_Facet Wrap - Fatal编程技术网

R 使用ggplot2翻转面标签和x轴

R 使用ggplot2翻转面标签和x轴,r,ggplot2,axis-labels,facet-wrap,R,Ggplot2,Axis Labels,Facet Wrap,我希望翻转1行5列的刻面面板上的标签,以便刻面标题显示在底部,x轴显示在刻面顶部 原因是,我希望将这些头重新用于图形正下方的表 所以在这个例子中 library(ggplot2) my.hist<-ggplot(diamonds, aes(clarity)) + geom_bar() my.hist + facet_wrap( ~ cut, ncol=5) + coord_flip() 库(ggplot2) my.hist获取绘图下方的镶嵌条很容易 library(gtable) g

我希望翻转1行5列的刻面面板上的标签,以便刻面标题显示在底部,x轴显示在刻面顶部

原因是,我希望将这些头重新用于图形正下方的表

所以在这个例子中

library(ggplot2)

my.hist<-ggplot(diamonds, aes(clarity)) + geom_bar()

my.hist + facet_wrap( ~ cut, ncol=5) + coord_flip()
库(ggplot2)

my.hist获取绘图下方的镶嵌条很容易

library(gtable)
g <- ggplotGrob(p)

strips <- gtable_filter(g, "strip_t", trim=FALSE)
grid.newpage()
grid.draw(rbind(g, strips[3,], size="first"))
库(gtable)

我不知道这个问题的答案在或之后发生了变化。看起来像是坏消息。。。可能是离题了,但我想知道lattice是否提供了此功能。kohske为轴发布了一些技巧:如果我使用
“strip\u t”
,然后运行
strips
,我会得到输出
TableGrob(6 x 5)“布局”:0 grobs
。这是预期的效果吗?我这样问是因为我在最终输出中也得到了一个错误,
error in mmm
。但是,我正在用我的
my.plot
替换您代码中的
p
。这是正确的用法吗?您应该在该表格中有5个GROB,确保您的绘图已更新为包含第二行(
facet\u wrap
)。我已经更新了代码来处理另一个错误。好的,谢谢你指出这一点。我不得不重新分配到更新的绘图,它提供了预期的输出。现在,我得到了刻面标签的预期输出,我将尝试追踪反转记号。很高兴让gridExtra作者回答我的问题。非常感谢!这太棒了!我很感谢您将该功能包装成一个函数。看起来肯定有内幕代码。我必须对此进行实验,以便理解您正在操作的内部结构,然后对该函数进行参数化。但是有一个问题,当使用此函数的代码中没有传递参数时,函数如何知道
(a)
?再次感谢!
tweak_axis <- function(a){
  inner <- a[["children"]]["axis"][[1]]
  inner[["grobs"]] <- rev(inner[["grobs"]])
  inner$grobs[[2]]$y <- inner$grobs[[2]]$y - unit(0.15, "cm")
  a[["children"]]["axis"][[1]] <- inner
  a
}

axes <- gtable_filter(g, "axis_b", trim=FALSE)
axes$grobs <- lapply(axes$grobs, tweak_axis)
grid.newpage()
grid.draw(axes)
grid.newpage()
g2 <- g
new_axes <- lapply(g2$grobs[grepl("axis_b", g2$layout$name)], tweak_axis)
g$grobs[grepl("strip_t", g$layout$name)] <- new_axes 
g$grobs[grepl("axis_b", g$layout$name)] <- g2$grobs[grepl("strip_t", g2$layout$name)] 
# heights should be changed too, but it's kind of ok here
xlab <- 7; title <- 1:2
grid.draw(rbind(g[xlab,], g[-c(title, xlab), ], size="last"))