我正在尝试将自定义图例添加到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)
Here is my current plot, I want to change the legend to include male and female symbols
更新: 这是一个小数据集data
以下是生成Trellis图的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')))
答案 0 :(得分:1)
我与问题作者进行了一些面对面的讨论,我认为,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')))
答案 1 :(得分:0)
MrFlick的问题尚未得到答复,所以我将继续假设你真的想要一个基础图形解决方案。如果par参数“xpd”设置为TRUE,则在par调用或文本调用本身中,text函数可以绘制在用户坐标框架之外。首次在hte plot窗口中单击一次以更改焦点后,定位器函数将返回x-y值,然后再次在您想要此额外图例/注释的中心处返回x:y值:
text(locator(1), "Antennae:
\u2640 12h 4d 3h 24h 48h 72h \u2642 4d
Head+Thorax:
\u2640 12h 4d 3h 24h 48h 72h \u2642 4d
", xpd=TRUE)
目前请求也是关于位置的(这就是为什么我给了locator()
选项。也可以使用Hershey字体使用它的星座符号,其中男性是“火星”,女性是“维纳斯”