2014-02-17 32 views
2

我想用每個列的Fill_factor的計數值標記該特定列,但我不能每列獲得多個標籤。標籤列與計數填充值在ggplot2

mydata.df <- data.frame(Fill_factor = c("Fill1", "Fill3", "Fill1", "FILL2","Fill1","Fill1","FILL2", "FILL2","FILL3"), Count = c("AA", "BB", "AA", "AA","CC","BB","AA", "BB", "CC")) 
q <- qplot(Count, data= mydata.df, fill= Fill_factor, geom="bar", position="fill") # base sub by gene names 
q + theme(axis.text.x=element_text(angle=-90, hjust = 1), text = element_text(size=10)) 

table(mydata.df$Count,mydata.df$Fill_factor) 

Fill1 FILL2 Fill3 FILL3 
AA  2  2  0  0 
BB  1  1  1  0 
CC  1  0  0  1 

我使用有使用也ddply的功能,但我不知道如何得到正確的位置

mydata.df.count <- ddply(mydata.df, .(Fill_factor, Count), nrow) 
colnames(mydata.df.count) <- c("Fill_factor", "Count", "Frequency") 

mydata.df.count <- ddply(mydata.df.count, .(Count), transform, pos = cumsum(Frequency)/5) # incorrect position 

p = ggplot(mydata.df.count, aes(x = Count, y = Frequency), stat="identity") + geom_bar(aes(fill = Fill_factor), position="fill") + geom_text(aes(label = Frequency, y = pos), size = 5) 
p 

我怎樣才能把文字標籤在合適的位置(每列多個文本)位置=「填充」參數?我怎麼能排序的最高填充因子==「fill1」計數的計數水平?

謝謝!

+0

有對SO這個話題不少不錯的答案:http://stackoverflow.com/search?q=[ggplot2]+標籤+堆疊+酒吧+ geom_bar – Henrik

+0

下面是如何標籤堆疊barplot http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2 – lukeA

+0

我不知道但我總是缺少y和ymax的參數,我不知道如何獲得位置參數和y值 – user3300849

回答

2

您必須將累積頻率與總頻率相關聯,然後垂直調整標籤。

代碼:

mydata.df.count <- ddply(mydata.df.count, .(Count), transform, pos = cumsum(Frequency)/sum(Frequency)) 

ggplot(mydata.df.count, aes(x = Count, y = Frequency, fill = Fill_factor)) + 
    geom_bar(stat="identity", position="fill") + 
    geom_text(aes(y = pos, label = Frequency), vjust = 3) 

結果: enter image description here