ggplot2 facet_调用中的(重新)名称因子级别(或包括变量名称) [英] (Re)name factor levels (or include variable name) in ggplot2 facet_ call

查看:73
本文介绍了ggplot2 facet_调用中的(重新)名称因子级别(或包括变量名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常做的一种模式是在数值切割上刻面绘图.ggplot2中的facet_wrap不允许您从内部调用函数,因此您必须创建一个临时因子变量.使用dplyr的mutate可以.这样做的好处是您可以进行EDA并更改分位数,或者更改为设置切点等,然后在一行中查看更改.不利的一面是,这些方面仅通过因子级别进行标记;例如,您必须知道这是温度.这对自己来说还不错,但是如果我在两个这样的变量上执行facet_grid并必须记住哪个是哪个,我也会感到困惑.因此,能够通过包含有意义的名称来重新标记构面真的很不错.

One pattern I do a lot is to facet plots on cuts of numeric values. facet_wrap in ggplot2 doesn't allow you to call a function from within, so you have to create a temporary factor variable. This is okay using mutate from dplyr. The advantage of this is that you can play around doing EDA and varying the number of quantiles, or changing to set cut points etc. and view the changes in one line. The downside is that the facets are only labelled by the factor level; you have to know, for example, that it's a temperature. This isn't too bad for yourself, but even I get confused if I'm doing a facet_grid on two such variables and have to remember which is which. So, it's really nice to be able to relabel the facets by including a meaningful name.

这个问题的关键是,水平会随着分位数等的改变而改变.您不知道他们事先准备了什么.您可以使用基本的level()函数,但这意味着使用cut变量扩展数据框,然后调用level(),然后将此扩展的数据框传递给ggplot().

The key points of this problem is that the levels will change as you change the number of quantiles etc.; you don't know what they are in advance. You could use the base levels() function, but that means augmenting the data frame with the cut variable, then calling levels(), then passing this augmented data frame to ggplot().

因此,使用plyr :: mapvalues,我们可以将所有这些包装到dplyr :: mutate中,但是mapvalues()所需的参数使其非常笨拙.不得不多次键入"Temp.f"不是很"dplyr"!

So, using plyr::mapvalues, we can wrap all this into a dplyr::mutate, but the required arguments for mapvalues() makes it quite clunky. Having to retype "Temp.f" many times is not very "dplyr"!

是否有一种更整洁的方式即时"重命名此类因子水平?我希望这个描述足够清楚,下面的代码示例对您有所帮助.

Is there a neater way of renaming such factor levels "on the fly"? I hope this description is clear enough and the code example below helps.

library(ggplot2)
library(plyr)
library(dplyr)
library(Hmisc)
df <- data.frame(Temp = seq(-100, 100, length.out = 1000), y = rnorm(1000))

# facet_wrap doesn't allow functions so have to create new, temporary factor
# variable Temp.f
ggplot(df %>% mutate(Temp.f = cut2(Temp, g = 4))) + geom_histogram(aes(x = y)) + facet_wrap(~Temp.f)
# fine, but facet headers aren't very clear,
# we want to highlight that they are temperature
ggplot(df %>% mutate(Temp.f = paste0("Temp: ", cut2(Temp, g = 4)))) + geom_histogram(aes(x = y)) + facet_wrap(~Temp.f)
# use of paste0 is undesirable because it creates a character vector and
# facet_wrap then recodes the levels in the wrong numerical order

# This has the desired effect, but is very long!
ggplot(df %>% mutate(Temp.f = cut2(Temp, g = 4), Temp.f = mapvalues(Temp.f, levels(Temp.f), paste0("Temp: ", levels(Temp.f))))) + geom_histogram(aes(x = y)) + facet_wrap(~Temp.f)

推荐答案

我认为您可以使用自定义标签程序功能在 facet_wrap 中完成此操作,

I think you can do this from within facet_wrap using a custom labeller function, like so:

myLabeller <- function(x){
  lapply(x,function(y){
    paste("Temp:", y)
  })
}

ggplot(df %>% mutate(Temp.f = cut2(Temp, g = 4))) +
  geom_histogram(aes(x = y)) +
  facet_wrap(~Temp.f
             , labeller = myLabeller)

那个贴标机笨拙,但至少是一个例子.您可以为每个要使用的变量(例如 tempLabeller yLabeller 等)编写一个.

That labeller is clunky, but at least an example. You could write one for each variable that you are going to use (e.g. tempLabeller, yLabeller, etc).

稍作调整后,效果会更好:它会自动使用您要描述的事物的名称:

A slight tweak makes this even better: it automatically uses the name of the thing you are facetting on:

betterLabeller <- function(x){
  lapply(names(x),function(y){
    paste0(y,": ", x[[y]])
  })
}

ggplot(df %>% mutate(Temp.f = cut2(Temp, g = 4))) +
  geom_histogram(aes(x = y)) +
  facet_wrap(~Temp.f
             , labeller = betterLabeller)

这篇关于ggplot2 facet_调用中的(重新)名称因子级别(或包括变量名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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