ggplot - 按功能输出方面 [英] ggplot - facet by function output

查看:251
本文介绍了ggplot - 按功能输出方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何处理 ggplot 对象的 data 元素中的数据函数。在下面的玩具例子中,我想要做的是这样的:

I'm unsure how to facet by a function of the data in the data element of a ggplot object. In the following toy example, what I want to do is something like this:

df <- data.frame(x=1:8, y=runif(8), z=8:1)
ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap( ~ (z %% 2))

但是,出现错误: layout_base(data,vars, drop = drop):至少有一个图层必须包含所有用于构图的变量

我可以通过变换数据框来获得所需的结果:

I can achieve the desired result by transforming the data frame:

ggplot(transform(df, z=z%%2), aes(x=x, y=y)) + geom_point() + facet_wrap( ~ z)

但通常不希望使用这种转换,例如,如果我已经获得了一个 ggplot 对象,并且我想添加一些专门的分面。

but often it's desirable to not use such a transformation, for instance if I've already been given a ggplot object and I want to add some ad-hoc faceting to it.

推荐答案

这听起来很熟悉,但我从来没有设法解决它 - 我认为facet变量处理不如审美变量手强大ling。

this sounds familiar to me, but I never managed to fix it - I think facet variable handling is just less powerful than aesthetic variable handling.

解决您的根本需求 - 专门面向现有的ggplot;请注意,您可以替换批发现有 R ggplot的(主数据集) - 例如

Addressing your root requirement - to ad-hoc facet an existing ggplot; note that you can replace wholesale the (master) data set of an existing R ggplot - for instance

myplot %+% transform(myplot$data, z=z%%2)

这篇关于ggplot - 按功能输出方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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