[R] boxplots instead of a scatterplot

Jim Lemon jim at bitwrit.com.au
Tue Apr 25 02:33:11 CEST 2006


Michael Graber wrote:
> Dear R list,
> 
> I am a newbie to R and programming itself, so my question may be easy to 
> answer for you.
> I wanted to create a scatterplot and i used the following code:
> 
> par(mar=c(10, 4.1,4.1,2.1))
> plot(q$location,q$points, , las=2, cex.axis=0.5,xlab="", ylab="" )
> 
> #location are character strings, there are about 70 locations
> #points are numeric, there are more than 4 points for every location
> 
> my problem is that this code does not create a simple scatterplot with 
> location on the x axis and points on the y axis. Instead of this i get 
> "vertical boxplots" for every location.
> 
Hi Michael,

R is probably trying to interpret the locations as a factor and 
helpfully breaking down the numeric variable by this factor. One way to 
get around this is to fake a numeric for the x axis by using the indices 
of the character variable for plotting, then cram in the labels using 
staxlab in the plotrix package:

# nr.comb is an 'n choose r' function in a private package
# I think there is an equivalent in gregmisc
indies<-nr.comb(9,3)[1:70,]
locations<-
  apply(indies,1,nonsense.words<-function(x) 
paste(LETTERS[x],sep="",collapse=""))
testdf<-data.frame(locations=sample(locations,300,TRUE),numbers=rnorm(300))
location.indices<-
  sapply(testdf$locations,whichloc<-function(x) which(locations==x))
# leave lots of room for the labels
x11(width=18,height=7)
plot(location.indices,testdf$numbers,axes=FALSE)
box()
axis(2)
# you can also try vertical labels using par
staxlab(1,at=1:70,labels=locations,nlines=3)

Jim




More information about the R-help mailing list