2014-02-12 59 views
3

我對R很陌生,我有以下問題。使用不同變量的數據在條形圖中繪製百分比

我有這樣一個data.frame:

mydata.df <- data.frame(ID.name = c("1000", "1000", "1000", "1001","1001",1001,"1002","1002"), 
         project_name = c("project1", "project1", "project1", "project1","project1","project1","project2", "project2"), 
         Canonical_Hugo_Symbol = c("gene1", "gene3", "gene1", "gene1","gene3","gene4","gene1", "gene2")) 

,我試圖繪製x上的不同的基因,佔的事實,一些基因只是在一個project_set和其他Y上的百分比在另一個。

我曾嘗試用:

q <- qplot(Canonical_Hugo_Symbol, data= mydata.df, geom="bar", fill= project_name, y = (..count..)/sum(..count..)) 
q + theme(axis.text.x=element_text(angle=-90, hjust = 1)) + stat_bin(geom = "text", aes(label = paste(round((..count..)/sum(..count..)*100), "%")), vjust = 5) 

,但作爲一個例子基因2只是存在於項目2和應具有的比例爲100%,因爲是存在於所有(之一)的病人。

非常感謝!

我只是想編輯的data.frame,使其更加「真實」

dput(subset_mydata[c(1:10,1065:1077),c(3,9,20)]) 
structure(list(ID.name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 140L, 140L, 140L, 140L, 141L, 141L, 141L, 141L, 
141L, 141L, 142L, 142L, 142L), .Label = c("", "1075", "1104", 
"1108", "1120", "1121", "1137", "1258", "1264", "1280", "1286", 
"1310", "1317", "1338", "1392", "1401", "1435", "1477", "1480", 
"1494", "1519", "1574", "1588", "1595", "1607", "1611", "1644", 
"1645", "1651", "1653", "1654", "1673", "1687", "1702", "1714", 
"1740", "1776", "1781", "1812", "1835", "1838", "1857", "1874", 
"1890", "1899", "1911", "1933", "1936", "1999", "2006", "2046", 
"2063", "2079", "2081", "2088", "2116", "2135", "2144", "2147", 
"2155", "2166", "2167", "2176", "2183", "2200", "2209", "2223", 
"2253", "2256", "2442", "2444", "2453", "2456", "2462", "2467", 
"2472", "2482", "2497", "2504", "2507", "2513", "2518", "2523", 
"2567", "2568", "2576", "2578", "2598", "2600", "2619", "2623", 
"2625", "2632", "2636", "2646", "2652", "2659", "2660", "2676", 
"2680", "2682", "2705", "2711", "2756", "2765", "2772", "2793", 
"2803", "2854", "2856", "2882", "2912", "2916", "2919", "3058", 
"3063", "3114", "3116", "3117", "3125", "3132", "3140", "3145", 
"3175", "3181", "3248", "3383", "3431", "3436", "3442", "3472", 
"3576", "3639", "4093", "FL1_01215", "FL10_01501", "FL12_01593", 
"FL13_01598", "FL16_01738", "FL17_01752", "FL18_01763", "FL19_01881", 
"FL2_01222", "FL22_02025", "FL23_02032", "FL24_02085", "FL25_02175", 
"FL26_02242", "FL28_02459", "FL3_", "FL30_02558", "FL35_02726", 
"FL37_02808", "FL41_02865", "FL43_02926", "FL44_02994", "FL45_03018", 
"FL47_03119", "FL48_03128", "FL55_03303", "FL62_03406", "FL64_03418", 
"FL65_03421", "FL69_03484", "FL7_01306", "FL70_03517", "FL71_03534", 
"FL76_03644", "FL77_03651", "FL8_01425"), class = "factor"), 
project_name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L 
), .Label = c("", "FL_Ontogeny", "Hemoseq1"), class = "factor"), 
Canonical_Hugo_Symbol = structure(c(174L, 158L, 113L, 113L, 
97L, 175L, 14L, 9L, 35L, 65L, 14L, 14L, 14L, 111L, 123L, 
113L, 113L, 160L, 50L, 50L, 25L, 25L, 131L), .Label = c("", 
"ABCA7", "ACTA2", "ACTB", "ALMS1", "ARID1A", "ARID1B", "ARID2", 
"ARID3A", "ASXL1", "ATM", "B2M", "BCAS3", "BCL2", "BCL7A", 
"BCL9", "BCORL1", "BCR", "BIRC3", "BLNK", "BRAF", "BRD2", 
"BRWD3", "BTG1", "BTG2", "BTG3", "CA10", "CARD11", "CCDC80", 
"CCND1", "CCND2", "CCND3", "CD40", "CD58", "CD79A", "CD79B", 
"CDH23", "CDK4", "CDK6", "CDKN2A", "CDKN2B", "CELSR2", "CHD1", 
"CHD2", "CHRM3", "CIITA", "CNOT1", "COL4A2", "COL6A2", "CREBBP", 
"CRLF2", "CTSS", "CUL9", "CXCR4", "DDX3X", "DGKG", "DIRAS3", 
"DLEU2", "DMD", "DNAH9", "DNM2", "DNMT3A", "DST", "DTX1", 
"EP300", "EPHA6", "EPHA7", "EPRS", "ETS1", "ETV6", "EZH2", 
"FAM135B", "FAM155A", "FAM18A", "FAS", "FAT2", "FBXO11", 
"FCGR2A", "FCGR3A", "FOXO1", "GNA13", "GNAS", "GNB1", "GNB2", 
"GOPC", "HCK", "HEATR1", "HUWE1", "ID3", "IDH1", "IDH2", 
"IKBKE", "IKZF1", "IKZF2", "IKZF3", "IRF4", "IRF8", "JAK1", 
"KAT2A", "KAT2B", "KAT5", "KDM6A", "KIF20B", "KLHL6", "LRRC16A", 
"MALT1", "MAP3K14", "MAP4K1", "MCL1", "MDM2", "MEF2B", "MLL", 
"MLL2", "MPDZ", "MSH6", "MTAP", "MUC16", "MUM1", "MYC", "MYD88", 
"NBAS", "NF1", "NFKB2", "NOTCH1", "NOTCH2", "NPM1", "NR3C1", 
"OSBP2", "P2RY8", "PASD1", "PAX5", "PCLO", "PDGFC", "PDGFRA", 
"PDGFRB", "PHF6", "PIK3CA", "PIK3CD", "PIK3R1", "PIM1", "POSTN", 
"PRKDC", "PTEN", "RASSF8", "RB1", "RELN", "RET", "RFTN1", 
"RFX7", "RHOA", "RIPK3", "RNF213", "ROS1", "SAMD9", "SBF1", 
"SETD2", "SF3B1", "SGK1", "SIN3A", "SLITRK6", "SMARCA2", 
"SMARCA4", "SMARCB1", "SOCS1", "SOX5", "STAT3", "STAT6", 
"SWAP70", "SYK", "TBL1XR1", "TET2", "TMEM30A", "TNFAIP3", 
"TNFRSF14", "TP53", "TRAF2", "TRAF3", "TRAF6", "TTC27", "TYK2", 
"U2AF1", "UBR5", "UGGT1", "ULK4", "UNC13B", "UNC5C", "UNC5D", 
"USP6", "VPS13A", "XPO1", "ZMAT4", "ZNF608", "ZNF708", "ZRSR2" 
), class = "factor")), .Names = c("ID.name", "project_name", 
"Canonical_Hugo_Symbol"), row.names = c(1L, 49L, 94L, 96L, 111L, 
115L, 125L, 127L, 134L, 138L, 18794L, 18796L, 18797L, 18798L, 
18800L, 18801L, 18802L, 18803L, 18805L, 18806L, 18809L, 18810L, 
18814L), class = "data.frame") 
+0

