[R] plot legend outside the grid

Ghosh, Sandeep sghosh at lexgen.com
Tue Jul 5 16:21:10 CEST 2005


Thanks Bert for all the help. I got the legend figured out Friday but left early becoz of long weekend so didn't get a chance to reply.. I modified the plot margins a little bit and Here's what I finally had...
par(mar=c(c(10, 6, 6, 10) + 0.1));

par(xpd=FALSE);

with (dataFrame, stripchart(marbles_buried ~ genotype, method="jitter", vertical=TRUE,  col = c('blue', 'red', 'green'), xlab='Genotype', ylab = "Marbles Buried", main='MBA WTs Vs HOMs', pch=c(1,4,2), jitter=1/3.5, cex=1));

meds <- as.vector(with(dataFrame, by(marbles_buried, genotype, mean)));
segments((1:3)-0.25, meds, (1:3)+0.25, meds, col = c('blue', 'red', 'green'));

dataWt <- subset(dataFrame, genotype=='wt', select=c(marbles_buried,genotype));
dataHet <- subset(dataFrame, genotype=='het', select=c(marbles_buried,genotype));
dataHom <- subset(dataFrame, genotype=='hom', select=c(marbles_buried,genotype));

wtCount <- length(dataWt$marbles_buried);
hetCount <- length(dataHet$marbles_buried);
homCount <- length(dataHom$marbles_buried);
wtLegend <- paste ("wt, (n=", wtCount, ")");
hetLegend <- paste ("het, (n=", hetCount, ")");
homLegend <- paste ("hom, (n=", homCount, ")");

par(xpd=TRUE);
legend(3.8, max(as.vector(dataFrame$marbles_buried)), c(wtLegend, hetLegend, homLegend), col=c('blue', 'red', 'green'), pch=c(1,4,2));

Again thanks so much for all the pointers..

- Sandeep

-----Original Message-----
From: Berton Gunter [mailto:gunter.berton at gene.com]
Sent: Friday, July 01, 2005 7:37 PM
To: Ghosh, Sandeep
Subject: RE: plot legend outside the grid 


 You are not specifying the x,y position of the legend correctly. You do not
need to end each statement with a ";"; you should set par(xpd=TRUE) before
you do the plot. On a windows device, I used locator() to determine the
coordinates of where to put he legend. The following produced a clean legend
that does not get mixed up with the plotted points:

legend(2.5,72, c(wtLegend, hetLegend, homLegend), col=c('blue', 'red',
'green'), pch=c(1,4,2))

If you change the 72 to 75 the legend is plotted outside the plot frame.

Please read the help files carefully and also "An Introduction to R" to
learn R's syntax.

-- Bert

-----Original Message-----
From: Ghosh, Sandeep [mailto:sghosh at lexgen.com] 
Sent: Thursday, June 30, 2005 3:43 PM
To: Berton Gunter
Subject: plot legend outside the grid 

Thanks for the pointers... I managed to get everything to look and feel the
way I want except for the legend to plot outside the grid... Thanks for the
note on the par, but I'm not able to it to plot outside the plot grid..

dataFrame <- as.data.frame(t(structure(c(
64,'wt',
62,'wt',
66,'wt',
65,'wt',
60,'wt',
61,'wt',
65,'wt',
66,'wt',
65,'wt',
63,'wt',
67,'wt',
65,'wt',
62,'wt',
65,'wt',
68,'wt',
65,'wt',
63,'wt',
65,'wt',
62,'wt',
65,'wt',
66,'wt',
62,'wt',
65,'wt',
65,'wt',
66,'wt',
65,'wt',
62,'wt',
65,'wt',
66,'wt',
65,'wt',
61,'wt',
65,'wt',
66,'wt',
65,'wt',
62,'wt',
63,'het',
67,'het',
60,'het',
67,'het',
66,'het',
62,'het',
65,'het',
62,'het',
61,'het',
62,'het',
66,'het',
60,'het',
65,'het',
65,'het',
61,'het',
64,'het',
68,'het',
64,'het',
63,'het',
62,'het',
64,'het',
62,'het',
64,'het',
65,'het',
60,'het',
65,'het',
70,'het',
63,'het',
67,'het',
66,'het',
65,'hom',
62,'hom',
68,'hom',
67,'hom',
67,'hom',
63,'hom',
67,'hom',
66,'hom',
63,'hom',
72,'hom',
62,'hom',
61,'hom',
66,'hom',
64,'hom',
60,'hom',
61,'hom',
66,'hom',
66,'hom',
66,'hom',
62,'hom',
70,'hom',
65,'hom',
64,'hom',
63,'hom',
65,'hom',
69,'hom',
61,'hom',
66,'hom',
65,'hom',
61,'hom',
63,'hom',
64,'hom',
67,'hom'), .Dim=c(2,98))));

colnames(dataFrame) <- c('marbles_buried', 'genotype');

png('mb.png', width=400, height=400, pointsize=8);

dataFrame[c("marbles_buried")] <- lapply(dataFrame[c("marbles_buried")],
function(x) as.numeric(levels(x)[x]));

par(xpd=FALSE)

with (dataFrame, stripchart(marbles_buried ~ genotype, method="jitter",
vertical=TRUE,  col = c('blue', 'red', 'green'), xlab='Genotype', ylab =
"Marbles Buried", main='MBA WTs Vs HOMs', pch=c(1,4,2), jitter=1/3.5,
cex=1))

meds <- as.vector(with(dataFrame, by(marbles_buried, genotype, mean)))
segments((1:3)-0.25, meds, (1:3)+0.25, meds, col = c('blue', 'red',
'green'));

dataWt <- subset(dataFrame, genotype=='wt',
select=c(marbles_buried,genotype));
dataHet <- subset(dataFrame, genotype=='het',
select=c(marbles_buried,genotype));
dataHom <- subset(dataFrame, genotype=='hom',
select=c(marbles_buried,genotype));

wtCount <- length(dataWt$marbles_buried);
hetCount <- length(dataHet$marbles_buried);
homCount <- length(dataHom$marbles_buried);
wtLegend <- paste ("wt, (n=", wtCount, ")");
hetLegend <- paste ("het, (n=", hetCount, ")");
homLegend <- paste ("hom, (n=", homCount, ")");

par(xpd=TRUE)
legend(1, max(as.vector(dataFrame$marbles_buried)), c(wtLegend, hetLegend,
homLegend), col=c('blue', 'red', 'green'), pch=c(1,4,2));

-Thanks
Sandeep.

-----Original Message-----
From: Berton Gunter [mailto:gunter.berton at gene.com]
Sent: Thursday, June 30, 2005 2:55 PM
To: Ghosh, Sandeep
Subject: RE: [R] Help with stripplot


Of course!

stripchart() is a base graphics function and tehrefore has available to it
the base graphics functionality, like (the base graphics function, **not**
the lattice argument) legend(). See ?legend in the graphics package. Note
the use of locator() for positioning the legend.

Note: By default the legend will be clipped to the plot region. If you wish
to have a legend outside the plot region set the xpd parameter of par to
TRUE or NA prior to plotting.

-- Bert 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ghosh, Sandeep
> Sent: Thursday, June 30, 2005 12:22 PM
> To: Deepayan Sarkar; r-help at stat.math.ethz.ch
> Subject: Re: [R] Help with stripplot
> 
> Another question, in stripchart is there a way to draw a 
> legends. I need legends that gives the mice count for each 
> genotype wt/het/hom, something like the xyplot plot support 
> for key/auto.key.
> 
> -Sandeep




More information about the R-help mailing list