R堆叠条形图绘制几何图形文本

R堆叠条形图绘制几何图形文本,r,ggplot2,R,Ggplot2,我正在尝试使用ggplot在R中绘制一个堆叠条形图。我还想包括在每一块酒吧的百分比为这一块。我试着跟随这些帖子,但是这些值并不完全在它们各自的块中。我的数据是中的一个文件 我的代码如下: f<-read.table("Input.txt", sep="\t", header=TRUE) ggplot(data=f, aes(x=Form, y=Percentage, fill=Position)) + geom_bar(stat="identity", colour="blac

我正在尝试使用ggplot在R中绘制一个堆叠条形图。我还想包括在每一块酒吧的百分比为这一块。我试着跟随这些帖子,但是这些值并不完全在它们各自的块中。我的数据是中的一个文件

我的代码如下:

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

f我认为您使用的是较旧版本的
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
这使得:


这里是一个使用晶格中的
条形图的解决方案

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

库(latticeExtra)
条形图(百分比~形式|样本类型*样本名称,数据=dat,
组=位置,堆栈=T,
面板=功能(…){
面板条形图(…)

我会考虑你给
geom\u text
的y值。它是每个条形段的高度。你需要做一些算术来计算中点(或顶部,或其他)对于每个条形段,将其作为单独的变量添加,并将其用作
geom_text
中的y变量。函数
cumsum
可能会有所帮助。类似的操作将计算每个条形段中点处标签的y位置:
f
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())