0

I have the next code:

area01 <- 122.8
parea01 <- 36.1

text02 <- eval(substitute(expression("L (2510-3410 masl):\n"~a*" "*km^2*"("*s*"%)"),list(a=area01,s=parea01)))
plot(1,1)
title(text02)

enter image description here

I only want that the text does not appear to the right. I want everything in the center. I try too with bquote. It works, but the separation between lines it is too much and I dont want that. I let the solution witn bquote.

text02=bquote(bold(atop("L (2510-3410 masl):",.(area01)~km^2~"("~.(parea01)~"%)"),))

The problem here is the separation between lines. With expression, works good for me, but I want to center it. Any guess?

Someone help me to resolve my doubt or problem with the code

Mark
  • 7,785
  • 2
  • 14
  • 34
Felipe
  • 25
  • 2
  • I can't reproduce this, because your code doesn't work without the data. `plot(1,1); title( "L (2510-3410 masl):\n122.8 km2(36.1%)")` works fine – Mark Aug 13 '23 at 06:54
  • Please either update your example so that it has your data, or include fake sample data – Mark Aug 13 '23 at 06:56
  • @Mark which data do you need? if you cant run the code just have `area01 <- 122.8` and `parea01 <- 36.1` then run the code. Also the code you provided above does not produce the desired result ie OP wants to have `km^2` and not `km2` – Onyambu Aug 13 '23 at 08:26
  • thanks @Onyambu. The thing I made was more of an opening gambit – Mark Aug 13 '23 at 08:27
  • btw you never replied to me from ages ago, what happened – Mark Aug 13 '23 at 08:28
  • @Mark sorry, sometimes I get like 15 inbox messages and I just click on mark all as read without reading any. Could you remind me what that was about? – Onyambu Aug 13 '23 at 08:34
  • @Onyambu it's okay! I understand. I can't find the exact thing rn, but I think you posted a thoughtful comment to me, when I was complaining about how people complain about things no matter what I do. I went to sleep, intending to reply, and the comment was deleted – Mark Aug 13 '23 at 10:06
  • I wanted to thank you, but then it was deleted. I tried commenting but you never replied to my questions about it – Mark Aug 13 '23 at 10:09
  • Like @Onyambu, it doesnt work your code Mark – Felipe Aug 13 '23 at 13:48
  • @Felipe my code works, you are just moving the goalposts – Mark Aug 13 '23 at 13:51

2 Answers2

1

A workaround:

pacman::p_load(latex2exp, glue)

plot(1,1)
title(main = TeX(("L (2510-3410 masl):")), line = -1, outer = T)
title(main = TeX(glue("${area01} km^2$ ({parea01}%)")), line = -2, outer = T)

plot

Mark
  • 7,785
  • 2
  • 14
  • 34
  • Another workaround: https://stackoverflow.com/a/18237295/4145280 – Mark Aug 13 '23 at 10:30
  • Or this: https://stackoverflow.com/a/20549895/4145280 – Mark Aug 13 '23 at 10:31
  • btw you don't need to use glue or latex, I just thought it was easier to read – Mark Aug 13 '23 at 10:32
  • i dont know if this will work, because i need to replace it en ggplot text lines. I just one text to represent the example. I dont need to do it en "plot". I only need just one script and i need to work in ggplot. – Felipe Aug 13 '23 at 13:46
  • okay, so you are moving the goalposts – Mark Aug 13 '23 at 13:51
  • why don't you create a real example of what you ***actually*** want, and that way I won't answer a dozen questions in between – Mark Aug 13 '23 at 13:52
  • 1
    Using Tex is the way to go. Great solution btw – Onyambu Aug 13 '23 at 14:26
0

Yet another workaround from base R

text02 <- sprintf("L (2510-3410 masl):\n %5.1f km²(%4.1f%s)", 
    area01, parea01, "%")
plot(1,1)
title(text02)
G5W
  • 36,531
  • 10
  • 47
  • 80