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

Ramon Alonso-Allende allende at cnb.uam.es
Sat Mar 8 12:40:43 CET 2003


Hi

I have been ussing this code displayed while a go to do balloons plots.

My problem is that the labels of the data i'm working on now are to big 
and they overlap in the X axis.

Is there any way i can plot the text vertically or with some inclination?


Thanks

Ramon

Warnes, Gregory R wrote:
> 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:
>>
>>
>>>How about this 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=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 confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
> 
> 

-- 
Ramon Alonso-Allende Erhardt			Tel: 91 585 46 76
Protein Design Group		   		fax: 91 585 45 06
CNB/CSIC Campus U. Autonoma. Cantoblanco  Madrid 28049
http://www.pdg.cnb.uam.es/allende/index.html



More information about the R-help mailing list