繁体   English   中英

通过填充位置将标签添加到geom_bar

[英]Add labels to geom_bar by fill location

我有以下数据:

set.seed(1)
df <- data.frame(type=c("A","B","D","A","C","D","B","C","D","E"),
                 count=as.integer(runif(10,100,200)),
                 sample=c(1,1,1,2,2,2,3,3,3,3),stringsAsFactors=F)
df$type <- factor(df$type,levels=unique(sort(df$type)))

我使用geom_bar

require(ggplot2)
ggplot(df,aes(x=sample,y=count,fill=type))+geom_bar(stat="identity",position="dodge")+theme(legend.position="none")

在此处输入图片说明

我的问题是如何在每个栏的顶部添加df$type作为标签?

新增:

geom_text(aes(label=type,hjust=0),position=("dodge"))

显然不起作用

这将起作用:

ggplot(df,aes(x=sample,y=count,fill=type))+
  geom_bar(stat="identity",position="dodge")+
  geom_text(aes(label=type,hjust=0, vjust=-0.1),position=position_dodge(width = 1))+
  theme(legend.position="none") 

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM