自定义图例R图(晶格)

章鱼

我正在尝试将自定义图例添加到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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SSRS-自定义饼图图例

来自分类Dev

自定义素面图

来自分类Dev

自定义图例R图(晶格)

来自分类Dev

rCharts中的自定义图例(高图)

来自分类Dev

自定义晶格功能:跨面板获得正确的颜色

来自分类Dev

添加与图没有任何关系的自定义图例

来自分类Dev

R:晶格函数中的图例

来自分类Dev

当使用ggplot覆盖另一个图时,如何制作自定义图例?

来自分类Dev

带有R传单的自定义图例

来自分类Dev

如何自定义BoxAndWhisker图的图例

来自分类Dev

Morris.js条形图的自定义悬停图例

来自分类Dev

高图:从导出中删除自定义图例工具提示

来自分类Dev

Google条形图-自定义图例文本,其总值和表格图的按列总计

来自分类Dev

自定义蜂巢图

来自分类Dev

如何在R中添加自定义图例?

来自分类Dev

R:将自定义图例添加到ggplot

来自分类Dev

R:多层ggplot的自定义图例

来自分类Dev

饼图自定义图例

来自分类Dev

rCharts中的自定义图例(高图)

来自分类Dev

自定义晶格功能:跨面板获得正确的颜色

来自分类Dev

自定义图例标签

来自分类Dev

matplotlib(python)-在没有pyplot的情况下为多个图创建单个自定义图例

来自分类Dev

自定义R箱形图

来自分类Dev

R ggplot2:将自定义文本添加到图例和热图侧面的值计数

来自分类Dev

自定义图例饼图到标签 div

来自分类Dev

使用 ggplot 的气泡图自定义图例

来自分类Dev

条形图图例中变量的自定义排序

来自分类Dev

ChartJS:自定义图例不显示多饼图的标签

来自分类Dev

MPAndroidChart 饼图自定义图例显示不正确