11

我正在尝试使用 ggplot 在 R 中绘制堆积条形图。我还想在每条条中包含该条的百分比。我试图关注帖子123但这些值并不完全在它们各自的块中。我的数据是dropbox中的一个文件。

我的代码如下:

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")

输出是-

在此处输入图像描述

任何帮助是极大的赞赏。感谢您的帮助和时间!

4

2 回答 2

11

我认为您使用的是旧版本的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

于 2013-02-18T20:24:38.743 回答
5

这里有一个使用barchartfrom lattice 的解决方案。

在此处输入图像描述

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())
于 2013-02-19T00:33:54.023 回答