1

I cannot find how to adjust distance from labels to table in grid.arrange:

distances_lsit = c(25,50,75,100) 
category_lsit = 0:5 
damage_table = matrix(NA, nrow=length(distances_lsit), ncol=length(category_lsit))
damage_table[1,] = c(10,25,50,75,100,100)
damage_table[2,] = c(5,10,25,50,75,100)
damage_table[3,] = c(2.5,5,10,25,50,75)
damage_table[4,] = c(0,2.5,5,10,25,50)
rownames(damage_table) = distances_lsit
colnames(damage_table) = category_lsit
table_scale = tableGrob(damage_table)

grid.arrange(table_scale, top = "Label 1", left = "Label 2")

which produces table as follows: enter image description here

Is there anyway to glue it to the table? Thank you in advance for your help.

zx8754
  • 52,746
  • 12
  • 114
  • 209
KJarocki
  • 53
  • 6
  • Maybe try other packages: https://gt.rstudio.com/ . Link is for gt package, but the homepage lists other useful packages with similar functionality. – zx8754 Oct 22 '21 at 12:42
  • Thank you for providing another option. The only problem is that represented example is simplified a lot, so it will be a lot of work to adjust everything to the another solution and I was wondering if it is doable without adjusting big parts of the code for that. – KJarocki Oct 22 '21 at 12:46
  • OK, you can use layout_matrix option with grid.arrange, see https://stackoverflow.com/a/31465255/680068 – zx8754 Oct 22 '21 at 12:47
  • I tried with textGrob created instead of labels but it is still moving with respect to each other when I change the size of the window. – KJarocki Oct 22 '21 at 13:42

1 Answers1

1

You should play with next parameters for finding your best location

  • heights;
  • widths;
  • textGrob.

For example, this:

grid.arrange(table_scale, top = textGrob("Really looks \n better now?", x = 0, hjust = -1), left = "I'm near,\n my man", heights = c(2,1), widths = c(1,1.5))

show to you this:

enter image description here

manro
  • 3,529
  • 2
  • 9
  • 22
  • I think it is still on the viewer side how to display it with these parameters. This is for your code with RStudio and Shiny: https://i.imgur.com/O68caaO.png https://i.imgur.com/A3nLQft.png – KJarocki Oct 22 '21 at 15:47
  • I think I will have to give up and change it, because the app is not going to be used only at one window size. Thank you for your help and advice! – KJarocki Oct 22 '21 at 17:11
  • @Kjarocki Yes, but can you accept it like an answer? Because it is a really answer, for them, who wants manually adjust labels and table. Thanks ;) – manro Oct 23 '21 at 05:36