# Take care with codes()! (was [R] type of representation)

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Fri Jan 3 20:46:43 CET 2003

```Ahh yes, sorry about that.

Here's the corrected snippet:

# Create an Example Data Frame Containing Car x Color data
carnames <- c("bmw","renault","mercedes","seat")
carcolors <- c("red","white","silver","green")
datavals <- round(rnorm(16, mean=10, sd=4),1)
data <- data.frame(Car=rep(carnames,4),
Color=rep(carcolors, c(4,4,4,4) ),
Value=datavals )
# show the data
data

# plot the Car x Color combinations, using 'cex' to specify the dot size
plot(x=as.numeric(data\$Car),     # as.numeric give numeric values
y=as.numeric(data\$Color),
cex=data\$Value/max(data\$Value)*12,  # standardize size to (0,12)
pch=19,  # filled circle
col="skyblue", # dot color
xlab="Car", # x axis label
ylab="Color", # y axis label
xaxt="n", # no x axis lables
yaxt="n", # no y axis lables
bty="n",  # no box around the plot
xlim=c(0,nlevels(data\$Car  )+0.5), # extra space on either end of plot
ylim=c(0.5,nlevels(data\$Color)+1.5)  # so dots don't cross into margins
)

# add text labels
text(x=1:nlevels(data\$Car), y=nlevels(data\$Car)+1, labels=levels(data\$Car))
text(x=0, y=1:nlevels(data\$Color), labels=levels(data\$Color) )

# add borders between cells
abline(v=(0:nlevels(data\$Car)+0.5))
abline(h=(0:nlevels(data\$Color)+0.5))

# annotate with actual values
text(x=as.numeric(data\$Car),     # as.numeric give numeric values
y=as.numeric(data\$Color),
labels=format(data\$Value),       # label value
col="black", # textt color
)

# put a nice title
title(main="Car by Color Popularity\n(Dot size proportional to popularity)")

-Greg

> -----Original Message-----
> From: ripley at stats.ox.ac.uk [mailto:ripley at stats.ox.ac.uk]
> Sent: Friday, January 03, 2003 1:53 PM
> To: Warnes, Gregory R
> Cc: 'allende at gredos.cnb.uam.es'; 'r-help at stat.math.ethz.ch'
> Subject: RE: Take care with codes()! (was [R] type of representation)
>
>
> From the help page of codes():
>
>      Normally `codes' is not the appropriate function to use with an
>      unordered factor.  Use `unclass' or `as.numeric' to extract the
>      codes used in the internal representation of the factor, as these
>      do not assume that the codes are sorted.
>
> and this is one of the `normally' cases.  Your code will only work
> correctly if the levels are in alphabetical order (in the
> locale in use).
>
> On Fri, 3 Jan 2003, Warnes, Gregory R wrote:
>
> >
> > # Create an Example Data Frame Containing Car x Color data
> > carnames <- c("bmw","renault","mercedes","seat")
> > carcolors <- c("red","white","silver","green")
> > datavals <- round(rnorm(16, mean=10, sd=4),1)
> > data <- data.frame(Car=rep(carnames,4),
> >                    Color=rep(carcolors, c(4,4,4,4) ),
> >                    Value=datavals )
> > # show the data
> > data
> >
> > # plot the Car x Color combinations, using 'cex' to specify
> the dot size
> > plot(x=codes(data\$Car),     # codes give numeric values
> >      y=codes(data\$Color),
> >      cex=data\$Value/max(data\$Value)*12,  # standardize size
> to (0,12)
> >      pch=19,  # filled circle
> >      col="skyblue", # dot color
> >      xlab="Car", # x axis label
> >      ylab="Color", # y axis label
> >      xaxt="n", # no x axis lables
> >      yaxt="n", # no y axis lables
> >      bty="n",  # no box around the plot
> >      xlim=c(0,nlevels(data\$Car  )+0.5), # extra space on
> either end of plot
> >      ylim=c(0.5,nlevels(data\$Color)+1.5)  # so dots don't
> cross into margins
> >      )
> >
> > # add text labels
> > text(x=1:nlevels(data\$Car), y=nlevels(data\$Car)+1,
> labels=levels(data\$Car))
> > text(x=0, y=1:nlevels(data\$Color), labels=levels(data\$Color) )
> >
> > # add borders between cells
> > abline(v=(0:nlevels(data\$Car)+0.5))
> > abline(h=(0:nlevels(data\$Color)+0.5))
> >
> > # annotate with actual values
> > text(x=codes(data\$Car),     # codes give numeric values
> >      y=codes(data\$Color),
> >      labels=format(data\$Value),       # label value
> >      col="black", # textt color
> >      )
> >
> > # put a nice title
> > title(main="Car by Color Popularity\n(Dot size proportional
> to popularity)")
> >
> >
> > -Greg
> >
> > > -----Original Message-----
> > > From: allende at gredos.cnb.uam.es [mailto:allende at gredos.cnb.uam.es]
> > > Sent: Friday, January 03, 2003 4:46 AM
> > > To: r-help at stat.math.ethz.ch
> > > Cc: allende at gredos.cnb.uam.es
> > > Subject: [R] type of representation
> > >
> > >
> > > Hi
> > >
> > > I have some data that i want to plot but i don't find how to
> > > do it. I have car
> > > types (bmw,renault,mercedes,seat ...), colors and a number
> > > for each car
> > > type-color relation.I want to come up with a matrix
> > > representation of cars vs
> > > colors where in each intersection i could set a dot
> > > proportional in size to my
> > > third variable.
> > >
> > >
> > > Can anybody give me a clue of hoe to come up with such
> representation.
> > >
> > > Thanks
> > >
> > > Ramon
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > >
> >
> >
> > LEGAL NOTICE\ Unless expressly stated otherwise, this
> message is ... [[dropped]]
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
>
> --
> 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
>

LEGAL NOTICE\ Unless expressly stated otherwise, this message is ... [[dropped]]

```