rpolicastro
rpolicastro

Reputation: 1305

removing unused factors from facet_grid with row and column specified

What is the Problem?

Each facet in ggplot2 may not contain all factor levels in the 'name' column, in which case I want the facet to ignore those factor levels to avoid the gaps in the tiles.

What have I tried

I have tried adding and removing scales = "free", drop = TRUE, and space = "free" from facet_grid as recommended in a stackexchange question here, and a stackoverflow question here.

Any help would be much appreciated!

Example slice of data

test_data <- structure(list(sample = c("1", "1", "1", "1", "1", "2", "2", 
"2", "2", "2", "3", "3", "3", "3", "3", "1", "1", "1", "1", "1", 
"2", "2", "2", "2", "2", "3", "3", "3", "3", "3"), name = c("IV_1385127_1385127_1_+_3_1", 
"IV_78222_78222_1_-_3_1", "XV_978130_978130_1_-_3_1", "XIV_574351_574351_1_+_3_1", 
"XV_357215_357215_1_-_3_1", "XII_456601_456601_1_-_3_1", "V_423552_423552_1_+_3_1", 
"XI_200191_200191_1_-_3_1", "XII_465717_465717_1_-_4_2", "XII_455342_455342_1_-_3_1", 
"VII_84298_84298_1_-_3_1", "IV_229884_229884_1_+_4_2", "XII_633371_633371_1_-_4_2", 
"XIII_9888_9888_1_-_4_2", "X_703096_703096_1_-_3_2", "IV_1385127_1385127_1_+_3_1", 
"IV_78222_78222_1_-_3_1", "XV_978130_978130_1_-_3_1", "XIV_574351_574351_1_+_3_1", 
"XV_357215_357215_1_-_3_1", "XII_456601_456601_1_-_3_1", "V_423552_423552_1_+_3_1", 
"XI_200191_200191_1_-_3_1", "XII_465717_465717_1_-_4_2", "XII_455342_455342_1_-_3_1", 
"VII_84298_84298_1_-_3_1", "IV_229884_229884_1_+_4_2", "XII_633371_633371_1_-_4_2", 
"XIII_9888_9888_1_-_4_2", "X_703096_703096_1_-_3_2"), ntile = c("1", 
"1", "1", "1", "1", "1", "1", "1", "2", "1", "1", "2", "2", "2", 
"2", "1", "1", "1", "1", "1", "1", "1", "1", "2", "1", "1", "2", 
"2", "2", "2"), position = c("-1", "-1", "-1", "-1", "-1", "-1", 
"-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"
), base = c("T", "T", "T", "A", "C", "A", "T", "T", "A", "T", 
"T", "G", "C", "C", "A", "A", "G", "A", "G", "G", "A", "T", "A", 
"G", "T", "A", "A", "A", "A", "A")), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -30L))

Code used for plotting

  p <- ggplot(test_data, aes(x = position, y = factor(name))) +
      geom_tile(aes(fill = base)) +
      scale_fill_viridis_d() +
      theme_bw() +
      theme(
          axis.title.y=element_blank(),
          axis.text.y=element_blank(),
          legend.title=element_blank(),
          axis.title.x=element_text(margin = margin(t = 15)),
          panel.grid=element_blank()
       )

    p <- p + facet_grid(ntile ~ sample, scales = "free", space = "free", drop = TRUE)

Example plot output

example plot

ggplot2 version

ggplot2 3.1.1

Upvotes: 4

Views: 896

Answers (1)

deepseefan
deepseefan

Reputation: 3791

There are no unused factors in your sample data and the arguments are not giving you what you desire. Possible startup workaround can be:

# Your script
p <- ggplot(test_data, aes(x = position, y = factor(name))) +
  geom_tile(aes(fill = base)) +
  scale_fill_viridis_d() +
  theme_bw() +
  theme(
    axis.title.y=element_blank(),
    axis.text.y=element_blank(), 
    legend.title=element_blank(),
    axis.title.x=element_text(margin = margin(t = 15)),
    panel.grid=element_blank()
  )
p + coord_flip() + facet_wrap(ntile ~ sample, scales = "free")

output

unused_factor_updated

Upvotes: 2

Related Questions