R:几个单独图的重排序因子水平 [英] R: reorder factor levels for several individual plots

查看:66
本文介绍了R:几个单独图的重排序因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从同一data.frame创建几个单独的图,每个图的y轴上的因子水平的顺序不同。每个图应该按因子y的顺序递减。



我知道可以为每个图手动完成此操作,但我正在寻找一种更高效,更优雅的方法因为我要创建很多图。如果有另一种方法(例如使用循环等),则不必包括使用facet_wrap?

 库(ggplot2)
库(dplyr)
数据(钻石)

获取数据集并按两个因子级别(明晰度和削减度)进行汇总:

 表示<-钻石%>%
group_by(净度,切工)%>%
summarise(克拉=均值(克拉)) )

在这里,我根据一个因素重新排序,但最终我想针对每个地块分别重新排序(通过降低清晰度)。

 意味着$ clarity<-重新排序(意味着$ clarity,表示$ carat,FUN =表示)

使用face_wrap创建单独的图。使用coord_flip可以更轻松地比较绘图。

  ggplot(平均值,aes(x =净度,y =克拉))+ 
geom_col()+
facet_wrap(〜cut,ncol = 1)+
coord_flip()

您可以看到,这为每种类型的切割创建了单独的图,但是在每种情况下y轴上因子水平的顺序都不正确。

解决方案

这可以在一个绘图中完成吗?通过使用两个函数:

  reorder_within<-function(x,by,inside,fun = mean,sep = ___ ,...){
new_x<-paste(x,inside,sep = sep)
stats :: reorder(new_x,by,FUN = fun)
}


scale_x_reordered<-函数(...,sep = ___){
reg<-paste0(sep,。+ $)
ggplot2 :: scale_x_discrete(labels = function(x)gsub(reg,,x),...)
}

可在GitHub


I am trying to create several individual plots from the same data.frame with a different order of the factor levels on the y axis for each plot. Each plot is supposed to order the factor levels on y decreasingly.

I know that this can be done manually for each plot but I am looking for a more efficient and elegant way since I'll have quite a number of plots that I need to create. This doesn't have to include the use of of facet_wrap, if there is another way, maybe with loops etc.?

library(ggplot2)
library(dplyr)
data("diamonds")

Taking the data set and aggregating by two factor levels (clarity and cut):

means <- diamonds %>%
group_by(clarity, cut) %>%
summarise(carat = mean(carat))

Here I reorder by mean of one factor but eventually I would like to reorder separately for each plot (by decreasing mean of clarity).

means$clarity <- reorder(means$clarity, means$carat, FUN = mean)

Creating separate plots with face_wrap. Using coord_flip to compare plots more easily.

ggplot(means, aes(x = clarity, y = carat)) +
  geom_col() +
  facet_wrap(~cut, ncol = 1) +
  coord_flip()

You'll see that this creates separate plots for each type of cut but the order of the factor levels on the y axis are not correct for each individual case. How can I order them correctly without having to do that manually for each type of cut?

解决方案

This can be done in one plot by utilizing two functions:

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}


scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

available on github dgrtwo/drlib

ggplot(means, aes(x = reorder_within(clarity, carat, cut, mean), y = carat)) + 
  geom_col() + 
  scale_x_reordered() +
  facet_wrap(~cut,  scales = "free_y", ncol = 1) +
  coord_flip()

这篇关于R:几个单独图的重排序因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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