[R] Adding text outside (lattice barchart) plotting region.
Deepayan Sarkar
deepayan.sarkar at gmail.com
Thu Mar 18 08:23:57 CET 2010
On Thu, Mar 18, 2010 at 5:26 AM, Peng Cai <pengcaimaillist at gmail.com> wrote:
> Thanks Richard and Jim for your reply!
>
> @All:
>
> Is there a way to do this without altering default x-labels (because in my
> case there could be up to 30-40 x-labels and I may have to repeat the script
> multiple times with different x-lables).
>
> OR, not sure but can it be done by extending plotting region parameters for
> lattice plots (though I'm aware of how to change these).
The most general solution I can think of is to use the legend
functionality (whereby new grid objects can be used as legends). A
simple example is
dotplot(yield ~ year, data = barley,
legend = list(bottom =
list(fun = textGrob,
args = list(x = c(0.5, 1, 2), y = 0.5,
label = c("Month:", "March", "April"),
just = "center", default.units
= "native",
vp = viewport(xscale = c(0.4,
2.6), yscale = c(0, 1))))))
More complex layouts will typically need frameGrob (see
lattice::draw.colorkey for an example). Note that the legend doesn't
know what the panel limits are, so you will need to tell it
explicitly.
-Deepayan
>
> Thanks,
> Peng Cai
>
> On Wed, Mar 17, 2010 at 7:27 PM, Jim Lemon <jim at bitwrit.com.au> wrote:
>
>> On 03/18/2010 09:50 AM, Peng Cai wrote:
>>
>>> Hi All,
>>>
>>> I need some help with adding text at the bottom of the graph (outside of
>>> default outline of graph).
>>>
>>> Say, I need to write "Month: 01 04" below each of the two years in
>>> the
>>> following R code, something like this:
>>>
>>> _______________
>>> | |
>>> | Plot |
>>> |___ __________|
>>> 1931 1932
>>> Month: 01 04
>>>
>>> # R code
>>> require(lattice)
>>> barchart(yield ~ year, data = barley,
>>> stack=TRUE,
>>> col="black",
>>> ylim=c(0,85)
>>> )
>>>
>>
>> Hi Peng,
>> Try using mtext:
>>
>> mtext("Month: 01 04",side=1,line=2,at=0,adj=0)
>>
>> You may have to adjust the last three arguments to get it in the correct
>> position.
>>
>> Jim
>>
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list