[R] How is label parameter used in text() when passing it a vector?

Jim Lemon drjimlemon at gmail.com
Thu Oct 20 23:55:29 CEST 2016


Hi again,
Sorry, the text command should read:

text(x=dta$age[samp010],y=dta$fit5[samp010],labels=dta$exposure[samp010],
 col=expcol[samp010])

Jim

On Fri, Oct 21, 2016 at 8:50 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
> Hi mviljama,
> Without knowing what "dta" contains, it's a bit difficult. Here is an example:
>
> set.seed(2345)
> dta<-data.frame(age=sample(20:80,50),skin=sample(0:1,50,TRUE),
>  gender=sample(0:1,50,TRUE),trt=sample(0:1,50,TRUE),
>  exposure=sample(1:21,50,TRUE),fit5=runif(50))
> # define your subset here for better readability
> samp010<-dta$trt==0 & dta$skin==1 & dta$gender==0
> library(plotrix)
> # create your exposure colors
> expcol<-color.scale(dta$exposure,extremes=c("red","green"))
> par(mar=c(5,4,4,4))
> # set up an empty plot
> plot(x=dta$age[samp010],y=dta$fit5[samp010],type="n")
> # add the points as text
> text(x=dta$age[samp010],y=dta$fit5[samp010],col=expcol[samp010])
> legend.values<-seq(1,21,length.out=5)
> # add a legend for the colors
> color.legend(47,0.2,49,0.5,gradient="y",legend=legend.values,
>  rect.col=color.scale(legend.values,extremes=c("red","green")))
> # label the legend
> text(48,0.55,"Exposure")
>
> Jim
>
>
> On Fri, Oct 21, 2016 at 4:33 AM, mviljamaa <mviljamaa at kapsi.fi> wrote:
>> I have a slight doubt with using text() with the label parameter having to
>> contain a vector of of integers (specifically integers in the range [1, 21]
>> corresponding to factors of my categorical variable that I want to numbers
>> to tell).
>>
>> What I'm currently plotting is the following command:
>>
>> text(dta[trt == 0 & skin == 1 & gender == 0,]$age,invlogit(predict(fit5,
>> newdata=dta[trt == 0 & skin == 1 & gender == 0,])),label=1:21, col = dta[trt
>> == 0 & skin == 1 & gender == 0,]$exposure)
>>
>> (it's a plot of a logistic model)
>>
>> $exposure is the categorical variable that takes values of integers in the
>> range [1, 21]. The coloring seems to work, but I'm not sure whether
>> label=1:21 makes it so that it actually labels the point with the
>> corresponding exposure level or whether it just walks through 1 to 21 and 1
>> to 21 over and over again.
>>
>> Can someone explain?
>>
>> Also how can I get the label parameter to put the number of the level of the
>> exposure as the corresponding point?
>>
>> ______________________________________________
>> R-help at 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