本文介绍了R:使用ggplot2中的free_x对facet_wrapped x轴重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在一个facet-wrapped图中使用 reorder ,它也在ggplot2中使用 scales = free_x ,但重新排序功能不会正确地重新排列x轴。以下是我正在运行的程序: library(ggplot2) df< - read.table (speaking_distribution_by_play.txt, header = F, sep =\ t) ggplot(df,aes(x = reorder(V2,V3), y = V3))+ geom_bar(stat =identity)+ facet_wrap(〜V1,ncol = 4,scales =free_x)+ opts(title =Distribution of莎士比亚戏剧中的演员)+ xlab(演讲角色)+ ylab(口语)+ opts(axis.text.x = theme_text(angle = 90,hjust = 1)) 从这个制表符分隔的文件会产生一个图形,其中每个分面图的x轴只是部分排列的。 其他人对此提出了一个非常类似的问题,但唯一的提议解决方案是使用网格排列。因为我的数据集比那个问题中的数据集大得多,但是这不会是一个非常快速的操作,所以我想问一下:有没有办法重新排列每个分面图的x轴的顺序,以增加(或减少)大小顺序来显示条形?我非常感谢他人在这个问题上提供的任何帮助。 问题是 ggplot 将 V2 视为单因子;它不是为每个方面(值 V1 )的 V2 子集,然后将每个方面作为独立因素处理)。由于某些角色(信使1等)出现在多个游戏中,因此这些级别根据他们在第一次玩游戏时的重要性排序。 有一个解决方法,但这有些破绽:您需要通过将播放的名称连接到每个角色来使角色唯一,然后将其用作x值。要恢复原始角色,请关闭轴文本,并使用 geom_text(...)作为条形标签。这里是一个例子: gg ggplot(gg [gg $ V1%in%unique (df $ V1)[1:4],], aes(x = factor(lvl,levels = unique(lvl)),y = V3))+ geom_text(aes(y = 5 ,label = V2),angle = 90,size = 3,hjust = -0)+ geom_bar(stat =identity,fill =blue,alpha = 0.2)+ facet_wrap(〜 V1,ncol = 2,scales =free_x)+ 实验室(title =莎士比亚戏剧中演讲者的分布,x =演讲角色,y =口语)+ 主题(axis.text.x = element_blank(),axis.ticks.x = element_blank()) 这看起来很糟糕小规模(不像你原来的情节那样糟糕,尽管...)。但是如果你把它做得更大(因为你需要做38次,没有??),那么你可以看到标签和酒吧。如果你真的想要在酒吧下面的标签,使用这样的: ggplot(gg [gg $ V1%in%unique (df $ V1)[1:4],], aes(x = factor(lvl,levels = unique(lvl)),y = V3))+ geom_text(aes(y = - 5,label = V2),angle = 90,size = 3,hjust = 1)+ ylim(-500,NA)+ geom_bar(stat =identity,fill =lightblue) + facet_wrap(〜V1,ncol = 2,scales =free_x)+ labs(title =莎士比亚戏剧中演讲者的分布,x =演讲角色,y = Words Spoken)+ theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) geom_text(...)中的 size = ... 参数。 I'm trying to use reorder in a facet-wrapped plot that also uses scales = free_x in ggplot2, but the reorder function isn't reordering the x-axis properly. Here's what I'm running:library(ggplot2)df <- read.table("speaking_distribution_by_play.txt", header = F, sep = "\t")ggplot(df, aes(x=reorder(V2, V3), y=V3)) + geom_bar(stat = "identity") + facet_wrap(~V1, ncol = 4, scales = "free_x") + opts(title = "Distribution of Speakers in Shakespearean Drama") + xlab("Speaking Role") + ylab("Words Spoken") + opts(axis.text.x=theme_text(angle=90, hjust=1))Running that code on the data frame read from this tab-separated file yields a plot in which the x-axis of each faceted plot is only partially ordered. Someone else on SO asked a very similar question, but the only proposed solution was to use grid arrange. Because my data set is quite a bit larger than the data set in that question, though, this won't be a terribly swift operation, so I wanted to ask: Is there a way to reorder the x axis of each faceted plot so as to show the bars in increasing (or decreasing) order of size? I would be very grateful for any help others can offer on this question. 解决方案 The problem is that ggplot treats V2 as a single factor; it does not subset V2 for each facet (value of V1) and then treat each of those as independent factors (unfortunately). Since some of the roles ("Messenger 1", and so on), appear in more than one play, those levels are ordered based on their importance in the first play in which they are encountered.There is a workaround, but it's a bit of a hack: you need to make the roles unique by concatenating the name of the play to each, then use that as the x-value. To get the original roles back, turn off the axis text and instead use geom_text(...) for the bar labels. Here is an example:gg <- df[order(df$V1,-df$V3),] # reorder by play and linesgg$lvl <- with(df,paste(V2,V1,sep="."))ggplot(gg[gg$V1 %in% unique(df$V1)[1:4],], aes(x=factor(lvl,levels=unique(lvl)), y=V3)) + geom_text(aes(y=5,label=V2),angle=90,size=3,hjust=-0)+ geom_bar(stat = "identity", fill="blue",alpha=0.2) + facet_wrap(~V1, ncol = 2, scales="free_x") + labs(title="Distribution of Speakers in Shakespearean Drama", x="Speaking Role", y="Words Spoken") + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())This looks awful at such a small scale (not as bad as your original plot, though...). But if you make it larger (as you will have to do with 38 plays, no??), then you can see the labels and the bars. If you really want the labels below the bars, use something like this:ggplot(gg[gg$V1 %in% unique(df$V1)[1:4],], aes(x=factor(lvl,levels=unique(lvl)), y=V3)) + geom_text(aes(y=-5,label=V2),angle=90,size=3,hjust=1)+ ylim(-500,NA)+ geom_bar(stat = "identity", fill="lightblue") + facet_wrap(~V1, ncol = 2, scales="free_x") + labs(title="Distribution of Speakers in Shakespearean Drama", x="Speaking Role", y="Words Spoken") + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())Again, looks awful at this small scale, but better enlarged. Either way you are likely to need to tweak the size=... parameter in geom_text(...). 这篇关于R:使用ggplot2中的free_x对facet_wrapped x轴重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 04:26