[R] Limiting size of pairs plots
Sébastien
pomchip at free.fr
Mon May 12 20:36:40 CEST 2008
Dear R-users,
This is a follow-up on a quite old post of mine which dealt with margins
in the pairs function. I thought my problem was solved, but it doesn't
seem so (see the code below).
I use the pairs function to produce matrix plots, where distinct groups
are represented by dots of different colors within each panel. My
troubles start when I add a legend at the bottom of the plot. The steps are:
1- I build my dataframe
2- I plot the graph and define the margins within pairs with oma (I want
to add 5 + my number of group lines at the bottom)
3- I use gridBase functions to add the legend (symbol + text)
The result is an overlay of the legend on the plot. It looks like the
lines do not have the same size when using pairs or the gridBase
functions. Could anyone explain this discrepancy to me?
Thank you in advance for your help.
Sebastien
####### START########
library(gridBase)
my.Group.number=12
my.df<-data.frame(rnorm(my.Group.number*20, mean=5,
sd=2),rnorm(my.Group.number*20, mean=4,
sd=1.6),rnorm(my.Group.number*20, mean=8,
sd=3),rnorm(my.Group.number*20, mean=6,
sd=1),rep(letters[1:my.Group.number],each=20))
names(my.df)<-c("Var1","Var2","Var2","Var2","Group")
pairs(my.df[1:4],
main = "My Title",
pch = 21,
col =rainbow(n=my.Group.number)[unclass(my.df$Group)],
bg = rainbow(n=my.Group.number)[unclass(my.df$Group)],
oma = c(5 + my.Group.number, 3, 5, 3)
)
mylegend = paste("Group ",letters[1:my.Group.number],sep="")
mylegend.width = strwidth(mylegend[which.max(nchar(mylegend))],
"figure", mylegend.Cex(mylegend,myTarget = 0.90))
vps <- baseViewports()
pushViewport(vps$inner)
for (j in 1:my.Group.number) {
grid.text(mylegend[j],
x = unit((1-mylegend.width)/2,"npc"),
y = unit(1 + my.Group.number - j,"lines"),
gp = gpar(cex = 1),
just = "left",
draw=TRUE)
grid.points(x =
unit((1-mylegend.width)/2,"npc")-convertUnit(unit(0.5,"lines"),"npc"),
y = unit(1 + my.Group.number - j,"lines"),
size = unit(0.5, "lines"),
default.units = "lines",
pch = 21,
gp = gpar(col = rainbow(n=my.Group.number)[j],fill =
rainbow(n=my.Group.number)[j]),
draw=TRUE)
}
######## END ########
Prof Brian Ripley a écrit :
> On Tue, 28 Aug 2007, Sébastien wrote:
>
>> Thanks you very much.
>> I did not thought about calling oma inside the pairs function...
>>
>> Do you know by any chance why 'pairs' behaves differently from
>> 'plot', with regards to the plot dimension ?
>
> Well, it is an array of figures so why should it be the same? See
> chapter 12 of 'An Introduction to R'.
>
>>
>> Prof Brian Ripley a écrit :
>>>> From ?pairs
>>>
>>> The graphical parameter 'oma' will be set by 'pairs.default'
>>> unless supplied as an argument.
>>>
>>> so try
>>>
>>> pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21,
>>> bg = c("red", "green3", "blue")[unclass(iris$Species)],
>>> oma = c(8,3,5,3))
>>>
>>>
>>>
>>> On Tue, 28 Aug 2007, Sébastien wrote:
>>>
>>>> Dear R-users,
>>>>
>>>> I would like to add a legend at the bottom of pairs plots (it's my
>>>> first
>>>> use of this function). With the plot function, I usually add some
>>>> additional space at the bottom when I define the size of the graphical
>>>> device (using mar); grid functions then allows me to draw my legend
>>>> as I
>>>> want.
>>>> Unfortunatley, this technique does not seem to work with the pairs
>>>> function as the generated plots use all the available space on the
>>>> device (see below). I guess I am missing a key argument... my attempts
>>>> to modify the oma, mar, usr arguments were unsuccesfull, and I
>>>> could not
>>>> find any helpful threads on the archives.
>>>>
>>>> As usual, any advice would be greatly appreciated
>>>>
>>>> Sebastien
>>>>
>>>>
>>>> pdf(file="C:/test.pdf", width=6, height= 6 + 0.2*6)
>>>>
>>>> par(mar=c(5 + 6,4,4,2)+0.1)
>>>>
>>>> pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21,
>>>> bg = c("red", "green3", "blue")[unclass(iris$Species)])
>>>>
>>>> dev.off()
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch 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