2016-11-25 8 views
3

ファセットされたggplot2の図形から不要なファセットを選択的に削除したいと思います。私はこの質問を見ていたが、それを行う方法を見つけ出すことができませんでした(多分アドバイスは今時代遅れあり):ここでは ggplot2:未使用の因子レベルの組み合わせのファセットをプロットから削除する(facet_grid)

adding empty graphs to facet_wrap in ggplot2

は、最小限の例です。私は右下の空のファセットを削除したいと思います(b、2)。

enter image description here

library('ggplot2') 
d <- data.frame('factor_1' = factor(c('a', 'a', 'b')), 
       'factor_2' = factor(c('1', '2', '1')), 
       x = 1:3, y = 1:3) 

ggplot(data = d, mapping = aes(x = x, y = y)) + 
    geom_point() + 
    facet_grid(facets = factor_1 ~ factor_2, drop = TRUE) 

は明らか drop = TRUEは、未使用の因子レベルのみ未使用の組み合わせが存在しないので、ここでは効果がありません。

答えて

1

を、プロットのgrobsの名前を持っていますかわった。

library(ggplot2) 
library(grid) 
d <- data.frame('factor_1' = factor(c('a', 'a', 'b')), 
       'factor_2' = factor(c('1', '2', '1')), 
       x = 1:3, y = 1:3) 

p = ggplot(data = d, mapping = aes(x = x, y = y)) + 
    geom_point() + 
    facet_grid(facets = factor_1 ~ factor_2, drop = TRUE) 

# Get ggplot grob 
g = ggplotGrob(p) 

# Get the layout dataframe. 
# Note the names. 
# You want to remove "panel-2-2" 
g$layout 

# gtable::gtable_show_layout(g) # Might also be useful 

# Remove the grobs 
# The grob needs to be remove, 
# and the relevant row in the layout data frame needs to be removed 
pos <- grepl(pattern = "panel-2-2", g$layout$name) 
g$grobs <- g$grobs[!pos] 
g$layout <- g$layout[!pos, ] 


# Alternatively, replace the grobs with the nullGrob 
g = ggplotGrob(p) 
pos <- grep(pattern = "panel-2-2", g$layout$name) 
g$grobs[[pos]] <- nullGrob() 

# If you want, move the axis 
# g$layout[g$layout$name == "axis-b-2", c("t", "b")] = c(8, 8) 

# Draw the plot 
grid.newpage() 
grid.draw(g) 

enter image description here

あなたのリンクでの答えは、このようなものに変更する必要があります:私は、私は間違いなく `facet_grid`なく` facet_wrap`を使用する必要が怖い

n <- 1000 
df <- data.frame(x = runif(n), y=rnorm(n), label = sample(letters[1:7], 
       size = n, replace = TRUE), stringsAsFactors=TRUE) 
df$label.new <- factor(df$label, levels=sort(c(""," ",levels(df$label)))) 


p <- ggplot(df, aes(x=x, y=y)) + geom_point() + 
     facet_wrap(~ label.new, ncol=3,drop=FALSE) 

g = ggplotGrob(p) 

g$layout # Note the names and their positions (t, b, l, r) 
# gtable::gtable_show_layout(g) # Might also be useful 

pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1") 
g$grobs <- g$grobs[!pos] 
g$layout <- g$layout[!pos, ] 

# Or replace the grobs with the nullGrob 
g = ggplotGrob(p) 
pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1") 
g$grobs[pos] <- list(nullGrob()) 

# Move the axis 
g$layout[g$layout$name == "axis-l-1-1", c("l", "r")] = c(10,10) 

grid.newpage() 
grid.draw(g) 
+0

これはすばらしい答えであり、私には大いに役立ちます。しかし、私の非おもちゃの問題にそれを適用すると、(i)パネルの名前( "panel-5-3"など)がプロット内のパネルの座標を参照していないように見えること、しかし、少なくとも、それらは 'panel $ layout'の中でコラムメジャーであり、(ii)軸/ストリップの位置を調整するとき(名前が単純なところでは)、位置情報(t )、b(ottom)、r(ight)、l(eft))は、実際には 'panel $ layout'に含まれています。あなたはそれを説明したり、それに関する有用な情報を教えてもらえますか?とにかく、ありがとうございました。 – NoBackingDown

+0

(i)t(op)、b(ottom)、r(ight)、l(eft)は、基礎となるgtableに対するgrobの範囲を指定します。 'gtable_show_layout()'を使って基礎となるgtableのイメージを取得します。レイアウトデータフレームの '名前'列に与えられたgrobsの名前にある数字は、t、l、b、rではありません。 v2.2.0では、「panel-5-3」は「5 panel down、3 panels across」を意味します。ストリップの場合、最初の数字が横になり、2番目の数字が下になります。 'gtable_show_layout()'やレイアウトデータフレームを使ってgrobsをgrob名とtlbrにマッチさせます。 –

+0

(ii) 'gtable_show_layout()'では、軸を取り付けるパネルの行(tとb)に注意してください。あなたの例では、それは行7です。軸は1行、すなわち行8に配置されます。したがって、tとbは両方とも8です。列番号(rとl)は変更されません。お役に立てれば。 –

1

ない最善の解決策が、それはやや満足のいく結果得られます。

d$fInter <- interaction(d$factor_2, d$factor_1, sep = ' V ') 

    ggplot(data = d, mapping = aes(x = x, y = y)) + 
     geom_point() + 
     facet_wrap(~ fInter, drop = TRUE, 
       ncol = nlevels(d$factor_1)) 

プロット:ggplot2 2.2.0で

Plot

+0

を。 – NoBackingDown

+0

何か特別な理由があるかどうか尋ねてもよろしいですか?新しいインタラクション値の代わりに〜factor_1 + factor_2だけを使うことができますが、目にはそれほど快適ではありません。 –

+0

これはほんのわずかな例ですが、実際には列のネストされた要素があります。 – NoBackingDown

関連する問題