Skip to content

Adding a superscript 6 to a number

12 messages · Sorkin, John, Richard M. Heiberger, Bill Dunlap +6 more

#
Colleagues,

I would like to create a number (stored in the variable x) to the number with the exponent of, i.e. the number to the sixth power. The code I have tried, pasted below does not work.

# create plot
plot(x=0.2,y=0.2,xlim=c(0,1),ylim=c(0,1))
PNotSusp=0.69
# Create a string containing the base and exponent.
x <- 0.95123
# Convert the base to text and add the exponent of 6
mylab <- expression(paste(" ",character(x)^6))
# Add the text to the plot
text(0.4,0.8,labels=paste(PNotSusp,"=\n",mylab))

I hope someone can show me how to create the text string I need.

Thank you,
John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
#
paste() is the problem.  don?t use paste with expression()

On Tue, Dec 29, 2020 at 10:50 Sorkin, John <jsorkin at som.umaryland.edu>
wrote:

  
  
#
Does this do what you want?

plot(0:1, 0:1)
x <- .25
text(.5,.5, labels = bquote(.(x)^6, list(x= x)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Dec 29, 2020 at 7:50 AM Sorkin, John <jsorkin at som.umaryland.edu>
wrote:

  
  
#
And if you have multiple labels, use lapply with bquote:

plot(1:5, sin(101:105)^2, type="n")
text(2:4, sin(102:104)^2, labels=lapply(102:104,
function(i)bquote(sin(.(i))^2)))

-Bill
On Tue, Dec 29, 2020 at 8:31 AM Bert Gunter <bgunter.4567 at gmail.com> wrote:

            

  
  
#
After calling plot, I draw a horizontal line representing the median with aline.
abline (h=median(walk_seconds), lty=1, lwd=2.0, col="firebrick4")
Then, I want to label that line as the median and do so by calling mtext.
mtext  (side=4, "median", col="firebrick4", adj=?0.50?)
The dilemma is I do not see how to know what value to pass for adj in mtext so that the horizontal line for median, and the text for median line up.
Greg
walk_seconds <- c (5207, 4276, 5351, 4218, 5104, 4229, 3916, 3441, 4319, 4277, 5341, 5397, 4963, 6416, 3781, 4284, 2820, 3090, 3620, 3540, 3660, 3600, 3630, 3640, 3479, 3154, 3495, 3040, 3535, 3600, 3165, 3225, 3737, 4183, 4354, 3236, 3234, 3714, 3599, 3570, 2227, 4692, 3596, 3030, 3390, 3669, 4495, 5079, 5225, 5407, 4042, 5582, 5449, 5471, 6681, 5717, 5825, 3997, 4026, 4065, 4223, 5448, 5284, 5180, 3937, 3871, 4265, 4999, 5119, 5411, 4481, 5600, 4414, 4282, 5095, 4904, 4311, 4542, 4481, 5257, 4774, 4037, 4409, 4944, 4616, 4153, 4401, 3708, 5338, 4250, 3353, 4154, 4631, 4333, 3856, 5220, 4056, 4077, 5252, 3768, 4122, 3757, 3890, 5344, 5311, 3809, 3736, 4028, 4177, 6815, 4795, 6217, 4480, 5667, 6010, 5671, 6457, 4671, 5416, 5256, 5713, 6023, 5400, 5327, 5104, 5282, 5534, 6452, 5481, 5126, 6464, 5540, 6362, 5027, 5247, 4826, 5697, 5252, 5478, 4948, 5192, 4187, 5391, 5110, 5032, 5042, 5139, 4314, 5277, 5284, 4892, 4797, 5145, 4921, 5960, 5219, 4377, 4946, 5557, 4063, 5457, 5279, 5422, 4343, 4227, 5725, 4863, 5451, 3651, 5459, 4621, 5894, 4940, 5792, 4952, 5556, 5636, 5511, 3861, 6017, 5520, 4564, 4756, 3966, 4529, 4138, 4782, 4253, 4810, 4708, 5504, 5603, 5897, 6128, 5618, 5289, 5694, 5263, 5482, 5442, 4497, 3180, 7110, 3194, 2154, 4532, 4340, 4031, 3451, 4839, 4730, 4223, 3515, 3542, 4708, 4078, 4938, 4153, 3126, 3492, 4025, 4380, 4517, 4961, 4080, 4226, 4551, 3625, 4375, 5112, 5377, 3811, 3268, 2722, 3125, 3134, 3376, 3316, 2928, 2712, 3062, 3238, 4540, 4595, 3878, 4158, 4922, 6083, 4812, 5278, 5216, 4970, 3892, 4996, 3617, 3849, 3980, 5682, 4373, 3860, 5086, 4936, 5252, 5228, 5220, 5009, 4858, 4953, 5170, 5468, 6674, 3907, 4671, 3836, 3692, 3945, 3731, 5003, 3882, 3637, 3459, 3699, 3690, 4138, 3990, 3448, 3983, 5309, 4146, 4027, 4307, 4707, 4166, 3600, 3600, 3600, 5324, 3600, 4379, 3647, 3875, 5190, 4228, 4790, 3858, 4741, 3644, 3793, 3901, 3779, 3820, 3680, 3455, 3896, 3621, 3449, 4013, 3417, 3752, 3546, 3466, 2947, 3587, 3634, 4948, 3538, 3414, 3571, 3935, 3756, 4586, 3713, 4782, 3957, 3806, 3717, 3574, 3811, 3849, 2880, 4219, 3038, 2831, 3138, 4918, 3485, 3300, 3456, 3609, 3682, 4380, 3600, 3795, 3154, 4338, 3925, 3719, 2700, 3045, 3751, 3771, 3457, 3794)
plot    (walk_seconds, type="l", xaxt="n", xlab=" ", yaxt="n", ylab="Duration", col="blue")
abline  (v=1,                                       lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31),                                  lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29),                               lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31),                            lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30),                         lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31),                      lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30),                   lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31),                lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31+31),             lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31+31+30),          lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31+31+30+31),       lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31+31+30+31+30),    lty=4, lwd=1.0, col="grey60")
abline  (v=(1+31+29+31+30+31+30+31+31+30+31+30+31), lty=4, lwd=1.0, col="grey60")
axis    (side=1, at=1,                                       cex.axis=1.0, label="1-1")
axis    (side=1, at=(1+31),                                  cex.axis=1.0, label="2-1")
axis    (side=1, at=(1+31+29),                               cex.axis=1.0, label="3-1")
axis    (side=1, at=(1+31+29+31),                            cex.axis=1.0, label="4-1")
axis    (side=1, at=(1+31+29+31+30),                         cex.axis=1.0, label="5-1")
axis    (side=1, at=(1+31+29+31+30+31),                      cex.axis=1.0, label="6-1")
axis    (side=1, at=(1+31+29+31+30+31+30),                   cex.axis=1.0, label="7-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31),                cex.axis=1.0, label="8-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31+31),             cex.axis=1.0, label="9-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31+31+30),          cex.axis=1.0, label="10-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31+31+30+31),       cex.axis=1.0, label="11-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31+31+30+31+30),    cex.axis=1.0, label="12-1")
axis    (side=1, at=(1+31+29+31+30+31+30+31+31+30+31+30+31), cex.axis=1.0, label="1-1")
abline  (h=c(0,600,1200,1800,2400,3000,3600,4200,4800,5400,6000,6600,7200), lty=2, lwd=1.0, col="grey40")
median (walk_seconds)
sd     (walk_seconds)
abline (h=median(walk_seconds), lty=1, lwd=2.0, col="firebrick4")
abline (h=(median(walk_seconds) - sd(walk_seconds)), lty=3, lwd=3.00, col="#008000")
abline (h=(median(walk_seconds) + sd(walk_seconds)), lty=3, lwd=3.00, col="#008000")
mtext  (side=4, "median", col="firebrick4", adj="1.00")
mtext  (side=4, "median", col="firebrick4", adj="0.10")
#
Hi Greg,
I think what you want is:

