R:使用Lattice

时间:2016-11-20 01:36:50

标签: r legend lattice

我正在研究这个问题here,以便从2x2图的子图中获取一个图例到整个窗口。目标是获取单个图例,然后删除其他图例,只有一个图例就足够了,应该位于整个窗格的右侧。

enter image description here

也许相关

  1. Global legend using grid.arrange (gridExtra) and lattice based plots
  2. 代码

    require(lattice)
    require(gridExtra)
    
    f<-function(x) as.double(as.character(x))   #factors converted to vectors https://stackoverflow.com/a/40680020/54964
    
    data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", 
    "1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("", 
    "0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L, 
    2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("", 
    "0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L, 
    2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L, 
    2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L, 
    7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9", 
    "22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1", 
    "N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus", 
    "Arr/AHB"), class = "data.frame")
    
    data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", 
    "1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("", 
    "0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L, 
    2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L, 
    2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"), 
        N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"), 
        N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"), 
        N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8", 
        "11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0", 
        "44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1", 
    "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus", 
    "Arr/AHB"), class = "data.frame")
    
    ID<-c("Sinus","Arr/AHB")
    
    tl <- "female"
    p1 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
             data=data.female,
             auto.key=list(space='right'), 
             ylim=c(0,50),
         beside=TRUE,
         ylab = "Number of cases", 
         xlab = "Population/Sample",
         main = tl
             )
    tl <- "male"
    p2 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
             data=data.male,
             auto.key=list(space='right'),
             ylim=c(0,50),
         beside=TRUE,
         ylab = "Number of cases", 
         xlab = "Population/Sample",
         main = tl 
             )
    
    # Just repeat two barcharts more to get 2x2 example
    tl <- "female"
    p3 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
             data=data.female,
             auto.key=list(space='right'), 
             ylim=c(0,50),
         beside=TRUE,
         ylab = "Number of cases", 
         xlab = "Population/Sample",
         main = tl
             )
    tl <- "male"
    p4 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
             data=data.male,
             auto.key=list(space='right'),
             ylim=c(0,50),
         beside=TRUE,
         ylab = "Number of cases", 
         xlab = "Population/Sample",
         main = tl)
    
    
    
    grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2,left=("LEFT TITLE"),right=("RIGHT"),bottom=("BOTTOM"), top=("TOP"))
    

    编译生成上面的网格图,但由于图例

    ,以下内容不起作用
    grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2,
            legend=list(space='right',
            text=c("N11.1","N22.1","N33.1","N44.1","N21.1","N31.1","N32.1"),
                        columns=1))
    

    最简单的方法是从一些子图继承图例并使用像auto.key这样的东西,但是我无法使用这样的共同工作,因此尝试使用legend命令创建grop对象。

    图例命令有什么问题,是否有方便的方法将子图的图例继承到整个窗格,以便可以在窗格外部使用auto.key?

    帮助者问题

      
        
    1. 颜色如何从自动glob创建命令auto.key继承到图例?

    2.   
    3. 不是手动编写图例标签(N11.1,N22.1,...),而是如何从子图中方便地获取它?

    4.   

1 个答案:

答案 0 :(得分:0)

我发了一个答案here。我们的想法是避免在绘图中插入密钥,并使用draw.key()在最终grid.arranged plot的任何位置打印定义的键。