2013-02-18 211 views
8

我想繪製使用ggplot在R中堆積的條形圖。我還希望在每件酒吧中包含百分比。我試圖按照文章1,2,3,但值不完全在其各自的塊。我的數據是dropbox中的文件。R堆積的條形圖繪製geom_text

我的代碼如下:

f<-read.table("Input.txt", sep="\t", header=TRUE) 

ggplot(data=f, aes(x=Form, y=Percentage, fill=Position)) + 
    geom_bar(stat="identity", colour="black") + 
    geom_text(position="stack", aes(x=Form, y=Percentage, ymax=Percentage, label=Percentage, hjust=0.5)) + 
    facet_grid(Sample_name ~ Sample_type, scales="free", space="free") + 
    opts(title = "Input_profile", 
     axis.text.x = theme_text(angle = 90, hjust = 1, size = 8, colour = "grey50"), 
     plot.title = theme_text(face="bold", size=11), 
     axis.title.x = theme_text(face="bold", size=9), 
     axis.title.y = theme_text(face="bold", size=9, angle=90), 
     panel.grid.major = theme_blank(), 
     panel.grid.minor = theme_blank()) + 
    scale_fill_hue(c=45, l=80) 

ggsave("Output.pdf") 

輸出是 -

enter image description here

任何幫助不勝感激。 謝謝你的幫助和時間!

+5

想想你給到'geom_text' y值。它是每個酒吧段的_height_。您需要進行一些算術運算來計算每個線段的中點(或頂點或其他),將其作爲單獨的變量添加,並將其用作'geom_text'中的y變量。函數'cumsum'可能會有所幫助。 – joran 2013-02-18 17:35:51

+1

這樣的東西會計算標籤在每條線段中點的y位置:'f < - ddply(f,。(Form,Sample_name,Sample_type),transform,pos =(cumsum(Percentage) - 0.5 * Percentage )''(需要plyr),那麼'geom_text'語句應該是:'geom_text(aes(x = Form,y = pos,label = Percentage))' – 2013-02-18 21:02:00

回答

9

我想你使用的是舊版本的ggplot2。因爲修改GGPLOT2 v 0.9.3你的代碼,我得到這個:

p <- ggplot(data = df, aes(x = Form, y = Percentage, fill = Position)) 
p <- p + geom_bar(stat = "identity", colour = "black") 
p <- p + geom_text(position = "stack", aes(x = Form, y = Percentage, ymax = Percentage, label = Percentage, hjust = 0.5)) 
p <- p + facet_grid(Sample_name ~ Sample_type, scales="free", space="free") 
p <- p + theme(plot.title = element_text("Input_profile"), 
     axis.text.x = element_text(angle = 90, hjust = 1, size = 8, colour = "grey50"), 
     plot.title = element_text(face="bold", size=11), 
     axis.title.x = element_text(face="bold", size=9), 
     axis.title.y = element_text(face="bold", size=9, angle=90), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank()) 
p <- p + scale_fill_hue(c=45, l=80) 
p 

ggplot2_text_placement

您看到文本對象通常位於正常。有些地方酒吧太短,所以數字重疊。您也可以玩size參數。

要糾正這一點,你可以做這樣的事情來自己加上數字。

df <- ddply(df, .(Form, Sample_type, Sample_name), transform, 
     cum.perc = Reduce('+', list(Percentage/2,cumsum(c(0,head(Percentage,-1)))))) 

p <- ggplot(data = df, aes(x = Form, y = Percentage, fill = Position)) 
p <- p + geom_bar(stat = "identity", colour = "black") 
p <- p + geom_text(aes(x = Form, y = cum.perc, ymax = cum.perc, label = Percentage, hjust = 0.5), size=2.7) 
p <- p + facet_grid(Sample_name ~ Sample_type, scales="free", space="free") 
p <- p + theme(plot.title = element_text("Input_profile"), 
     axis.text.x = element_text(angle = 90, hjust = 1, size = 8, colour = "grey50"), 
     plot.title = element_text(face="bold", size=11), 
     axis.title.x = element_text(face="bold", size=9), 
     axis.title.y = element_text(face="bold", size=9, angle=90), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank()) 
p <- p + scale_fill_hue(c=45, l=80) 
p 

這給:

ggplot2_facet_text_final

5

這裏使用萊迪思barchart的解決方案。

enter image description here

library(latticeExtra) 
barchart(Percentage~Form|Sample_type*Sample_name,data=dat, 
     groups =Position,stack=T, 
     panel=function(...){ 
      panel.barchart(...) 
      ll <- list(...) 
      keep <- !is.na(ll$groups[ll$subscripts]) 
      x <- as.numeric(ll$x[keep]) 
      y <- as.numeric(ll$y[keep]) 
      groups <- as.numeric(factor(ll$groups)[ll$subscripts[keep]]) 
      for (i in unique(x)) { 
       ok <- x == i 
       ord <- sort.list(groups[ok]) 
       pos <- y[ok][ord] > 0 
       nok <- sum(pos, na.rm = TRUE) 
       h <- y[ok][ord][pos] 
       panel.text(x = rep(i, nok),y = cumsum(h)-0.5*h, 
          label = h,cex=1.5) 
      } 
     }, 
     auto.key = list(columns = 5), 
     par.settings = ggplot2like(n = 5), 
     lattice.options = ggplot2like.opts())