Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plot border gets behind sf element in ggplot2

Here’s a basic plot:

pb %>% 
  ggplot(aes(fill = couv_rappel)) +
  geom_sf() +
  theme_void() +
  theme(plot.background = element_rect(colour = "red", size = 2),
        legend.position = "none") 

Plot1

Now, if I restrain the plot with limits, I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pb %>% 
  ggplot(aes(fill = couv_rappel)) +
  geom_sf() +
  theme_void() +
  theme(plot.background = element_rect(colour = "red", size = 2),
        legend.position = "none") +
  xlim(2.3,2.4)

Plot2

As you can see, the plot left and right borders get behind the plotted object. Does anyone have a solution to get the border above the plot elements?

Thanks!

Data used:

pb <- structure(list(dep = "75", couv_rappel = 57.3, geometry = structure(list(
    structure(list(list(structure(c(2.46724637546176, 2.46575592250353, 
    2.46120600898013, 2.43669006552864, 2.42985496603744, 2.4199582646563, 
    2.4032827566051, 2.39006934685366, 2.36420634490071, 2.35635437759204, 
    2.3528666667096, 2.34390883177532, 2.33190823942743, 2.31414902154357, 
    2.30132131510763, 2.28939887025059, 2.28085866249027, 2.27192797978477, 
    2.2676168439641, 2.26278403146322, 2.2535603968777, 2.24805520435306, 
    2.22422447830154, 2.22566318222318, 2.23173608410806, 2.24569866927731, 
    2.25528620843296, 2.25997554492291, 2.27994638125302, 2.28445806712108, 
    2.30377644516541, 2.31988421733846, 2.32998322451236, 2.35187284606698, 
    2.3658401097796, 2.37028625603703, 2.38944370936257, 2.39650003656717, 
    2.39863816549655, 2.4003389935185, 2.41069370568239, 2.413263340928, 
    2.41531944369422, 2.41634007356596, 2.41597350659806, 2.41365345988931, 
    2.41652928944455, 2.42307640181955, 2.42757060634054, 2.44785155779452, 
    2.46724637546176, 48.839079305745, 48.8262836552197, 48.8183579113295, 
    48.8185510543002, 48.8233933012562, 48.8240824636363, 48.8292444489485, 
    48.825697080461, 48.8163977497727, 48.8159598008205, 48.818216578285, 
    48.8157574531834, 48.8170124945509, 48.8222907392244, 48.8251298670846, 
    48.8283515995125, 48.8313312729353, 48.8288853042635, 48.8342010419919, 
    48.8339284802732, 48.8368570967459, 48.8463198681933, 48.8535158838331, 
    48.8594069515364, 48.8690697019275, 48.8764605941563, 48.8743532798546, 
    48.8801925532761, 48.8785786260854, 48.8856382772024, 48.8941520304135, 
    48.9004587194173, 48.9011635770595, 48.9015268868537, 48.901610825998, 
    48.9016516431006, 48.901156965113, 48.8961929113555, 48.8894140761193, 
    48.8837472239427, 48.8784752787775, 48.8731186017642, 48.8551777489297, 
    48.8492377880662, 48.8466288464984, 48.8372275617342, 48.8346874842976, 
    48.8427061233008, 48.8415756560415, 48.8448097861725, 48.839079305745
    ), .Dim = c(51L, 2L)))), class = c("XY", "MULTIPOLYGON", 
    "sfg"))), class = c("sfc_MULTIPOLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 2.22422447830154, 
ymin = 48.8157574531834, xmax = 2.46724637546176, ymax = 48.9016516431006
), class = "bbox"), crs = structure(list(input = "WGS84", wkt = "GEOGCRS[\"WGS 84\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = 1L, class = c("sf", 
"data.table", "data.frame"), sf_column = "geometry", agr = structure(c(dep = NA_integer_, 
couv_rappel = NA_integer_), .Label = c("constant", "aggregate", 
"identity"), class = "factor"))

>Solution :

The issue is that you have used plot.background, which is drawn before the geom, rather than panel.border, which is drawn after. Try:

pb %>% 
  ggplot(aes(fill = couv_rappel)) +
  geom_sf() +
  theme_void() +
  theme(panel.border = element_rect(colour = "red", fill=NA, size = 2),
        legend.position = "none") +
  xlim(2.3,2.4)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading