分面时重复值到多个图 [英] Repeat values to multiple plots when faceting

查看:149
本文介绍了分面时重复值到多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的data.frame:

 > foo 
类别类型(0,10](10,20)(20,30)(30,40)
1 A NA NA 0.2 0.2 0.1 0.1
2 B NA> ; 0.7 0.1 0.1 0.1
3 C 1 0.5 0.4 0.1 0.0
4 C 2 0.5 0.3 0.1 0.1
5 D 1 0.7 0.3 0.0 0.0
6 D 2 0.7 0.2 0.0 0.1
7 E 1 0.4 0.3 0.2 0.1
8 E 2 0.5 0.3 0.1 0.1

我按课程类型融化并做一个barplot:

  ggplot(melt(foo,id = c(class, type)),aes(x = variable,y = value,fill = class))+ 
geom_bar(position =dodge)+
facet_grid(type〜。)


事实上, facet_grid()创建3个图表,但我想要的是,不知何故,为了忽略A级和A级。 B类型并在类型1和类型2的facet中显示它们,并且只获得2个图表(实际上,A和B值应该重复):


>

手动修改,并复制A和B类型1和2,因为我需要原始data.frame作为其他图形/测试。



(对不起,随机顺序我在家中使用的不同版本比在工作中的不同版本,不知道为什么会发生)

由于 ggplot 的原则,您将必须执行数据重复:图中的每个项目都恰好代表一个数据点。因此,如果您希望在两个方面具有 NA 数据,则需要为每个原始数据项创建两个数据点。



如果您想避免仅为绘图而显式创建临时数据,则可以创建一个函数来为您执行数据重复。按照以下内容:

  distribute.na.type<  -  function(dat){
rbind(
变换(子集(dat,类型%,%c(2,NA)),类型= 2)

}

上面的例子没有经过测试,也不是很通用,但运气好一点就行了。像这样使用它: distribute.na.type(melt(...))


I've a data.frame that looks like this:

> foo
        class      type    (0,10]    (10,20]    (20,30]    (30,40]
    1       A      <NA>       0.6        0.2        0.1        0.1
    2       B      <NA>       0.7        0.1        0.1        0.1
    3       C         1       0.5        0.4        0.1        0.0
    4       C         2       0.5        0.3        0.1        0.1
    5       D         1       0.7        0.3        0.0        0.0
    6       D         2       0.7        0.2        0.0        0.1
    7       E         1       0.4        0.3        0.2        0.1
    8       E         2       0.5        0.3        0.1        0.1

I melt by class & type and do a barplot:

ggplot(melt(foo, id=c("class", "type")), aes(x=variable, y=value, fill=class)) +
  geom_bar(position="dodge") +
  facet_grid(type ~.)

In fact, facet_grid() creates 3 graphs, but what I want is, somehow, to 'ignore' that class A & B are type and display them both in the facet for type 1 and type 2, and get only 2 graphs (A and B values should be, in fact, repeated):

I try to avoid modifying manually and duplicate A and B with type 1 and 2, because I need the original data.frame as is for other graphs/tests.

(sorry the random order of the columns, I'm using different versions at home than the ones at work and don't know why it happens)

解决方案

You will have to do the data duplication due to ggplot's philosophy: Each item in the plot represents exactly one data point. So if you want to have the NA data in two facets, you need to create two data points for each original data item.

If you want to avoid explicitly creating temporary data just for plotting, you can create a function that will do the data duplication for you. Something along the following lines:

distribute.na.type <- function(dat) {
  rbind(
    transform(subset(dat, type %in% c(1, NA)), type=1),
    transform(subset(dat, type %in% c(2, NA)), type=2)
  )
}

The above example is untested and not very generic, but with a little bit of luck it'll just work. Use it like this: distribute.na.type(melt(...)).

这篇关于分面时重复值到多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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