Help is on the way [Off Topic]

posted by Helmut Homepage – Vienna, Austria, 2022-04-25 00:24 (523 d 04:33 ago) – Posting: # 22937
Views: 6,069

Hi ElMaestro,

❝ hot greetings from Ahmedabad.


Hope you enjoy it. ;-)

The problem with your code is that rect() does not – and can not – ‘know’ how wide text() will be.

A workaround:

foo2 <- function(name, h, w, values, loc, cex, lgd) {
  pdf(name, height = 4, width = 3)
  barplot(values, las = 1)
  legend(loc, legend = lgd,
         cex = cex,     # trial & error
         x.intersp = 0, # otherwise, we have a left margin
         trace = TRUE)  # shows user coordinates in the console
  dev.off()
}
name <- "foopdf2.pdf"
foo2(name = name, h = 4, w = 3,
     values = c(8, 2, 3, 3, 1, 2, 3, 1, 9),
     loc = "center", cex = 0.85,
     lgd = c("Text is ok.", "Text is also ok."))
system2("open", args = name, wait = TRUE)


With the additional argument plot = FALSE you could assign all what is needed to a variable:
Then generate a rectangle which is large enough to contain the text. However, using legend() directly (eventually with x, y coordinates) is much simpler. See the examples at the end of the man page of legend.

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

UA Flag
Activity
 Admin contact
22,763 posts in 4,775 threads, 1,627 registered users;
21 visitors (0 registered, 21 guests [including 8 identified bots]).
Forum time: 04:58 CEST (Europe/Vienna)

We should not speak so that it is possible
for the audience to understand us,
but so that it is impossible
for them to misunderstand us.    Quintilian

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5