Home > Mobile >  Draw Box around ggplot excluding the legend and axis labels
Draw Box around ggplot excluding the legend and axis labels

Time:10-05

How can I draw a box around a ggplot? I have a facet grid ggplot and would like to draw a box around it. That means only one line for the x-axis and y-axis and then above the facet labels and on the right side. Is it possible to do that when using facet grid? I was able to draw a box around each facetted plot but not around the entire plot.

plot.background = element_rect(...) does not work because it draws boxes around each facet but I only want a big box drawn around all facets.

Here is the data and code that I have so far:

tibble::tribble(
  ~Habitat_Year_Cohort, ~Month,         ~Location,             ~avg,               ~sd,  ~Year,                   ~Habitat,     ~Habitat_Year_Cohort_2,
  "Seagrass (hard-bottom) 2005",  "Dec",       "Birch Bay", 1.22657142857143, 0.461769010488598, "2005",   "Seagrass (hard-bottom)",   "Seagrass (hard-bottom)",
  "Seagrass (soft-bottom) 2005",  "Dec",     "Padilla Bay",            1.405, 0.518022200296474, "2005",   "Seagrass (soft-bottom)",   "Seagrass (soft-bottom)",
  "Mussel (mixed substrate) 2005",  "Dec",       "Penn Cove",         1.803125, 0.523759878870352, "2005", "Mussel (mixed substrate)", "Mussel (mixed substrate)",
  "After Hatch Year Male 2021",  "Dec",      "Adult Male", 1.29973684210526, 0.492007434689227, "2021", "Mussel (mixed substrate)",    "After Hatch Year Male",
  "After Hatch Year Female 2021",  "Dec",    "Adult Female", 1.24852173913043, 0.405312547140556, "2021", "Mussel (mixed substrate)",  "After Hatch Year Female",
  "Hatch Year Male 2021",  "Dec",   "Juvenile Male",             1.09, 0.270336826940023, "2021", "Mussel (mixed substrate)",          "Hatch Year Male",
  "Hatch Year Female 2021",  "Dec", "Juvenile Female",            1.233, 0.298399061660723, "2021", "Mussel (mixed substrate)",        "Hatch Year Female",
  "Seagrass (hard-bottom) 2006",  "Mar",       "Birch Bay", 1.06914285714286, 0.334118456325212, "2006",   "Seagrass (hard-bottom)",   "Seagrass (hard-bottom)",
  "Seagrass (soft-bottom) 2006",  "Mar",     "Padilla Bay",         1.140125, 0.248227682063982, "2006",   "Seagrass (soft-bottom)",   "Seagrass (soft-bottom)",
  "Mussel (mixed substrate) 2006",  "Mar",       "Penn Cove",           0.8865, 0.181639360114643, "2006", "Mussel (mixed substrate)", "Mussel (mixed substrate)"
)

ggplot(data = df, aes(x = Month, y = avg, col = Habitat, group = Habitat_Year_Cohort))  
  geom_errorbar(aes(ymin = avg - sd, ymax = avg   sd),
    width = 0.3,
    position = position_dodge(width = 2)
  )  
  geom_point(position = position_dodge(width = 2), show.legend = F)  
  facet_grid(~Year, scales = "free")  
  geom_text(aes(label = Location), show.legend = F, angle = 90, position = position_dodge(width = 2), vjust = -0.5, col = "black")  
  theme(
    legend.position = "bottom",
    legend.key = element_blank(),
    panel.background = element_blank(),
    panel.border = element_blank(),
    strip.background = element_blank(),
    plot.background = element_blank(),
    panel.grid = element_blank()
  )  
  scale_color_manual(
    values = c(
      "Seagrass (hard-bottom)" = "grey75",
      "Seagrass (soft-bottom)" = "grey50",
      "Mussel (mixed substrate)" = "grey1"
    )
  )  
  annotate("segment", x = Inf, xend = -Inf, y = Inf, yend = Inf, color = "black", lwd = 1)

Thank you.

CodePudding user response:

If I understand you correctly, you are looking for this:

enter image description here

This cannot really be achieved with facets, since the rectangle drawn around the plot does not correspond to any existing theme elements. The above image is not actually drawn with facets. Rather, we have the interaction of year and month on the x axis (which is relabelled so it only prints the month component), with the "facet strips" being drawn on as annotations:

ggplot(data = df, aes(x = paste(Year, Month), y = avg, col = Habitat, 
                      group = Habitat_Year_Cohort))  
  geom_errorbar(aes(ymin = avg - sd, ymax = avg   sd),
                width = 0.3,
                position = position_dodge(width = 0.8)
  )  
  geom_point(position = position_dodge(width = 0.8), show.legend = F)  
  geom_text(aes(label = Location), show.legend = F, angle = 90, 
            position = position_dodge(width = 0.8), vjust = -0.5, 
            col = "black")  
  annotate("segment", x = c(0.6, 1.6, 2.6), xend = c(1.4, 2.4, 3.4),
           y = 2.5, yend = 2.5)  
  annotate("text", x = 1:3, y = 2.55, label = c(2005, 2006, 2022))  
  theme(
    legend.position = "bottom",
    legend.key = element_blank(),
    panel.background = element_blank(),
    panel.border = element_rect(fill = NA),
    plot.background = element_blank(),
    panel.grid = element_blank()
  )  
  scale_x_discrete(labels = ~ sub("\\d  ", "", .x), name = "Month")  
  scale_color_manual(
    values = c(
      "Seagrass (hard-bottom)" = "grey75",
      "Seagrass (soft-bottom)" = "grey50",
      "Mussel (mixed substrate)" = "grey1"
    )
  ) 
  • Related