本文介绍了用ggplot2翻转刻面标签和x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在翻看1行和5列的小平面面板上的标签,以便小平面标题出现在底部,并且x轴出现在小平面的顶部。 原因是我想重复使用这些标题作为直接位于图下方的表。 所以在这个例子中... library(ggplot2) my.hist< -ggplot (diamonds,aes(clarity))+ geom_bar() my.hist + facet_wrap(〜cut,ncol = 5)+ coord_flip() pre> 我希望剪切标签显示在图表下方。我在想,facet_grid可能会把钥匙,但只是一个猜测。 任何人都知道如何做到这一点? $ b library(gtable)>解决方案 )g< - ggplotGrob(p) strip< - gtable_filter(g,strip_t,trim = FALSE) grid.newpage() grid .draw(rbind(g,strips [3,],size =first)) 但是,轴需要更多的关注,因为必须扭转刻度标记和标记的位置。您可以从这开始, tweak_axis< - function(a){ inner< - a [ [children]] [axis] [[1]] inner [[grobs]] inner $ grobs [ [2]] $ y a [[children]] [axis] [[1]]< ; - 内部a } 轴< - gtable_filter(g,axis_b,trim = FALSE)轴$ grobs< - lapply(axes $ grobs,tweak_axis) grid.newpage() grid.draw(斧头) 编辑:根据以上所述,完整解决方案可能是 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)] #高度也应该改变,但是这里可以确定 xlab grid.draw(rbind(g [xlab,],g [-c(title,xlab),],size =last)) (有明显的警告) I am looking to flip the labels on a faceted panel of 1 row and 5 columns, so that the facet headers appear on bottom, and the x axis appears on top of facets.The reason is that I want to reuse those headers for a table that will be directly below the graph.So in this example...library(ggplot2)my.hist<-ggplot(diamonds, aes(clarity)) + geom_bar()my.hist + facet_wrap( ~ cut, ncol=5) + coord_flip()I would want the "cut" labels to show up below the chart. I was thinking that facet_grid might hold the key, but is only a guess.Anyone know how to accomplish this? 解决方案 Getting the facet strips below the plot is easy,library(gtable)g <- ggplotGrob(p)strips <- gtable_filter(g, "strip_t", trim=FALSE)grid.newpage()grid.draw(rbind(g, strips[3,], size="first"))the axes, however, require more care because one has to reverse the position of the tick marks and labels. You can maybe start with this,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)Edit: based on the above, a "complete" solution might begrid.newpage()g2 <- gnew_axes <- lapply(g2$grobs[grepl("axis_b", g2$layout$name)], tweak_axis)g$grobs[grepl("strip_t", g$layout$name)] <- new_axesg$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 herexlab <- 7; title <- 1:2grid.draw(rbind(g[xlab,], g[-c(title, xlab), ], size="last"))(with obvious caveats) 这篇关于用ggplot2翻转刻面标签和x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-17 22:16