[R] Question about "Title"
David Winsemius
dwinsemius at comcast.net
Wed Feb 17 20:03:07 CET 2010
On Feb 17, 2010, at 1:30 PM, David Winsemius wrote:
>
> On Feb 17, 2010, at 1:06 PM, Lu Wang wrote:
>
>> Hi,
>>
>> I want to produce a pie chart with titles. I used the following
>> code. So it created a pie chart for the percentage of the score for
>> ID="002".
>>
>>
>> ID <-
>> c
>> ("001
>> ","001
>> ","002","002","002","003","003","003","004","004","005","005","006")
>> test <-
>> c("A","B","A","B","C","A","B","C","A","B","A","B","A")
>> score <- c(60,90,32,56,89,45,77,82,68,79,56,77,90)
>> mydata <- data.frame(ID,test,score)
>> score_002 <- score[mydata$ID=="002"]
>> lbls <- round(score_002/sum(score_002)*100)
>> lbls <- paste(lbls,"%",sep="")
>> par(mar=c(4,4,4,4),oma=c(0,0,0,0))
>> pie(score_002,labels=lbls,radius=0.5)
>> title(main="Pie chart of score",font.main=3,cex.main=1,line=-2.6)
>> title(main="(Total score: 177)",font.main=3,cex.main=0.8,line=-3.6)
>> title(main="ID is 002",font.main=3,cex.main=0.9,line=-22)
>> As you can see in the title:"(Total score: 177)" and "ID is 002" is
>> inserted by hand. If I want to change the ID to "003" and let R
>> produce the corresponding score sum and ID: 003. How can I do this?
>> Is there any built-in function or I need to write my own function?
>
> ?substitute
Or other options:
> ss <- 120
> ID = "003"
> plot(1,1)
> title(main= substitute(ss~"is sum of ID = "~ID, list(ss=ss, ID=ID)) )
#Or the equivalent:
> plot(1,1)
> title(main= bquote(ss~"is sum of ID = "~ID, .(ss, ID)) )
#Or simply with paste:
> plot(1,1)
> title(main= paste(ss,"is sum of ID = ",ID))
The paste() method is what I would have tried a few months ago, but I
have been struggling to understand the proper use of expressions, so I
tried those first.
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list