我完全被你的命名困惑。什麼是病人? – ziggystar

+0

ID.name是患者 – user3300849

+1

目前文本標籤分別是:12%,38%,12%,25%,12%。請說明您希望他們是什麼:從您的問題,它不是很清楚所需的輸出是什麼。 – tonytonov

回答

2

你沒有得到「100%」爲基因2,因爲你的百分比計算是將每個小組的意見(按基因類型計劃)所有觀測值的總和。

如果我正確理解了你,你想要每個基因類型的每個項目的百分比。

#added two additional values to gene4. 
mydata.df <- data.frame(ID.name = c("1000", "1000", "1000", "1001","1001",1001,"1002","1002","1000","1003"), 
        project_name = c("project1", "project1", "project1", "project1","project1","project1","project2", "project2","project2","project2"), 
        Canonical_Hugo_Symbol = c("gene1", "gene3", "gene1", "gene1","gene3","gene4","gene1", "gene2","gene4","gene4")) 

#use aggregate function to summarize data per ID.name: 

#at gene level - and 
table1 = aggregate(mydata.df$ID.name,by=list(Canonical_Hugo_Symbol=mydata.df$Canonical_Hugo_Symbol), length) #number of patients per type of gene 
#at gene/project level 
table2 = aggregate(mydata.df$ID.name,by=list(Canonical_Hugo_Symbol=mydata.df$Canonical_Hugo_Symbol,project_name=mydata.df$project_name), length) #number of patients per type of gene/project 

temp=merge(table1,table2,by=1) #merge table 1 and table 2 - this will be useful to calculate percentages of patients inside each gene and project 
names(temp)[c(2,4)] = c("Total_gene","Total_gene_project") #change columns name on the df 

percentage = temp$Total_gene_project/temp$Total_gene*100 #calculate percentage 
percentage = round(percentage,0) 

temp=data.frame(temp,pctg=percentage) #create new df as input to ggplot 

pos = ifelse(temp$project_name=="project1",temp$Total_gene_project,temp$Total_gene) #these values will guide the position of labels "percentages" 

df = data.frame(temp,pos=pos) 

require(ggplot2) 

ggplot(data=df,aes(x=Canonical_Hugo_Symbol,y=Total_gene_project,fill=project_name)) + 
    geom_bar(stat="identity") + 
    xlab("Type of Gene") + ylab("Number of Patients") + 
    theme_bw() + 
    theme(axis.text.x=element_text(angle=-90, hjust = 1)) + 
    geom_text(aes(y=pos,label=paste(pctg, "%")),vjust=2) 

enter image description here

更新 - 該代碼也能與您的真實數據。

enter image description here

在上面的代碼中,假設數據幀的名字是mydata.df,只要設置與正確的名稱的對象pos爲「項目1」(例如:「FL_Ontogeny」)。

相關問題