[R] Adding text to multiple plots

David Winsemius dwinsemius at comcast.net
Tue Jan 24 17:39:51 CET 2012


On Jan 24, 2012, at 11:20 AM, Katarzyna Sawicka wrote:

> Dear David,
>
> Thanks for your reply. However, I can't see how this is a case. The  
> table is read into R from .csv file. The column Date has a factor  
> format then. If I don't change it to as.Date format and use the same  
> code, the 'text' function print the mean by the maximum on y ax  
> regardless to the column Date being chosen for x ax. But if the Date  
> stays as factor on the x ax function 'plot' returns boxplots (not  
> what I want), not single points for each value of A,B,C or D  
> (desired). The problem with 'text' starts when I change the column  
> 'Date' format in order to get the points instead of boxplots. Any  
> idea how to solve that?
>

Sorry. I wasn't able to follow that problem statement. Let me ask a  
counter question: Are you aware that the x-values in your plot calls  
are being converted to the numeric values for the dates?

 > as.numeric(Sys.Date())
[1] 15363

So... I (still) think your x-values for 'text' are not matching your x- 
values for 'plot' AND you are not supplying your y-values for 'text'  
in a sensible manner.

 > plot(Sys.Date(), 1)
 > text(x=15363, labels="here is today") # nothing plotted
 > text(x=15363, y=0.8, labels="here is today") # plotted as expected



> Cheers,
> Kasia
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: 24 January 2012 15:22
> To: Katarzyna Sawicka
> Cc: r-help at r-project.org
> Subject: Re: [R] Adding text to multiple plots
>
>
> On Jan 24, 2012, at 8:12 AM, ksaw wrote:
>
>> Dear R-helpers,
>>
>> I am trying to add an information about the mean value for each
>> variable within each plot by adding text to the multiple plots in  
>> this
>> way:
>>
>> par(mfrow=c(2,2))
>> df$Date <- as.Date(df$Date, format="%d/%m/%Y")
>>
>> for (i in 2:5){
>> 	plot(df$Date, df[[i]])
>> 	Mean <- mean(na.omit(df[[i]]))
>> 	text(x=max(na.omit(df[[i]])), labels=paste("Mean = ", Mean, sep=""))
>> 	}
>>
>> where df is an example table:
>> Date	A	B	C	D
>> 01/12/2008	3	6	3	6
>> 14/12/2008	4	7	4	7
>> 27/12/2008	4	5	65	8
>> 09/01/2009	5	5	7	6
>> 22/01/2009	23	4	8	6
>> 04/02/2009	2	3	5	2
>> 17/02/2009	4	65	4	2
>> 02/03/2009	5	6	3	2
>> 15/03/2009	5	7	3	5
>> 28/03/2009	6	7	2	5
>> 10/04/2009	78	5	2	6
>> 23/04/2009	3	3	1	NA
>> 06/05/2009	4	3	4	6
>>
>> It does work if the Date is not in a date format. When changing the
>> Date format into date there is no output from text within a loop.
>
> Most likely because the plot is being constructed with dates as the  
> x- value and your `text` call has the x's as what you are  
> considering to be the y-values.
>
>
>> Is there a way
>> to keep the date format as.Date and be able to print the information
>> about the mean on each plot?
>>
>> Many thanks
>> Kasia
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Adding-text-to-multiple-plots- 
>> tp4323717p
>> 4323717.html Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list