mtext  (side=4, "median", col="firebrick4", at=median(walk_seconds))

"adj" defaults to 0.5 and is usually okay. It looks to me as though
mtext has taken your _character_ strings as non-numeric and then tried
to work something out with par("las"). Experts more familiar with the
underlying code may be able to provide a more elegant answer, but mine
is above.

Jim

On Wed, Dec 30, 2020 at 7:32 AM Gregory Coats via R-help
<r-help at r-project.org> wrote:
#
Why not? Is that a generalization, or specific to this case?
On December 29, 2020 7:54:22 AM PST, "Richard M. Heiberger" <rmh at temple.edu> wrote:

  
    
#
It is a generalization.
Don't use paste() and expression() together because paste is a sort-of
inverse to expression.
In this example I start by typing several characters, and the result
of paste(expression()) is a string containing the characters I
initially typed.
expression(12^6)
[1] "12^6"


expression(12^6)
paste(expression(12^6))

plot(0:1, 0:1)
text(.1, .4, labels=paste(expression(12^6)))
text(.1, .3, labels=expression(12^6))
On Tue, Dec 29, 2020 at 4:15 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
#
... yes, more specifically:

(from ?paste)
"paste converts its arguments (*via* as.character
<http://127.0.0.1:33923/help/library/base/help/as.character>) to character
strings, and concatenates them (separating them by the string given by sep).
"

