代码之家  ›  专栏  ›  技术社区  ›  degopwn

自定义图例R绘图(晶格)

  •  1
  • degopwn  · 技术社区  · 11 年前

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

    以下是生成网格图的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')))
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   rebeccmeister    11 年前

    我与问题作者进行了一些亲自讨论,我认为,在 lattice 而不是 base ,这只需要修改 auto.key 调用的参数 xyplot() 使用与上面相同的代码,尝试调用 xyplot() ,直接在 自动键 参数: 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')))
    
        2
  •  0
  •   Community Mohan Dere    5 年前

    Flick先生的问题尚未得到解答,因此我将继续假设您实际上想要一个基本的图形解决方案。如果par参数“xpd”设置为TRUE,无论是在par调用中还是在文本调用本身中,文本函数都可以在用户坐标系外绘图。定位器函数在第一次单击绘图窗口以更改焦点,然后再次在需要此额外图例/注释的中心位置,返回一个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)
    

    enter image description here

    目前,请求过于涉及位置(这就是为什么我给出了 locator() 选项也可以使用Hershey字体,使用它的占星符号,其中男性是“火星”,女性是“金星”

    推荐文章