[R] Mutiple vertical reference lines with a dotplot

markm0705 markm0705 at gmail.com
Sat Sep 10 11:28:54 CEST 2011


Dear R helpers

I would like to plot vertical reference lines for yearly averages the
variable being plotted.  In this case there are three years 2002, 2009 and
2010 for a variable Resc_Gt.  

I've attempted to get the 2002 mean plotting (2.27) using the 'abline'
command but no joy.

Ideally I would like to be able to compute and plot the means of the three
years automatically using a function rather than computing and plotting
manually.  The next challange will be to get these mean lines in the legend
as well!

Thanks in advance for any pointers

MarkM

(current code below and data attached)

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

##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 years
sym.pat<-c(19,20,21)

##set up the plot key
#----------------------------------------------------------------------------------------
# key for commodities
# defaults
bord.col<-"grey90"
trans.fac<-0.5
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="black")),
            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 = TRUE
)	

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

## Create the dot plot
#----------------------------------------------------------------------------------------
with(Cal_dat,
    dotplot(reorder(paste(Mine,Company), Resc_Gt) ~ Resc_Gt,
            fill_var = Commodity,
            pch_var = factor(Year),
            cex=sym.siz,
            pch = c(21, 22, 23),
            col = "black",
            fill = col.pat,
            aspect = 2.0,
		alpha=0.6,
            legend = list(inside = list(fun = mkey,corner = c(0.95, 0.01))),
		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.dotplot(x, y, pch = pch, fill = fill, ...)
            }))

            panel = function(x, y, ..., subscripts,
                             fill, pch, fill_var, pch_var) {
                pch <- pch[pch_var[subscripts]]
                fill <- fill[fill_var[subscripts]]
                panel.dotplot(x, y, pch = pch, fill = fill,...)
		    panel.abline(v=2.27,col="red")
            }
http://r.789695.n4.nabble.com/file/n3803562/Calibration2.dat
Calibration2.dat 

--
View this message in context: http://r.789695.n4.nabble.com/Mutiple-vertical-reference-lines-with-a-dotplot-tp3803562p3803562.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list