13

facet_gridいくつかのプロットを検討してください

mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() 
mt + facet_grid(vs ~ am, scales = "free") 

plottttt

上のプロットの一番上の行だけをズームして、3〜4のy軸の値のみを表示したいとします。coord_cartesian()ファセットがない場合、またはすべてのプロットをズームしたい場合は、これを行うことができますが、この場合、良い解決策はありません。最初にデータをサブセット化できると思いますが、それは正当な理由でタブーです(たとえば、統計レイヤーを破棄するなど)。

(質問はこれに関連していることに注意してください:R:{ggplot2}:どのように/ facet_gridプロットのx軸制限を個別に調整できますか?しかし、そこでの答えはこの目的では機能しません。)

4

3 に答える 3

12

ですから、これは古い質問ですが、私が見つけた方法を共有したいと思いました。最も簡単なのは、データに人工的なポイントを追加することです。たとえば、各プロットのデータの左下と右上に追加します。この情報を別のデータフレームに入れ、パラメータalpha = 0を使用してgeom_pointを使用してプロットに追加し、ポイントを非表示にします。必要に応じて、facet_wrapの軸のスケーリングを「free_x」または「free_y」に設定します。

これで、ggplotは、追加した非表示のポイントに対応するために、すべてのファセットを個別にスケーリングします。少しハッキーですが、うまく機能します。

于 2015-05-25T11:55:29.143 に答える
9

古い投稿ですが、同じものを探していましたが、実際には何も見つかりませんでした-[おそらくこれは、facet_grid()を使用して2つの従属変数のy軸に制限を設定する方法です

解決策はあまりエレガント/効率的ではありませんが、うまくいくと思います-基本的に、異なるcoord_cartesian呼び出しで2つのプロットを作成し、grobsを交換します。

# library(ggplot2)
# library(gtable)

# Plot
mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() 

# --------------------------------------------------------------------------------


p1 <- mt + facet_grid(vs ~ am, scales = "free") + coord_cartesian(ylim = c(1,6))
g1 <- ggplotGrob(p1)

p2 <- mt + facet_grid(vs ~ am, scales = "free") + coord_cartesian(ylim = c(3,5))
g2 <- ggplotGrob(p2)

# ----------------------------------------------------------
# Replace the upper panels and upper axis of p1 with that of p2
# Tweak panels of second plot - the upper panels
g1[["grobs"]][[6]] <- g2[["grobs"]][[6]]
g1[["grobs"]][[8]] <- g2[["grobs"]][[8]]

#Tweak axis
g1[["grobs"]][[4]] <- g2[["grobs"]][[4]]

grid.newpage()
grid.draw(g1)
于 2014-03-31T22:56:01.307 に答える
0

私が提案する解決策は、 @user20650によって提供された回答に基づいています。これは、半自動手順を使用して、からのg1要素で置き換える必要があるgrobの要素のインデックスを見つけることによって異なりg2ます。

# library(ggplot2)
# library(grid)

p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + 
  geom_point() +
  facet_grid(vs ~ am)

# modify p1 by zooming as desired
p2 <- p1 + coord_cartesian(ylim = c(3,4)) + 
  theme_bw() + 
  theme(axis.text  = element_text(color="blue"),
        strip.text = element_text(color="blue"))

p1
p2

次に、上部ファセット行のy軸の制限を変更します。g12つのグロブを生成g2し、パネルとy軸をg1からの対応する要素に置き換えますg2

以下のコードは、要素の名前に基づいて、置き換えられるgrob要素のインデックスを検索します。

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

# Replace the upper panels and upper axis of p1 with that of p2
# i.e. replace the corresponding grobs in g1 with the versions of g2
# Panel numbering goes row-wise from top left to bottom right
panels_to_replace_with_g2 <- c(1,2)

# To get names of grobs run: lapply(g1[["grobs"]],function(x) x$name)
# Use names of grobs to find out indices of g1[["grobs"]] of the panels we want to replace
# as well as for the axis ticks.
pattern_for_specific_panels <- 
  paste0("^panel-((",paste0(panels_to_replace_with_g2, collapse = ")|("),"))")
pattern_for_axes_ticks <- 
  "^GRID.absoluteGrob"

idx_panels_to_replace_from_g2 <- which(unlist(
  lapply(g1[["grobs"]], function(x) grepl(pattern_for_specific_panels, x$name))))
# > idx_panels_to_replace_from_g2
# [1] 2 4

idx_axesTicks <- which(unlist(
  lapply(g1[["grobs"]], function(x) grepl(pattern_for_axes_ticks, x$name))))

# Find out manually which of the defined axesTicks it is:
g_test <- g1
for (itr in idx_axesTicks) {
  g_test[["grobs"]][[itr]] <- g2[["grobs"]][[itr]]
  grid.newpage();grid.draw(g_test); grid.draw(textGrob(itr, just = "top"))
  Sys.sleep(1)
}
# We found out it is itr=10
idx_axesTicks_to_replace <- 10

idx_panels_to_replace_from_g2これで、交換するパネルのインデックスとy軸要素のインデックスが見つかりましたidx_axesTicks_to_replace。以下で交換できます。

# Replace panels
grid.newpage();grid.draw(g1)
for (iter in idx_panels_to_replace_from_g2) {
  g1[["grobs"]][[iter]] <- g2[["grobs"]][[iter]]
  grid.newpage();grid.draw(g1)
  Sys.sleep(1)
}

# Replace y-axis
g1[["grobs"]][[idx_axesTicks_to_replace]] <- g2[["grobs"]][[idx_axesTicks_to_replace]]

# Render plot
grid.newpage()
grid.draw(g1)

プロットが正常に変更された場合、変更をより見やすくするために、p2に適用したテーマの変更とテキストの色を削除できるようになりました。

修正されたプロット

将来TODO:現在不足しているのは、変更された軸ラベルを尊重するためにy軸の幅を増やすことです

于 2018-08-27T06:36:48.123 に答える