0

I'm drawing a geographical map next to a table with some data about it. I'm using shapefiles and a dataset.

The shapefiles are contained in a directory and I don't think there is a way to upload it in here. In my code I import from this directory the file ".sph" calling it "prov2022".

dput(prov2022)
.....
.....
.....
    2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
    input = "WGS 84 / UTM zone 32N", wkt = "PROJCRS[\"WGS 84 / UTM zone 32N\",\n    BASEGEOGCRS[\"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        ID[\"EPSG\",4326]],\n    CONVERSION[\"UTM zone 32N\",\n        METHOD[\"Transverse Mercator\",\n            ID[\"EPSG\",9807]],\n        PARAMETER[\"Latitude of natural origin\",0,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",9,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"Scale factor at natural origin\",0.9996,\n            SCALEUNIT[\"unity\",1],\n            ID[\"EPSG\",8805]],\n        PARAMETER[\"False easting\",500000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    ID[\"EPSG\",32632]]"), class = "crs"), class = c("sfc_MULTIPOLYGON", 
"sfc"), precision = 0, bbox = structure(c(xmin = 313279.2522, 
ymin = 3933682.9322, xmax = 1312016.151, ymax = 5220292.2928), class = "bbox"))), row.names = c(NA, 
-107L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(COD_RIP = NA_integer_, 
COD_REG = NA_integer_, COD_PROV = NA_integer_, COD_CM = NA_integer_, 
COD_UTS = NA_integer_, DEN_PROV = NA_integer_, DEN_CM = NA_integer_, 
DEN_UTS = NA_integer_, SIGLA = NA_integer_, TIPO_UTS = NA_integer_, 
SHAPE_AREA = NA_integer_, Shape_Leng = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")))

This is instead an example of the dataset

`City Name`  `Average amount` `Nominal Difference`   `%`   `Real Wage`    `Real Difference`

Barletta           2457            1007            41.0        27.5              11.3
Caserta            2445            910             37.2        27.4              10.2
Avellino           2363            1016            43.0        26.5              11.4
Lecce              2342            981             41.9        26.2              11.0
Benevento          2335            1157            49.6        26.1              13.0
Isernia            2334            1078            46.2        26.1              12.1
L'Aquila           2324            1010            43.5        26.0              11.3
Catanzaro          2310            1533            66.3        25.9              17.2
Campobasso         2259            1106            49.0        25.3              12.4
Enna               2242            922             41.1        25.1              10.3

This is my script

# GEOGRAPHIC MAP
MAP <- right_join(prov2022, dataset, by = "COD_PROV") %>% 
  ggplot(aes(fill = `Real Difference` >= 10 ))+
  geom_sf() +
  theme_void() +
  scale_fill_manual(values = c('white', 'red')) 

#TABLE
TABLE <- subset(cc , `Real Difference` >= 10, c("City Name", "Average amount", "Nominal Difference", "%", "Real Wage", "Real Difference" ))
TABLE <- arrange(TABLE, -`Real Wage`)
TABLE <- tableGrob(TABLE, theme = ttheme_default(base_size = 15))

#PLOTTING TOGETHER
grid.arrange(arrangeGrob(MAP, TABLE, ncol=2))

The script work fine, but there is too much empty space between the MAP on the left and the TABLE of the right, and (as consequence) the table is cut because it goes outside the canvas.

Is there something that I can set in grid.arrange to remove ( or reduce) this space between the 2 plots? or can I directly shift the table a little more on the left to avoid it goes out from tha canvas?

PLOT

I already know that I can reduce the size of the table setting theme = ttheme_default(base_size = 15) with a lower values, but in this way all the characters become small and difficult to be read

io_boh
  • 193
  • 7
  • 1
    no-one can run your code to see the problem or to fix it unless you make your example reproducible. Please include the contents of `prov2022` in your question using `dput`. – Allan Cameron Feb 27 '23 at 14:59
  • 1
    Maybe it's better to use [patchwork package](https://www.rdocumentation.org/packages/patchwork/versions/1.1.2) – Tribaldi Feb 27 '23 at 15:00
  • hi @AllanCameron and thank for the answer! I've update my question with all the information you asked. I hope now it can be clearer – io_boh Feb 27 '23 at 15:44
  • @Tribaldi i tried, but I haven't solved the problem – io_boh Feb 27 '23 at 17:30

1 Answers1

0

The better is to use patchwork package and next the function plot_spacer() and widths (as suggested in the answer to a similar question here) to adjust the spacer between the two plots, such as

library(patchwork)

p1 <- MAP + plot_spacer() + TABLE + plot_layout(widths = c(4, -1 ,6)

In this example the width of MAP is 4, the width of the spacer plot is -1 which allows you to decrease space between plots and the width of TABLE is 6. But this is an example, try to modulate widths in function to your situation because I did not succeed in reproducing your figures.

I hope this solution will work for you. Tell me if it doesn't work.

Tribaldi
  • 177
  • 9
  • hi @Tribaldi and thank! I've tried. It works but not in the way i expected. For example, if I set the spacer plot at -1. the space is too much, while -2 is too less. I've tried also -1.5, but it seems it doesn't recognize the decimal number. Should I set something inside `plot_spacer()` instead?? I also don't understand how it sets the width of the map and of the table. Even if I give larger numbers in `plot_layout`, sometimes the output is smaller... – io_boh Mar 01 '23 at 17:10
  • Hm very strange that `plot_spacer()` doesn't take the decimal. The pb is that I couldn't reproduce your graphs because I can't load your dataset from your `dput()`. Can you share your datas again originally from your graphs? but put when you do `dput()` put everything after `structure(list(....))`, please. – Tribaldi Mar 01 '23 at 17:38