web-dev-qa-db-ja.com

ファセットを使用する場合のパネル境界の外側のggplot2

ファセットプロットの外側に境界線を設定したいのですが、プロット内のパネルを区切る線はありません。問題は、panel.borderがファセットの各パネルの周囲に境界線を描画し、プロット全体の周囲に境界線を設定するオプションがないことです。または、内側の分割線を「白」に設定し、外側の境界線を「黒」のままにすることもできます。

これが私のコードです:

mtcars
mtcars$manufacturer=rownames(mtcars)
ggplot(mtcars, aes(x=manufacturer, y=mpg,fill=factor(gear,levels=c("3","4","5"))))+
  geom_bar(stat="identity",position="dodge",colour="black")+
  facet_grid(~cyl,scales = "free_x",space = "free_x",) +
  theme(axis.text.x = element_text(angle = 45,size=12,colour="Black",vjust=1,hjust=1),
    strip.background = element_blank(),
    strip.placement = "inside",
    strip.text = element_text(size=15),
    legend.position=c(0.9,0.8),
    legend.title=element_blank(),
    legend.text=element_text(size=15),
    panel.spacing = unit(0.2, "lines"), 
    panel.background=element_rect(fill="white"),
    panel.border=element_rect(colour="black",size=1),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())

結果:内側の境界線を持つファセットプロット

enter image description here

必要な出力(ペイントで編集):内側の線のないファセットプロット

enter image description here

内側の線を削除したい実際のデータプロットは次のようになります。

Actual data plot with inside borders needing to be removed

9
Guidofish

考慮すべき2つのオプション。どちらも、右側のパネル境界をシミュレートするために2次軸を使用します。上部のファセットボックスのアウトラインも削除する場合は、オプション2を使用します。

オプション1

ggplot(df,
       aes(x = Month, y = Abundance, fill = Type)) +
  geom_col(position = "dodge", colour = "black") +
  scale_y_continuous(labels = function(x){paste(x, "-")},    # simulate tick marks for left axis
                     sec.axis = dup_axis(breaks = 0)) +      # add right axis
  scale_fill_grey() +
  facet_grid(~Season, scales = "free_x", space = "free_x") +
  theme_classic() +
  theme(axis.title.y.right = element_blank(),                # hide right axis title
        axis.text.y.right = element_blank(),                 # hide right axis labels
        axis.ticks.y = element_blank(),                      # hide left/right axis ticks
        axis.text.y = element_text(margin = margin(r = 0)),  # move left axis labels closer to axis
        panel.spacing = unit(0, "mm"),                       # remove spacing between facets
        strip.background = element_rect(size = 0.5))         # match default line size of theme_classic

option 1

(ここでは重要ではないため、凡例はデフォルトの位置のままにしておきます。)

オプション2は、オプション1を変更したもので、ファセットアウトラインが削除され、上部の境界線をシミュレートするために水平線が追加されています。 Y軸の制限は、この境界線の高さに一致するように明示的に設定されます。

y.upper.limit <- diff(range(df$Abundance)) * 0.05 + max(df$Abundance)
y.lower.limit <- 0 - diff(range(df$Abundance)) * 0.05

ggplot(df,
       aes(x = Month, y = Abundance, fill = Type)) +
  geom_col(position = "dodge", colour = "black") +
  geom_hline(yintercept = y.upper.limit) +
  scale_y_continuous(labels = function(x){paste(x, "-")},    # 
                     sec.axis = dup_axis(breaks = 0),        # 
                     expand = c(0, 0)) +                     # no expansion from explicitly set range
  scale_fill_grey() +
  facet_grid(~Season, scales = "free_x", space = "free_x") +
  coord_cartesian(ylim = c(y.lower.limit, y.upper.limit)) +  # set explicit range
  theme_classic() +
  theme(axis.title.y.right = element_blank(),                # 
        axis.text.y.right = element_blank(),                 # 
        axis.ticks.y = element_blank(),                      # 
        axis.text.y = element_text(margin = margin(r = 0)),  # 
        panel.spacing = unit(0, "mm"),                       # 
        strip.background = element_blank())                  # hide facet outline

option 2

使用されたサンプルデータ

set.seed(10)
df <- data.frame(
  Month = rep(c("Jun 14", "Aug 14", "Oct 14", "Dec 14", "Apr 15", "Jun 15"),
             each = 3),
  Type = rep(c("Mangrove", "Mudflat", "Fringe"), 6),
  Season = rep(c("Dry1", rep("Wet1", 3), rep("Dry2", 2)), each = 3),
  Abundance = sample(50:600, 18)
)

df <- df %>%
  mutate(Month = factor(Month, levels = c("Jun 14", "Aug 14", "Oct 14", 
                                          "Dec 14", "Apr 15", "Jun 15")),
         Season = factor(Season, levels = c("Dry1", "Wet1", "Dry2")))

(ちなみに、facet_grid/facet_wrapはそのようなユースケースを対象としていたとは思いません...)

6
Z.Lin