2017-05-03 34 views
2

提供增值文本標題,我有以下的數據幀:如何ggplot基於由整齊data.frame


df <- structure(list(some_score = c(0.159908755191963, 0.191316882518594, 0.505115802144402, 
    0.137543374720433, 0.00611518542460786, 0.17817657028984, 0.184484678282946, 
    0.389765901467282, 0.240839237211809, 0.00673361274812246), crt = c(0.137543374720433, 
    0.137543374720433, 0.137543374720433, 0.137543374720433, 0.137543374720433, 
    0.17817657028984, 0.17817657028984, 0.17817657028984, 0.17817657028984, 
    0.17817657028984), sr = c(0.374378046673855, 0.374378046673855, 0.374378046673855, 
    0.374378046673855, 0.374378046673855, 0.115542340010558, 0.115542340010558, 
    0.115542340010558, 0.115542340010558, 0.115542340010558), sample = structure(c(1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Sample1", "Sample2"), class = "factor"), 
    celltype = c("B", "Mac", "NK", "Neu", "Stro", "B", "Mac", "NK", "Neu", "Stro")), 
    .Names = c("some_score", "crt", "sr", "sample", "celltype"), row.names = c(NA, 
    10L), class = "data.frame") 
df 
#>  some_score  crt  sr sample celltype 
#> 1 0.159908755 0.1375434 0.3743780 Sample1  B 
#> 2 0.191316883 0.1375434 0.3743780 Sample1  Mac 
#> 3 0.505115802 0.1375434 0.3743780 Sample1  NK 
#> 4 0.137543375 0.1375434 0.3743780 Sample1  Neu 
#> 5 0.006115185 0.1375434 0.3743780 Sample1  Stro 
#> 6 0.178176570 0.1781766 0.1155423 Sample2  B 
#> 7 0.184484678 0.1781766 0.1155423 Sample2  Mac 
#> 8 0.389765901 0.1781766 0.1155423 Sample2  NK 
#> 9 0.240839237 0.1781766 0.1155423 Sample2  Neu 
#> 10 0.006733613 0.1781766 0.1155423 Sample2  Stro 

這是繪圖代碼:

tableau10 <- c("#17BECF", "#BCBD22", "#7F7F7F", "#CFECF9", "#8C564B", "#9467BD", "#D62728", "#2CA02C", "#FF7F0E", "#1F77B4") 
ggplot(df, aes(x=celltype,y=some_score, fill=celltype)) + 
    geom_bar(stat="identity") + 
    scale_fill_manual(values=tableau10) + 
    geom_hline(aes(yintercept=crt), colour="red", linetype="dashed") + 
    theme_minimal(base_size=20) + 
    theme(strip.background=element_blank(),strip.text = element_text(size=10)) + 
    theme(legend.title=element_blank()) + 
    theme(axis.text.y=element_text(hjust=1,size=10)) + 
    facet_wrap(~sample) 

哪產生此圖:enter image description here

如圖所示,我想要使用ggtitle,其中包含srcrt列中的值df。我怎樣才能做到這一點?

+3

可以使用'labeller'功能:'facet_wrap(〜樣品+ CRT + SR,貼標機貼標機=(CRT = label_both,SR = label_both,.multi_line = F))' – Jimbou

+0

非常好@Jimbou – Umberto

回答

3

非常基本的解決方案。您可以自己添加CRT(抱歉沒有得到現在這麼多時間)...

ggplot(df, aes(x=celltype,y=some_score, fill=celltype)) + 
    geom_bar(stat="identity") + 
    scale_fill_manual(values=tableau10) + 
    geom_hline(aes(yintercept=crt), colour="red", linetype="dashed") + 
    theme_minimal(base_size=20) + 
    theme(strip.background=element_blank(),strip.text = element_text(size=10)) + 
    theme(legend.title=element_blank()) + 
    theme(axis.text.y=element_text(hjust=1,size=10)) + 
    facet_wrap(~paste(as.character(sample), paste("SR = ",round(df$sr,2)))) 

enter image description here

隨着@Jimbou(貼標機)的思想,結果是非常好的太

facet_wrap(~sample+crt+sr, 
    labeller = labeller(crt=label_both, sr = label_both, .multi_line = F)) 

,你會得到

enter image description here

+0

謝謝。有'ggtitle'做到這一點的方法嗎? – neversaint

+0

我怎樣才能使用'labeller'輪? – neversaint

2

你可以試試:

# function to round the values. As they are characters you have to use a string manipulation 
round_string <- function(string) substr(string, 1, 5) 

# the plot 
ggplot(df, aes(x=celltype,y=some_score, fill=celltype)) + 
    geom_bar(stat="identity") + 
    scale_fill_manual(values=tableau10) + 
    geom_hline(aes(yintercept=crt), colour="red", linetype="dashed") + 
    theme_minimal(base_size=20) + 
    theme(strip.background=element_blank(),strip.text = element_text(size=10)) + 
    theme(legend.title=element_blank()) + 
    theme(axis.text.y=element_text(hjust=1,size=10)) + 
    facet_wrap(~sample + crt + sr,labeller = labeller(crt=as_labeller(round_string, default = label_both), 
                sr=as_labeller(round_string, default = label_both), 
                .multi_line = F)) 

enter image description here