简体   繁体   中英

Can I facet data with NAs?

I would like to plot 4 years (2010-2013) out of 6 years (2008-2013) keeping the space assigned to 2008 and 2009 (for practical purposes). However, since I have NAs in those years, facet_grid complains:

"Error in layout_base(data, cols, drop = drop) : At least one layer must contain all variables used for facetting"

This is my dataset: https://www.dropbox.com/s/ehyccl8kubazs8x/test.csv?dl=0

This is my code:

cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

ggplot(test) + 
  geom_jitter(aes(mean,NEE_f, colour=factor(month.x)),alpha=1,size=2) +  scale_colour_manual(values=cbbPalette)+
  labs(x = "WT to surface (cm)", y = "Reco (μmol/m2/s)")+
  facet_grid(~year)+
  theme_bw(base_size = 12, base_family = "Helvetica")+
  theme(legend.position="none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA, size=1))

Quick and dirty solutions?

Cheers

You can use facet.grid with NA values:

> df = data.frame(Date = as.Date(seq(300, 2000, 300), origin = "2000-01-01"), 
    x = rnorm(6), y = rnorm(6))
> df$Date[3] <- NA
> ggplot(df) + geom_jitter(aes(x, y)) + facet_grid(~Date)

在此处输入图片说明

You're problem is that you have no column named year , you only have year.x and year.y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM