2013-04-21 11 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

मैं दो अलग-अलग जेनरेट किए गए भूखंडों से ग्रब्स को जोड़कर संतोषजनक परिणाम प्राप्त करने में कामयाब रहा। मुझे यकीन है कि समाधान अलग 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

UPDATED:

यहां एक त्वरित हैक है, मैं कर रहा हूँ सुनिश्चित नहीं है कि यह दृष्टिहीन निरीक्षण करना आसान है हालांकि ...

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

+0

बस एक और वास्तविक उदाहरण देने के लिए: [यहां एक पूर्ण संरेखण है] (http://i.imgur.com/wB4prkv.png) – Krizbi

+0

@ Krizbi अपडेट किया गया कृपया अपडेट करें । – kohske