[R] A Labeling the median

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Tue Dec 29 22:09:26 CET 2020


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 using r-project.org> 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")
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list