[R] Legend in scatterplot and adding regression lines

John Fox jfox at mcmaster.ca
Tue May 13 05:11:01 CEST 2008


Dear Andrew,

The scatterplot() function is in the car package, and xyplot() is in
the lattice package. The Rcmdr dialog boxes for these functions don't
provide for all possible uses, especially in the case of xyplot(). See
the help pages ?scatterplot and ?xyplot for more information. 

With respect to scatterplot(), if you want a legend in other than the
default location in the top margin of the plot, you'll have to add the
legend yourself, via the legend() function.

As a more general matter, if you want to produce a highly customized
graph, it probably makes sense to use R graphics commands to build it
up yourself.

I hope this helps,
 John

On Tue, 13 May 2008 14:29:07 +1200
 "Andrew McFadden" <Andrew.McFadden at maf.govt.nz> wrote:
> Hi R users
> 
> The scatterplot (used package R-Cmdr ) obtained has the legend off
> the
> plot in the upper left 
> I would like to decide where to put the legend - is that option still
> available and how do I do it? 
> code: 
> Dataset <- read.table("J:/Pigmatingsb.txt", header=TRUE, sep="\t",
> na.strings="NA", dec=".", strip.white=TRUE)
> scatterplot(Farrowing.rate~Time.in.Weeks | Operator, reg.line=lm,
> smooth=FALSE, labels=FALSE, boxplots=FALSE, span=0.5, by.groups=TRUE,
> ylab="Farrowing Rate", xlab="Time in Weeks", font.lab=2, cex.lab=1.5,
> cex.axis=1.2, pch=c(16,15,24,3), data=Dataset)
> 
> The other option was to use the following which allowed manipulation
> of
> the legend but had its own problems as I wasn't entirely sure how to
> produce a simple regression line for the scatter for each group?
> 
> 
> library(lattice)
> setwd("C:\\temp")
> Dataset <- read.table("Pigmatingsb.txt", header=TRUE, sep="\t",
> na.strings="NA", dec=".", strip.white=TRUE)
> head(Dataset)
> 
> xyplot(Farrowing.rate~Time.in.Weeks, groups= Operator, type=c("p"data
> =
> Dataset,layout = c(1, 1), panel=panel.superpose ,xlab="Time
> (weeks)",ylab="Farrowing rate", key = list(points =
> Rows(trellis.par.get("superpose.symbol"), 1:4),
>                            text = list(levels(Dataset$Operator)),
>                            columns = 4)) 
> 
> Regards
> 
> Andy
> 
> Andrew McFadden MVS BVSc
> Incursion Investigator
> Investigation & Diagnostic Centres - Wallaceville Biosecurity New 
> Zealand Ministry of Agriculture and Forestry
> 
> Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address:
> 
> Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St 
> Upper Hutt
> 
> 
>
########################################################################
> This email message and any attachment(s) is intended solely for the
> addressee(s) named above. The information it contains is confidential
> and may be legally privileged.  Unauthorised use of the message, or
> the
> information it contains, may be unlawful. If you have received this
> message by mistake please call the sender immediately on 64 4 8940100
> or notify us by return email and erase the original message and
> attachments. Thank you.
> 
> The Ministry of Agriculture and Forestry accepts no responsibility
> for
> changes made to this email or to any attachments after transmission
> from
> the office.
>
########################################################################
> 
> 	[[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.

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/



More information about the R-help mailing list