R : Version 1.9.1
Hi,
Am having trouble adding a legend to scatterplot. R code is shown below.
I have tried various incantations to add a legend (using the legend()
function) to the resulting plot but without any success. Looks like it
should be simple but I must be missing something. Any pointers would be
welcome.
Have looked at help(legend) etc.
--8<----------------------------------------------------------------------
---
sfiles <- c("72_12_12_V.csv ",
"150_25_15_V.csv",
"150_25_20_V.csv",
"150_25_25_V.csv",
"150_25_40_V.csv",
"150_25_60_V.csv",
"150_25_90_V.csv",
"240_40_40_V.csv")
## process each file in list
for (i in 1:length(sfiles)) {
data <- read.csv(paste("../data/",sfiles[i],sep=""))
## assign columns to some nice names
K <- data[,8]
AN <- data[,3] * (data[,2] - data[,4])
## plot K against AN
if ( i == 1) {
plot(AN, K, ylim=c(1000,9000), xlim=c(0,1500),
xlab="Area above Notch (mm)",
main="Size Effect Specimens")
par(new=TRUE)
}
else{
plot(AN,K, pch=(i),ylim=c(1000,9000), xlim=c(0,1500),
axes=FALSE,xlab="")
par(new=TRUE)
}
}
--8<----------------------------------------------------------------------
---
--
Sean Richards
C-fACS
P.O. Box 84, Lincoln University,
Canterbury, New Zealand
Phone: (64)(3) 325-2811 ext 8636
Email: richars3 at lincoln.ac.nz
You have not called legend() in your codes below, so we do not know what your problem is. See other comments below. On Mon, 2004-11-15 at 01:08, Sean David Richards wrote:> R : Version 1.9.1 > > Hi, > > Am having trouble adding a legend to scatterplot. R code is shown below. > I have tried various incantations to add a legend (using the legend() > function) to the resulting plot but without any success. Looks like it > should be simple but I must be missing something. Any pointers would be > welcome. > Have looked at help(legend) etc.help(legend) provides many nice examples. Here is a simplified one : x <- seq(-pi, pi, len = 65) plot(x, sin(x), type="l", lty=1, col=1) lines(x, cos(x), type="l", lty=2, col=2) legend(-pi, 1, legend=c("sin", "cosine"), lty=1:2, col=1:2) Or you can replace the last line with legend(locator(1), legend=c("sin", "cosine"), lty=1:2, col=1:2) where the legend will be placed on mouse left click.> --8<---------------------------------------------------------------------- > --- > > sfiles <- c("72_12_12_V.csv ", > "150_25_15_V.csv", > "150_25_20_V.csv", > "150_25_25_V.csv", > "150_25_40_V.csv", > "150_25_60_V.csv", > "150_25_90_V.csv", > "240_40_40_V.csv") > > ## process each file in list > for (i in 1:length(sfiles)) { > data <- read.csv(paste("../data/",sfiles[i],sep="")) > > ## assign columns to some nice names > K <- data[,8] > AN <- data[,3] * (data[,2] - data[,4]) > > ## plot K against ANPlease give a simplified example. You do not need to show us all the preprocessing steps. It can be distracting.> if ( i == 1) { > plot(AN, K, ylim=c(1000,9000), xlim=c(0,1500), > xlab="Area above Notch (mm)", > main="Size Effect Specimens") > par(new=TRUE) > } > else{ > plot(AN,K, pch=(i),ylim=c(1000,9000), xlim=c(0,1500), > axes=FALSE,xlab="") > par(new=TRUE) > } > }Have you considered points() or lines() here ? You could simplify to plot(0,1000, type="n", xlim=c(0,1500), ylim=c(1000,9000), xlab="Area above Notch (mm)", main="Size Effect Speciments") n <- length(sfiles) for (i in 1:n) { data <- read.csv(paste("../data/",sfiles[i],sep="")) K <- data[,8] AN <- data[,3] * (data[,2] - data[,4]) points( AN, K, pch=i, col=i ) } legend( 1500, 9000, legend=paste("Data from", sfiles), pch=1:n, col=i )> --8<---------------------------------------------------------------------- > ---
Sorry typo. The last line should read
legend(1500, 9000, legend=paste("Data from", sfiles), pch=1:n, col=1:n
)
^^^
On Mon, 2004-11-15 at 11:39, Adaikalavan Ramasamy wrote:> You have not called legend() in your codes below, so we do not know what
> your problem is. See other comments below.
>
> On Mon, 2004-11-15 at 01:08, Sean David Richards wrote:
> > R : Version 1.9.1
> >
> > Hi,
> >
> > Am having trouble adding a legend to scatterplot. R code is shown
below.
> > I have tried various incantations to add a legend (using the legend()
> > function) to the resulting plot but without any success. Looks like it
> > should be simple but I must be missing something. Any pointers would
be
> > welcome.
> > Have looked at help(legend) etc.
>
> help(legend) provides many nice examples. Here is a simplified one :
>
> x <- seq(-pi, pi, len = 65)
> plot(x, sin(x), type="l", lty=1, col=1)
> lines(x, cos(x), type="l", lty=2, col=2)
> legend(-pi, 1, legend=c("sin", "cosine"), lty=1:2,
col=1:2)
>
> Or you can replace the last line with
> legend(locator(1), legend=c("sin", "cosine"), lty=1:2,
col=1:2)
> where the legend will be placed on mouse left click.
>
> >
--8<----------------------------------------------------------------------
> > ---
> >
> > sfiles <- c("72_12_12_V.csv ",
> > "150_25_15_V.csv",
> > "150_25_20_V.csv",
> > "150_25_25_V.csv",
> > "150_25_40_V.csv",
> > "150_25_60_V.csv",
> > "150_25_90_V.csv",
> > "240_40_40_V.csv")
> >
> > ## process each file in list
> > for (i in 1:length(sfiles)) {
> > data <-
read.csv(paste("../data/",sfiles[i],sep=""))
> >
> > ## assign columns to some nice names
> > K <- data[,8]
> > AN <- data[,3] * (data[,2] - data[,4])
> >
> > ## plot K against AN
>
> Please give a simplified example. You do not need to show us all the
> preprocessing steps. It can be distracting.
>
> > if ( i == 1) {
> > plot(AN, K, ylim=c(1000,9000), xlim=c(0,1500),
> > xlab="Area above Notch (mm)",
> > main="Size Effect Specimens")
> > par(new=TRUE)
> > }
> > else{
> > plot(AN,K, pch=(i),ylim=c(1000,9000), xlim=c(0,1500),
> > axes=FALSE,xlab="")
> > par(new=TRUE)
> > }
> > }
>
> Have you considered points() or lines() here ? You could simplify to
>
> plot(0,1000, type="n", xlim=c(0,1500), ylim=c(1000,9000),
> xlab="Area above Notch (mm)", main="Size Effect
Speciments")
>
> n <- length(sfiles)
>
> for (i in 1:n) {
> data <-
read.csv(paste("../data/",sfiles[i],sep=""))
> K <- data[,8]
> AN <- data[,3] * (data[,2] - data[,4])
>
> points( AN, K, pch=i, col=i )
> }
>
> legend( 1500, 9000, legend=paste("Data from", sfiles), pch=1:n,
col=i )
>
> >
--8<----------------------------------------------------------------------
> > ---
>
> ______________________________________________
> 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
--
Adaikalavan Ramasamy ramasamy at cancer.org.uk
Centre for Statistics in Medicine http://www.ihs.ox.ac.uk/csm/
Cancer Research UK Tel : 01865 226 677
Old Road Campus, Headington, Oxford Fax : 01865 226 962
On 15 Nov 2004 at 12:11, Adaikalavan Ramasamy wrote:> > Have you considered points() or lines() here ? You could simplifyto> > > > plot(0,1000, type="n", xlim=c(0,1500), ylim=c(1000,9000), > > xlab="Area above Notch (mm)", main="Size Effect Speciments") > > > > n <- length(sfiles) > > > > for (i in 1:n) { > > data <- read.csv(paste("../data/",sfiles[i],sep="")) > > K <- data[,8] > > AN <- data[,3] * (data[,2] - data[,4]) > > > > points( AN, K, pch=i, col=i ) > > } > > > > legend( 1500, 9000, legend=paste("Data from", sfiles), pch=1:n,col=i:n) Thanks this got me going on the right track. The code is a lot more concise as well :) Using locator() instead of x,y coord was suggested by Tom and that showed me where my problem was. The legend was being created just not where it would be visible. I found this bit of code in the R-help archives and it makes thing a lot more straightforward when positioning a legend ## set the range of the usr coordinates to x = (0,1), y = (0,1) opar <- par(no.readonly=TRUE) par(usr=c(0,1,0,1)) ## add the legend legend(0.75,0.9,sub(".csv","",nfiles), pch=1:length(nfiles), cex=0.7) Cheers -- Sean Richards C-fACS P.O. Box 84, Lincoln University, Canterbury, New Zealand Phone: (64)(3) 325-2811 ext 8636 Email: richars3 at lincoln.ac.nz
hi, I try to used R to do one-way anova. here is the simple code f1<- lm (y ~ block, data=yd) there are 8 levels of factor block, I also want produce multiple pairwise comparisons for the 8 levels of block, inlcuding mean and std err for each of the 8 lelevls. It is tidious to do pair test. I looked the manuals, find no clues. any suggest? Regards, Yuandan
On Tue, 16 Nov 2004, Yuandan Zhang wrote:> I try to used R to do one-way anova. > > here is the simple code > > f1<- lm (y ~ block, data=yd) > > there are 8 levels of factor block, > > I also want produce multiple pairwise comparisons for the 8 levels of > block, inlcuding mean and std err for each of the 8 lelevls. It is > tidious to do pair test. I looked the manuals, find no clues. any > suggest??TukeyHSD library(help=multcomp) Chapter 6 of MASS4, a book recommended in the FAQ, has worked examples, and its scripts are included in the VR package for R. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595