I am trying to add a custom legend to a lattice
plot but I can't make as I want. Below is my code. I want to make a legend for a lattice
plot that looks like the one created using mtext()
but with the symbols I used using legend
. It would be great if I could make the male and female symbols darker or with different colors.
Thank you.
plot.new()
legend("bottom", legend = c("\u2640 12h Ant", "\u2640 4d Ant", "\u2640 3h Ant",
"\u2640 24h Ant", "\u2640 48h Ant", "\u2640 72h Ant", "\u2640 4d HT",
"\u2640 3h HT", "\u2640 24h HT", "\u2640 48h HT", "\u2640 72h HT",
"\u2642 4d Ant", "\u2642 4d HT"),
bty = "n",
xjust = 0,
ncol = 7,
lwd = 2, cex = 0.8,
col = c("red","blue","green","pink","maroon4",
"cyan3","blue","green","pink","maroon4","cyan3", "blue", "blue"),
lty = c(NA, NA, NA),
pch = c(15,15,15,15,15,15,19,19,19,19,19,17,2))
#I want to make one legend like this, but mtext cannot add symbols :(
mtext("Antennae:
\u2640 12h 4d 3h 24h 48h 72h \u2642 4d
Head+Thorax:
\u2640 12h 4d 3h 24h 48h 72h \u2642 4d
", side = 1, padj=1, adj=0, line=FALSE, outer=FALSE)
Here is my current plot, I want to change the legend to include male and female symbols
Update: Here a small data set data
Here is the R code to generate the Trellis graph:
#Command to load packages, not all needed
library("RColorBrewer")
library("gplots")
library("plyr")
library("lattice")
library("Hmisc")
library("latticeExtra")
library("scatterplot3d")
library("plotrix")
library("scales")
#Command clears the console
rm(list=ls())
#Command sets the working directory
setwd("/set-your-path-here")
miRs<-read.table("stackoverflow.csv", header=TRUE, sep=",")
attach(miRs)
head(miRs)
miRs.st<-stack(miRs[,2:14])
head(miRs.st)
miRs.st[,"miRs"]<-miRs[,1]
head(miRs.st)
summary(miRs.st$ind)
miRs.st$ind<-factor(miRs.st$ind, levels=c("Fem12hAnt", "Fem4dAnt", "Fem3hAnt",
"Fem24hAnt", "Fem48hAnt", "Fem72hAnt", "Fem4dHT",
"Fem3hHT", "Fem24hHT", "Fem48hHT", "Fem72hHT",
"Male4dAnt", "Male4dHT"))
#create list of symbols to use in the legend
supsym <- trellis.par.get("superpose.symbol")
supsym$col <- c("red","blue","green","pink","maroon4","cyan3","blue","green","pink","maroon4","cyan3", "blue", "blue")
supsym$fill <- c("red","blue","green","pink","maroon4","cyan3","blue","green","pink","maroon4","cyan3", "blue", "blue")
supsym$pch <- c(15,15,15,15,15,15,19,19,19,19,19,17,2)
supsym$cex <- c(.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5) # otherwise symbol 15 appears a bit larger
trellis.par.set("superpose.symbol",supsym)
#set log scales in the axis
options(scipen=10)
options(digits=10)
#set background color to each gene, can change the color of those genes of interest
bgColors <- c("gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"red", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44",
"gray44", "gray80", "gray44", "gray80", "gray44")
#color of the text in the plot
txtColors <- c("white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white",
"white", "black", "white", "black", "white")
# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
panel.rect(0, 0, 1, 1,
col = bgColors[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = txtColors[which.panel])
}
#plot the trellis graph
print(xyplot(values~ind|miRs,groups=ind,data=miRs.st,
layout=c(5,5),as.table=TRUE,
type="p",
par.strip.text=list(custBgCol=bgColors,
custTxtCol=txtColors),
strip=myStripStyle,
auto.key=list(space="bottom",columns=5, pch=8, cex=.8),
relation="same",
scales = list(x = list(draw = FALSE), y = list(log = 10)),
yscale.components = yscale.components.log10ticks,
main="miRs Expression",
xlab="",
ylab=expression('Number of Reads')))