我正在尝试将自定义图例添加到lattice
情节中,但是我无法按照自己的意愿制作。下面是我的代码。我想为一个lattice
绘图创建图例,该绘图看起来像使用创建的绘图,mtext()
但带有使用的符号legend
。如果我可以使男性和女性符号更深或具有不同的颜色,那就太好了。谢谢你。
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)
更新:这里有一个小数据集的数据
这是生成网格图的R代码:
#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')))
我与问题作者进行了面对面的讨论,我认为可以用实现lattice
而不是的图例来满足他的要求,该传说base
仅需要修改auto.key
对的调用参数,xyplot()
使用与上述相同的代码,请尝试对xyplot()
,直接在auto.key
参数中指定图例文本text=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")
:
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(text=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"), 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')))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句