使用facet时,ggplot2在面板边框外部 [英] ggplot2 outside panel border when using facet

查看:225
本文介绍了使用facet时,ggplot2在面板边框外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多面图的外部周围有一个边框,但没有将图内的面板分开的线.问题是panel.border在构面中的每个面板周围绘制边框,而没有选择仅在整个图周围有边框.或者,您可以将内部分隔线设置为白色",但将外部边界保持为黑色".

I would like to have a border around the outside of my faceted plot but not have the lines that separate the panels inside the plot. The issue is that panel.border draws a border around each panel in the facet with no option to just have a border around the entire plot. Alternatively can you set the inside dividing lines to 'white' but keep the outside border 'black'.

这是我的代码:

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())

结果:带有内部边界的多面图

Result: Faceted plot with inside borders

所需的输出(用油漆编辑):不带内线的多面图

Desired output (edited in paint): Faceted plot without inside lines

我要从中删除内部线条的实际数据图如下:

My actual data plot that I want to remove the inside lines from looks like this:

推荐答案

要考虑的两个选项,都使用辅助轴来模拟右侧的面板边框.如果您也想取消顶部的构面框轮廓,请使用选项2.

Two options for consideration, both making use of a secondary axis to simulate the panel border on the right side. Use option 2 if you want to do away with the facet box outlines on top as well.

选项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

(我将图例保留在默认位置,因为此处并不重要.)

(I'm leaving the legend in the default position as it's not critical here.)

选项2 是对选项1的修改,其中,刻面轮廓已删除&添加一条水平线以模拟顶部边框.明确设置Y轴限制以匹配此边框的高度:

Option 2 is a modification of option 1, with facet outline removed & a horizontal line added to simulate the top border. Y-axis limits are set explicitly to match the height of this border:

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

使用的样本数据:

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不适用于此类用例...)

(For the record, I don't think facet_grid / facet_wrap were intended for such use cases...)

这篇关于使用facet时,ggplot2在面板边框外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