11

ストリップは常にggplot2によって作成されたプロットの上にあるようです。プロットの下に移動できますか?

例えば:

library(ggplot2) 
qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)

上に車の情報を表示します。一番下に表示できますか?

4

1 に答える 1

7

更新:ggplot2バージョン 2.1.0 を使用して、使用を検討してswitch = 'x'ください。詳細?facet_gridについては、を参照してください。

関数を使用しgtableて、ストリップを簡単に移動できます。(または、別のバージョンについてはこちらを参照してください-x軸とストリップの交換)

library(ggplot2)
library(gtable)
library(grid)

p <- ggplot(mpg, aes(hwy, cty)) + geom_point() + facet_grid( . ~ manufacturer) +
     theme(strip.text.x = element_text(angle = 90, vjust = 1),
           strip.background = element_rect(fill = NA))

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the positions of the panels in the layout: t = top, l = left, ...
panels <-c(subset(gt$layout, grepl("panel", gt$layout$name), select = t:r))

# Add a row below the x-axis tick mark labels,
# the same height as the strip
gt = gtable_add_rows(gt, gt$height[min(panels$t)-1], max(panels$b) + 2)

# Get the strip grob
stripGrob = gtable_filter(gt, "strip-t")

# Insert the strip grob into the new row
gt = gtable_add_grob(gt, stripGrob, t = max(panels$b) + 3, l = min(panels$l), r = max(panels$r))

# remove the old strip
gt = gt[-(min(panels$t)-1), ]

grid.newpage()
grid.draw(gt)

ここに画像の説明を入力

于 2015-03-29T23:53:57.070 に答える