Dear R help group I've been working on this plot for a while now and now getting around to the minor adjusments. I would like to be able to put a border and background fill around the legend in this plot. I understand the legend 'bty' should do this have this capablity but not sure how the syntax works in this case ###### initalise 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 #---------------------------------------------------------------------------------------- col.pat<-c("violet","cyan","green","red","blue","black","yellow") sym.pat<-c(19,20,21) ##set up the plot key #---------------------------------------------------------------------------------------- key1 <- draw.key(list(text=list(levels(Cal_dat$Commodity)), title="Ore type", points=list(pch=22, cex=1.3, fill=col.pat, col="black")), draw = FALSE) key2 <- draw.key(list(text=list(levels(factor(Cal_dat$Year))), title="Year", points = list(pch = c(21, 22, 23), cex=1.3, 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=1.2, 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, ...) } http://r.789695.n4.nabble.com/file/n3785003/Calibration2.dat Calibration2.dat -- View this message in context: http://r.789695.n4.nabble.com/Background-fill-and-border-for-a-legend-in-dotplot-tp3785003p3785003.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-Sep-02 12:17 UTC
[R] Background fill and border for a legend in dotplot
Hi: Try this: key1 <- draw.key(list(text=list(levels(Cal_dat$Commodity)), title="Ore type", border = TRUE, background = 'ivory', points=list(pch=22, cex=1.3, fill=col.pat, col="black")), draw = FALSE) key2 <- draw.key(list(text=list(levels(factor(Cal_dat$Year))), title="Year", border = TRUE, background = 'ivory', points = list(pch = c(21, 22, 23), cex=1.3, col="black")), draw = FALSE) mkey <- mergedTrellisLegendGrob(list(fun = key2), list(fun = key1), vertical = TRUE ) Now rerun your dotplot; from the result I got, you may need to do some positional tweaking and may well want to change the background color of the legend to something else.. HTH, Dennis On Thu, Sep 1, 2011 at 4:47 PM, markm0705 <markm0705 at gmail.com> wrote:> Dear R help group > > I've been working on this plot for a while now and now getting around to the > minor adjusments. ?I would like to be able to put a border and background > fill around the legend in this plot. > > I understand the legend 'bty' should do this have this capablity but not > sure how the syntax works in this case > > ###### initalise > 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 > #---------------------------------------------------------------------------------------- > col.pat<-c("violet","cyan","green","red","blue","black","yellow") > sym.pat<-c(19,20,21) > > ##set up the plot key > #---------------------------------------------------------------------------------------- > key1 <- > ? draw.key(list(text=list(levels(Cal_dat$Commodity)), > ? ? ? ? ? ? ? ? title="Ore type", > ? ? ? ? ? ? ? ? points=list(pch=22, cex=1.3, fill=col.pat, col="black")), > ? ? ? ? ? ?draw = FALSE) > key2 <- > ? draw.key(list(text=list(levels(factor(Cal_dat$Year))), > ? ? ? ? ? ? ? ? title="Year", > ? ? ? ? ? ? ? ? points = list(pch = c(21, 22, 23), cex=1.3, 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=1.2, > ? ? ? ? ? ?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, ...) > ? ? ? ? ? ?} > > http://r.789695.n4.nabble.com/file/n3785003/Calibration2.dat > Calibration2.dat > > -- > View this message in context: http://r.789695.n4.nabble.com/Background-fill-and-border-for-a-legend-in-dotplot-tp3785003p3785003.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >