[R] monthly boxplot

Brian Diggs diggsb at ohsu.edu
Tue Aug 9 21:15:55 CEST 2011


On 8/9/2011 10:32 AM, Fernando Andreacci wrote:
> Hi, I got what I want using
>
>
> lines(varmeasure ~ vardates)
>
> but, as I'm using as.Date(vardates) in my boxplot code
>
> boxplot(varmeasure ~ as.Date(vardate))
>
> I want to change how vardate is displayed
> I used strftime(as.Date(vardate), format="%m/%Y") and it worked well,
> However it mixed the inital and final dates.
>
> My data has measuraments from october 2010 to june 2011.
>
> If I use  boxplot(varmeasure ~ as.Date(vardate)), data is displayed from
> 2010 to 2011.
>
> if I use boxplot(varmeasure ~ strftime(as.Date(vardate), format="%m/%Y"))
> data is displayed mixing 2010 and 2011 months in order to get jan to dez
> display.
>
>
> I need a way to keep my data display from oct 2010 to june 2011 but I also
> need a way to change how X scale is displayed.
>
> How can do it?

Make the "date" variable a factor with the right format and the levels 
in chronological order:

vardates <- c("10/1/2010", "10/1/2010", "10/1/2010", "10/1/2010",
	"10/1/2010", "10/1/2010", "10/1/2010", "10/1/2010", "10/1/2010",
	"11/1/2010", "11/1/2010", "11/1/2010", "11/1/2010", "11/1/2010",
	"11/1/2010", "11/1/2010", "11/1/2010", "11/1/2010", "12/1/2010",
	"12/1/2010", "12/1/2010", "12/1/2010", "12/1/2010", "12/1/2010")
varmeasure <- c(0.0, 26.0,  0.2, -0.2, -1.2, -0.8,  0.0,  4.4, -0.6,
	0.2, 14.4, -0.2, 4.8, 4.0, 2.8, 3.2, 3.8, 3.2, -11.4, 0.2, 0.4,
	3.0, 0.6, 6.2)

vardates <- as.Date(vardates, format="%m/%d/%Y")
vardates.fmt <- factor(strftime(vardates, format="%m/%Y"),
	levels=strftime(sort(unique(vardates)), format="%m/%Y"))

boxplot(varmeasure ~ vardates)
boxplot(varmeasure ~ vardates.fmt)

> Thanks
>
> On Tue, Aug 9, 2011 at 12:32 PM, David Winsemius<dwinsemius at comcast.net>wrote:
>
>>
>> On Aug 9, 2011, at 10:17 AM, Fernando Andreacci wrote:
>>
>>   It worked, thanks.
>>>
>>> Is there a way to put a trend line through the boxplots?
>>>
>>
>> You have not shown us how you set the data up or made the boxplots. A
>> "trend line through boxplots" seems be a bit ambiguous, since the x-variable
>> needs to be a factor and  a factor variable would not necessarily have
>> either a scale or an order. So the answer with the available information
>> cannot be anymore specific than "it depends".
>>
>> --
>> David.
>>
>>>
>>>
>>> On Tue, Aug 9, 2011 at 10:58 AM, David Winsemius<dwinsemius at comcast.net
>>>> wrote:
>>>
>>>
>>>> On Aug 9, 2011, at 9:46 AM, Fernando Andreacci wrote:
>>>>
>>>> I'm trying to make a monthly boxplot using this:
>>>>
>>>>>
>>>>> boxplot(varmeasure ~ vardates)
>>>>>
>>>>> vardates = [1] 10/1/2010 10/1/2010 10/1/2010 10/1/2010 10/1/2010
>>>>> 10/1/2010
>>>>> 10/1/2010
>>>>> [8] 10/1/2010 10/1/2010 11/1/2010 11/1/2010 11/1/2010 11/1/2010
>>>>> 11/1/2010
>>>>> ....
>>>>>
>>>>> varmeasure =  [1]   0.0  26.0   0.2  -0.2  -1.2  -0.8   0.0   4.4  -0.6
>>>>> -0.2  14.4  -0.2
>>>>> [13]   4.8   4.0   2.8   3.2   3.8   3.2 -11.4   0.2   0.4   3.0   0.6
>>>>> 6.2
>>>>> ....
>>>>>
>>>>>
>>>>> they have same size.
>>>>>
>>>>>
>>>>> My problem is that R is ploting ordered by months and not by year and by
>>>>> months
>>>>>
>>>>> I'm getting 1/1/2011, 10/1/2010, 11/1/2010, 2/1/2011
>>>>>
>>>>> How can I plot it in chronological order?
>>>>>
>>>>>
>>>> By converting those character strings to dates. At the moment you seem to
>>>> believe that R has the ability to "know" that you want these strings to
>>>> be
>>>> dates.
>>>>
>>>> ?as.Date
>>>> ?Dates
>>>>
>>>> --
>>>>
>>>> David Winsemius, MD
>>>> West Hartford, CT
>>>>
>>>>
>>>>
>>>
>>> --
>>> Fernando Andreacci
>>> Biólogo
>>> Fone +55 47 9921 4015
>>>        +55 41 9921 3934
>>> fandreacci at gmail.com
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html<http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>
> 	[[alternative HTML version deleted]]
>
>
>
>


-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list