R堆积的条形图绘制geom_text [英] R stacked bar graph plotting geom_text
问题描述
我试图在R中使用ggplot绘制堆积的条形图。我还希望在每件酒吧中包含百分比。我试图按照 1 , 2 ,3 但值不完全在其各自的块。我的数据是保管箱中的文件。
我的代码如下:
f <-read.table(Input.txt, sep =\ t,header = TRUE)
ggplot(data = f,aes(x = Form,y = Percentage,fill = Position))+
geom_bar(stat = 身份,颜色=黑色)+
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,color =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())+
scal e_fill_hue(c = 45,l = 80)
ggsave(Output.pdf)
输出是 -
非常感谢您的任何帮助。
感谢您的帮助和时间!
我认为您使用的是旧版本的 GGPLOT2
。因为你的代码修改为ggplot2 v 0.9.3,我得到这个:
pre $ p < - ggplot(data = df, aes(x = Form,y = Percentage,fill = Position))
p < - p + geom_bar(stat =identity,color =black)
p < - p + geom_text =stack,aes(x = Form,y = Percentage,ymax = Percentage,label = Percentage,hjust = 0.5))
p <-p + facet_grid(Sample_name〜Sample_type,scales =free =free)
p < - p + theme(plot.title = element_text(Input_profile),
axis.text.x = element_text(angle = 90,hjust = 1,size = 8 ,color =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
你会发现文本对象通常是正确放置的。有些地方酒吧太短,所以数字重叠。你也可以玩 size
参数。
为了解决这个问题,你可以这样做(df,。(Form,Sample_type,Sample_name),transform,$ b($ d $,$ $ b cum.perc = Reduce('+',list(Percentage / 2,cumsum(c(0,head(Percentage,-1))))))
p < - ggplot = df,aes(x = Form,y = Percentage,fill = Position))
p <-p + geom_bar(stat =identity,color =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,color =grey50),
plot.title = element_text(face =bold,size = 11),
axis.title.x = element_text(face =bold ,size = 9),
ax panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )
p< - p + scale_fill_hue(c = 45,l = 80)
p
这给出:
p>
I'm trying to plot a stacked bar graph in R using ggplot. I also want to include percentage in each piece of bars for that piece. I tried to follow the posts 1, 2, 3 but the values are not exactly in their respective blocks. My data is a file in dropbox.
My code is as follows:
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")
The output is-
Any help is greatly appreciated. Thank you for your help and time!
I think you're using an older version of ggplot2
. Because with your code modified for ggplot2 v 0.9.3, I get this:
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
You see that the text objects are normally placed properly. There are places where the bars are just too short so that the numbers overlap. You can also play with the size
parameter.
To rectify that, you could do something like this to add up the numbers by yourself.
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
This gives:
这篇关于R堆积的条形图绘制geom_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!