2017-07-29 58 views
10

是否可以將joyplot作爲面板添加到包含ggtree的圖中,如these examples所示?喜悅圖的例子是hereggjoy facet with ggtree

我意識到我可以手動將joyplot的物種標籤按照樹尖標籤的順序放置,但我正在尋找自動解決方案。我想自動將joyplot行與樹的提示相關聯,類似於boxplot數據如何與提示標籤相關聯。

我認爲Guangchuang瑜在上面的鏈接示例提供合適的數據:

require(ggtree) 
require(ggstance) 

# generate tree 
tr <- rtree(30) 

# create simple ggtree object with tip labels 
p <- ggtree(tr) + geom_tiplab(offset = 0.02) 

# Generate categorical data for each "species" 
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE)) 

#Plot the categorical data as colored points on the tree tips 
p1 <- p %<+% d1 + geom_tippoint(aes(color=location)) 

# Generate distribution of points for each species 
d4 = data.frame(id=rep(tr$tip.label, each=20), 
      val=as.vector(sapply(1:30, function(i) 
          rnorm(20, mean=i))) 
      )    

# Create panel with boxplot of the d4 data 
p4 <- facet_plot(p1, panel="Boxplot", data=d4, geom_boxploth, 
     mapping = aes(x=val, group=label, color=location))   
plot(p4) 

這將產生下面的情節: demo ggtree plot

是否有可能到位箱線圖的創建joyplot?

這裏是演示數據集D4的上方快速joyplot代碼:

require(ggjoy) 

ggplot(d4, aes(x = val, y = id)) + 
geom_joy(scale = 2, rel_min_height=0.03) + 
scale_y_discrete(expand = c(0.01, 0)) + theme_joy() 

結果是: demo joyplot

我是新來GGPLOT2,ggtree和ggjoy所以我完全是在如何甚至開始這樣做的損失。

+0

你能提供一些合適的樣本數據一起工作呢?粘貼到你的問題'dput(data_sample)' – eipi10

+0

@ eipi10的輸出我已經添加了一些由ggtree包的創建者編寫的演示代碼。我不知道'dput(data_sample)'指的是什麼' – LCM

回答

11

注意:截至2017年9月14日,ggjoy package has been deprecated。相反,請使用ggridges package。對於下面的代碼與ggridges一起使用,請使用geom_density_ridges而不是geom_joy


看起來你可以geom_joyfacet_plot只需更換geom_boxplot

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
      mapping = aes(x=val, group=label, fill=location), colour="grey50", lwd=0.3) 

enter image description here

如果你是新的作者,以GGPLOT2,the visualization chapter of Data Science with R(一個開源的書ggplot2)應該有助於學習基礎知識。

ggjoyggtree擴展了ggplot2的功能。當這樣的擴展很好地完成時,「顯而易見的」事情(就通常的ggplot「圖形語法」而言)經常起作用,因爲擴展包是以一種試圖忠實於基本ggplot2方法的方式編寫的。

在這裏,我的第一個想法是用geom_joy代替geom_boxplot,結果證明完成了這項工作。每個geom只是將數據可視化的一種不同方式,在這種情況下,框圖對比密度圖。但是情節的所有其他「結構」都保持不變,所以你可以改變幾何,並得到一個遵循相同的軸順序,顏色映射等的新的情節。一旦你獲得了一些經驗,這將更有意義。 ggplot2圖形文法。

下面是左手情節略有不同的標記方法:

p1 = ggtree(tr) %<+% d1 + 
    geom_tippoint(aes(color=location), size=6) + 
    geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3.2, fontface="bold") 

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
      mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3) 

enter image description here

UPDATE:這是在響應您的意見,詢問如何獲得相同的自定義顏色都刻面板。這裏的代碼,這樣做與你的問題示例數據:

p1 = ggtree(tr) %<+% d1 + 
    geom_tippoint(aes(color=location), size=5) + 
    geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3, fontface="bold") + 
    scale_colour_manual(values = c("grey", "red3", "blue")) + 
    scale_fill_manual(values = c("grey", "red3", "blue")) 

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
      mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3) 

enter image description here

+0

非常感謝!我該如何設置一個自定義色階並使其延續到joyplot?我有一個包含geom_tippoint()的二進制類的樹;我可以使用'+ scale_colour_manual(values = c(「no」=「gray」,「yes」=「red3」))來選擇顏色,但我無法弄清楚如何使顏色方案延伸到joyplot 。如果我使用scale_colour_manual(),則會出現「顏色比例」錯誤,併爲'顏色'添加另一個比例尺,它將替換現有比例尺。「但joyplot保持默認顏色。 – LCM

+1

將'scale_fill_manual(values = c(「no」=「gray」,「yes」=「red3」))'添加到初始繪圖,並在您執行'facet_plot'時執行。 'geom_joy'使用'fill'美學,所以你需要用'scale_fill_manual'設置填充顏色,而不是'scale_colour_manual'。只需使用'colour'和'fill'的相同顏色,並在繪製小平面時匹配。 「尺度」(顏色比例,形狀比例,軸線範圍和中斷等)是ggplot2中體現的「圖形語法」的另一個方面。 – eipi10

+0

當我這樣做時,我得到喜歡的顏色(灰色和紅色)的喜悅,但不是樹尖。我現在沒有一個可重複的例子,但是可以從語法中知道:'baseplot < - p%<+%d1 + geom_tippoint(aes(color = factor(HybridizesLax),cex = 1))+ scale_fill_manual (values = c(「no」=「gray」,「yes」=「red3」))+ theme(legend.position =「left」)'和'tree_joyplot < - facet_plot(baseplot,panel =「年平均氣溫C)「,data = amt_data,geom_joy, mapping = aes(x = amt,group = label,fill = factor(HybridizesLax)),color =」grey50「,lwd = 0.3)' – LCM