More to the point, I would say, is that if you need to manipulate
expressions or other language objects you should live in the world of
language objects, rather than converting them back and forth to character
strings.  But this may be off topic and in the weeds;  for which, if so,
apologies.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Dec 29, 2020 at 2:57 PM Richard M. Heiberger <rmh at temple.edu> wrote:

            

  
  
#
This doesn't sound right. When you use expression() it freezes the syntax as the expression you wrote, so expression(paste(x^2,"[", m^2, "]")) is a perfectly valid plotmath expression. The expression() call is _not_ an "inverse" of paste because the presence of paste is supporting the inclusion of non-syntactic elements in this context... it is handled by plotmath, not by actually executing paste(), and expression is delaying evaluation to give plotmath a chance to combine the sub expressions including character literals without as.character or a subsequent eval().

Code that invokes paste on an expression seems unwise to me... but not the other way around.
On December 29, 2020 3:05:21 PM PST, Bert Gunter <bgunter.4567 at gmail.com> wrote:

  
    
#
On 12/29/20 3:45 PM, Jeff Newmiller wrote:
There are three instances of `expression(paste(...))` in the examples 
for plotmath, so Heiberger's perspective was not adopted by the S/R 
plotmath authors. It behaves a bit like `paste0` inside plotmath. In my 
opinion its use in plotmath expressions makes the code less readable 
than if one were to just use "*" as a concatenation operator. The 
example in ?plotmath :


main = expression(paste(plain(sin) * phi, "? and? ",
 ??????????????????????????????? plain(cos) * phi))

... could be better expressed as:

main = expression( plain(sin) * phi *? "? and? " * plain(cos) * phi )


The usual `paste` has a "sep" and "collapse" arguments. The plotmath 
`paste` function does not honor those arguments.
#
On 29/12/2020 4:15 p.m., Jeff Newmiller wrote:
paste and expression are used several times in the code that was posted. 
  The one that causes all the trouble is

labels=paste(PNotSusp,"=\n",mylab)

where mylab is an expression, the result of

  expression(paste(" ",character(x)^6))

The paste() within the expression() call is harmless; the one outside it 
is disastrous.

Duncan Murdoch