Repeat values to multiple plots when faceting

2020-04-23 03:33发布

问题:

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)

回答1:

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(...)).



标签: r ggplot2 facet