[R] Colour code y-axis labels on a dot plot

markm0705 markm0705 at gmail.com
Thu Sep 15 11:03:01 CEST 2011


Dear R helpers

I would like to be able to colour code the y-axis labels on a complex dot
plot by a variable known as company (of which there are only two).  The code
is below and data attached.

Thanks

MarkM

library("lattice")
library(latticeExtra) # for mergedTrellisLegendGrob()

# set size of the window
windows(height=10, width=7,rescale=c("fixed"))

##read the data to a variable
#----------------------------------------------------------------------------------------

Cal_dat <- read.table("Calibration2.dat",header = TRUE,sep = "\t",)

## set up plotting colours
#----------------------------------------------------------------------------------------
# Colours for the six commodity

col.pat<-c("violet","cyan","green","red","blue","black","yellow") 

# Circles squares and diamond symbols for the year
sym.pat<-c(19,20,21)

##set up the plot key
#----------------------------------------------------------------------------------------
# key for commodities
# defaults
bord.col<-"grey90"
trans.fac<-0.9
sym.siz<-1.8

key1 <-
   draw.key(list(text=list(levels(Cal_dat$Commodity)),
      		title="Ore type",
                 	border =bord.col,
                 	background = bord.col,
			alpha.background=trans.fac,
			just=1,
                 	points=list(pch=22, cex=sym.siz, fill=col.pat,
col="transparent")),
            draw = FALSE)
key2 <-
   draw.key(list(text=list(levels(factor(Cal_dat$Year))),
                 	title="Year",
                 	border = bord.col,
                 	background = bord.col,
			alpha.background=trans.fac,
			just=1,
                 	points = list(pch = c(21, 22, 23), cex=sym.siz,
col="black")),
            draw = FALSE)

mkey <-
   mergedTrellisLegendGrob(list(fun = key2),
                           list(fun = key1),
                           vertical = FALSE
)	

##set some parameters for the plot
#----------------------------------------------------------------------------------------
trellis.par.set(
 	dot.line=list(col = "grey95", lty=1),
	axis.line=list(col = "grey50"),
	axis.text=list(col ="grey50", cex=0.8),
	panel.background=list(col="transparent")
)

## Create the dot plot
#----------------------------------------------------------------------------------------
# some mean values for reference lines first

ave_dat<-aggregate(Cal_dat$Resc_Gt, by = list(Cal_dat$Year),mean)

# create the plot

with(Cal_dat,
    dotplot(reorder(paste(Mine,Company), Resc_Gt) ~ Resc_Gt,
            fill_var = Commodity,
            pch_var = factor(Year),
		xlab_var = factor(Company),
            pch = c(21, 22, 23),
            cex=sym.siz,
            col = "black",
            fill = col.pat,
		alpha=0.6,
            legend = list(inside = list(fun = mkey,corner = c(0.97, 0.06))),
		scales = list(x = list(log = 10)), 
		xscale.components = xscale.components.log10ticks,
            origin = 0,
            type = c("p","a"), 
            main = "Mineral resources",
            xlab= "Total tonnes (billions)",
            panel = function(x, y, ..., subscripts,
                             fill, pch, fill_var, pch_var) {
                pch <- pch[pch_var[subscripts]]
                fill <- fill[fill_var[subscripts]]
		    panel.ablineq(v=log(ave_dat[1,2]),col="grey60", lty=1, rotate=
TRUE,label="2002", at=0.40)
		    panel.ablineq(v=log(ave_dat[2,2]),col="grey60", lty=1, rotate=
TRUE,label="2009", at=0.50)
		    panel.ablineq(v=log(ave_dat[3,2]),col="grey60", lty=1, rotate=
TRUE,label="2010", at=0.60)
                panel.dotplot(x, y, pch = pch, fill = fill, ...)

            })) http://r.789695.n4.nabble.com/file/n3815044/Calibration.dat
Calibration.dat 

--
View this message in context: http://r.789695.n4.nabble.com/Colour-code-y-axis-labels-on-a-dot-plot-tp3815044p3815044.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list