1

I have made a graph using ggplot and facet wrap to show my data across 3 different years. I am trying to remove the top and right border so each of the graphs have border only on the y and x axis. Here is my code for the graph

ggplot(dets_per_month, aes(x = month, y = ave)) +
  geom_bar(aes(fill = zone), position = "dodge", stat = "identity") +
  scale_fill_viridis_d(option = "plasma", name = "Zone") +
  ylim(0,3700) +
  xlab("Month") +
  ylab("Avergae Detections") +
 facet_wrap(vars(yearcollected),
            strip.position = "top") +
  facet_grid(yearcollected ~ .) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 

        axis.line = element_line(),

        panel.border = element_line(),
        strip.text = element_text(size = 10, face = "bold"),
        strip.background = element_rect(fill = "white", colour = "white"))

I've been playing around with panel.border but I can't seem to get the output I'm looking for. Here is what my graph looks like with this code: enter image description here

Does anyone know how to control the number and placement of borders in facet wrap?

Kristen Cyr
  • 629
  • 5
  • 16
  • 1
    It's unclear what you're trying to get—you just say this isn't it. We also can't run your code without a sample of data. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that's easier to help with – camille Dec 25 '19 at 15:43
  • 2
    Please consider: https://stackoverflow.com/help/someone-answers – Dave2e Dec 26 '19 at 13:23

1 Answers1

2

There are facet_rep_wrap()/facet_rep_grid from the lemon package to do this

if (!require("pacman")) install.packages("pacman")
#> Loading required package: pacman
pacman::p_load(ggplot2, lemon)

p1 <- ggplot(mtcars, aes(x = cyl, y = mpg)) +
  geom_bar(aes(fill = gear), position = "dodge", stat = "identity") +
  facet_rep_wrap(vars(carb),
             strip.position = "top",
             scales = 'free_y') +
  theme_classic(base_size = 14) + 
  theme(strip.text = element_text(face = "bold"),
        strip.background = element_blank())
p1

Created on 2019-12-25 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115