本文介绍了在使用facet_wrap时重新排序geom_bar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 一个简单的例子: library(ggplot2) library(plyr) df val = c(1,2,7,4,5,3,1), group = c(1,1,1,2,2,2)) p1 p1 $ b $ p2 p2 p2不会生成我想要的,因为并非每个因子级别都出现在所有面板中。是否有一个简单的解决方案已经解决了这个问题? 我找到的一个解决方案如下(计算每个组的每个因子级别的排名)。 $ b df2 df2 $ fac2 p3 opts(panel.margin = unit(2,lines)) p3 我必须在这里设置标签。一个可能的解决方案如下(硬编码为这个例子): grid.newpage() grob< --ggplotGrob(p3) object.path< - grid.ls(getGrob(grob,axis.text.y,grep = TRUE,global = TRUE),print = FALSE)$ name grob< - grid :: editGrob(grob,object.path [1],label = c(ABDQ,M,A,B)) grob< - grid :: editGrob(grob,object.path [2],label = c(A,B,EEEEX)) grid.draw(grob) 但还有另一个问题。我现在必须由我自己设置panel.margin,这似乎是不可能的,因为我只能设置一个全局panel.margin,我需要的是所有4边(或在至少2)。 问题1:是否有简单的解决方案使用重新订购? 问题2:是否有一个解决方案使用scale_x_discrete获取所需的轴? 问题3:我无法找到所需的网格对象来操作panel.margins的视口。有没有简单的方法来操纵适当的网格对象? 有什么想法? 解决方案我认为 grid.arrange 是一个更好的工具,而不是试图将免费的scale放到faceting框架中: geom_bar(gridExtra) q1< - ggplot ()+ facet_wrap(〜group)+ coord_flip() q2 grid.arrange(q1,q2,nrow = 1) What I try to achieve is having the bars ordered by a given variable per panel.A simple example: library(ggplot2) library(plyr) df <- data.frame(fac = c("a", "b", "c", "e", "b", "c", "d"), val = c(1, 2, 7, 4, 5, 3, 1), group = c(1, 1, 1, 1, 2, 2, 2)) p1 <- ggplot(df, aes(x = fac, y = val)) + geom_bar() + facet_wrap(~ group, scales = "free") + coord_flip() p1 p2 <- ggplot(df, aes(x = reorder(fac, val), y = val)) + geom_bar() + facet_wrap(~ group, scales = "free") + coord_flip() p2p2 does not produce what I want because not every "factor level" appears in all panels. Is there a simple solution already for this problem?One solution I found is the following (calculates the rank for each factor level per group).df2 <- ddply(df, .(group), transform, fac2 = rank(val))df2$fac2 <- factor(df2$fac2)p3 <- ggplot(df2, aes(x = fac2, y = val)) + facet_wrap(~ group, scales = "free") + geom_bar(stat = "identity") + coord_flip() + opts(panel.margin = unit(2, "lines"))p3I have to set the labels by my self here. One possible solution is the following (hard coded for this example):grid.newpage()grob <- ggplotGrob(p3)object.path <- grid.ls(getGrob(grob, "axis.text.y", grep = TRUE, global = TRUE), print = FALSE)$namegrob <- grid::editGrob(grob, object.path[1], label = c("ABDQ", "M", "A", "B"))grob <- grid::editGrob(grob, object.path[2], label = c("A", "B", "EEEEX"))grid.draw(grob)But there is another problem. I have to set the panel.margin by my self now and it seems to be not possible to do this as I'm only able to set a "global" panel.margin and what I need is one for all 4 sides (or at least 2).Question 1: Is there a simple solution using reorder?Question 2: Is there a solution using scale_x_discrete to get the needed axis?Question 3: I wasn't able to find the needed grid object to manipulate the viewports for the panel.margins. Is there an easy way to manipulate the appropriate grid object?Any ideas? 解决方案 I think that grid.arrange is a much better tool for this than trying to shoehorn free scales into a faceting framework:library(gridExtra)q1 <- ggplot(subset(df,group == 1),aes(x = reorder(fac,val),y = val)) + geom_bar() + facet_wrap(~group) + coord_flip()q2 <- q1 %+% subset(df,group == 2)grid.arrange(q1,q2,nrow = 1) 这篇关于在使用facet_wrap时重新排序geom_bar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-01 23:06