2013-04-21 165 views
15

我有一個例子,我想使用ggplot來強調序列比對的幾個屬性。我正在使用geom_tile,並希望爲兩個樂譜屬性分配兩組不同顏色的磁貼。我只能看到一個。ggplot2每個審美的多個比例/傳說,重新審視

我意識到每個美學的一個尺度的限制(and the logic behind it),但也許有人有一個想法如何在這樣的情況下破解它,在一個「情節」中有不同的顏色比例是有意義的。

也許與手動添加Grobs,但我不知道從哪裏開始...

額外的問題:由於某種原因,override.aes=list(shape = "A")不工作,任何想法,爲什麼?

又一個:任何方法來按比例縮放文本的大小(或相反的方式)?

library(ggplot2) 
library(grid) 

pd = data.frame(
    letters = strsplit("AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT", "")[[1]], 
    species = rep(c("Human", "Armadillo", "Porcupine"), each=16), 
    x  = rep(1:16, 3), 
    change = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
       0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0, 
       0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0), 
    score1 = c(0,0,0,0,0,0,1,1,2,2,2,3,3,3,4,3, 
       0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0, 
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), 
    score2 = c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0, 
       0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0, 
       0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0) 
) 


ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) + 
    coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) + 
    geom_tile(aes(fill=score1)) + 
    scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) + 
    geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") + 
    scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) + 
    theme(panel.background=element_rect(fill="white", colour="white"), 
     axis.title = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_text(family="mono", size=rel(2)), 
     axis.text.x = element_text(size=rel(0.7)), 
     legend.text = element_text(size=rel(0.7)), 
     legend.key.size = unit(0.7, "lines"), 
     legend.position = "bottom", legend.box = "horizontal") + 
    ggtitle("What about Score2?") 

How to add another layer of tiles with different color scale?

回答

8

我通過結合兩個單獨生成的圖表中的grobs,得到了滿意的結果。我敢肯定,該解決方案可以推廣更好地適應不同GROB指數...

library(ggplot2) 
library(grid) 

pd = data.frame(
    letters = strsplit("AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT", "")[[1]], 
    species = rep(c("Human", "Armadillo", "Porcupine"), each=16), 
    x  = rep(1:16, 3), 
    change = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
       0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0, 
       0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0), 
    score1 = c(0,0,0,0,0,0,1,1,2,2,2,3,3,3,4,3, 
       0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0, 
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), 
    score2 = c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0, 
       0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0, 
       0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0) 
) 


p1=ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) + 
    coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) + 
    geom_tile(aes(fill=score1)) + 
    scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) + 
    geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") + 
    scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) + 
    theme(panel.background=element_rect(fill="white", colour="white"), 
     axis.title = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_text(family="mono", size=rel(2)), 
     axis.text.x = element_text(size=rel(0.7)), 
     legend.text = element_text(size=rel(0.7)), 
     legend.key.size = unit(0.7, "lines"), 
     legend.position = "bottom", legend.box = "horizontal") + 
    ggtitle("Voila, the Score2!") 

p2=ggplot(pd[pd$score2 != 0,], aes(x=x, y=species)) + 
    coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) + 
    geom_tile(aes(fill=score2)) + 
    scale_fill_gradient2("Score 2", limits=c(0,3),low="#1B7837", mid="white", high="#762A83", guide=guide_colorbar(title.position="top")) + 
    geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") + 
    scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) + 
    theme(panel.background=element_rect(fill="white", colour="white"), 
     axis.title = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_text(family="mono", size=rel(2)), 
     axis.text.x = element_text(size=rel(0.7)), 
     legend.text = element_text(size=rel(0.7)), 
     legend.key.size = unit(0.7, "lines"), 
     legend.position = "bottom", legend.box = "horizontal") + 
    ggtitle("What about Score2?") 


p1g=ggplotGrob(p1) 
p2g=ggplotGrob(p2) 

combo.grob = p1g 

combo.grob$grobs[[8]] = cbind(p1g$grobs[[8]][,1:4], 
           p2g$grobs[[8]][,3:5], 
           size="first") 

combo.grob$grobs[[4]] = reorderGrob(
          addGrob(p1g$grobs[[4]], 
            getGrob(p2g$grobs[[4]], 
              "geom_rect.rect", 
              grep=TRUE)), 
          c(1,2,5,3,4)) 
grid.newpage() 
grid.draw(combo.grob) 

Two scales in one plot

4

我會用文字的大小,用於指示score2:

ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) + 
    coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) + 
    geom_tile(aes(fill=score1)) + 
    scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) + 
    geom_text(data=pd, aes(label=letters, size = score2, color=factor(change)), family="mono") + 
    scale_size_continuous(range = c(4, 8)) + 
    scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) + 
    theme(panel.background=element_rect(fill="white", colour="white"), 
     axis.title = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_text(family="mono", size=rel(2)), 
     axis.text.x = element_text(size=rel(0.7)), 
     legend.text = element_text(size=rel(0.7)), 
     legend.key.size = unit(0.7, "lines"), 
     legend.position = "bottom", legend.box = "horizontal") + 
    ggtitle("What about Score2?") 

enter image description here

更新:

這裏是一個快速黑客,我不知道這是否容易檢查,雖然...

library(ggplot2) 
library(grid) 
library(proto) 

GeomTile2 <- proto(ggplot2:::GeomTile, { 
    reparameterise <- function(., df, params) { 
    df <- .$.super$reparameterise(df, params) 
    if (params$ud == "u") 
     transform(df, ymin = y) 
    else 
     transform(df, ymax = (y-ymin)*0.8 + ymin, ymin = (y-ymin)*0.2 + ymin) 
    } 
    draw <- function(..., ud) {.$.super$draw(..., ud)} 
}) 
geom_tile2 <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., ud = "u") { 
    GeomTile2$new(mapping = mapping, data = data, stat = stat, position = position, ..., ud = ud) 
} 

ggplot(pd, aes(x=x, y=species)) + 
    coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) + 
    geom_tile2(aes(fill=score1), ud = "u") + 
    geom_tile2(aes(fill = score2), ud = "d") + 
    scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) + 
    geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") + 
    scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) + 
    theme(panel.background=element_rect(fill="white", colour="white"), 
     axis.title = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_text(family="mono", size=rel(2)), 
     axis.text.x = element_text(size=rel(0.7)), 
     legend.text = element_text(size=rel(0.7)), 
     legend.key.size = unit(0.7, "lines"), 
     legend.position = "bottom", legend.box = "horizontal") + 
    ggtitle("What about Score2?") 

enter image description here

上半部分表示,而下部爲score2 score1。

+0

感謝koshke,我認爲,但它不是一個足夠的視覺線索來代表這個屬性。這些對齊可以是長達一百個或更多的字符,並且可以包含數十種物種,所以字母相當小,因此通常沒有空間來操縱它們的尺寸。而且,有時分數是一個連續變量,並且很難辨別從一個序列到另一個序列的相對數量。恐怕我需要更多的'視覺' – Krizbi 2013-04-22 06:03:14

+0

只是舉一個更真實的例子:[這是一個完整的對齊方式](http://i.imgur.com/wB4prkv.png) – Krizbi 2013-04-22 06:11:04

+0

@Krizbi更新後請發現。 – kohske 2013-04-23 00:54:51