Edw*_*ese 0 label position r bar-chart ggplot2
gnew = ggplot(data= got, aes(x= got$attacker_king, fill= got$attacker_outcome))+
geom_bar() +
geom_text(stat = "count", aes(label =..count..), vjust = -.5)+
scale_y_continuous(limits = c(0,20)) # plotting the stacked ggplot #this works
Run Code Online (Sandbox Code Playgroud)
got = ddply(got, .(got$attacker_king),
transform, pos = cumsum(..count..)- (0.5 *..count..)) # positioning labels shows error
#This shows error as "Error in eval(expr, envir, enclos) : object '..count..' not found"
Please help!
Run Code Online (Sandbox Code Playgroud)
您可以position=position_stack(vjust=0.5)在geom_text()通话中使用。
使用数据集的示例iris。
library(ggplot2)
ggplot(data= iris, aes(x= Sepal.Width, fill= Species))+
geom_bar() +
geom_text(stat = "count", aes(label =..count..), position=position_stack(vjust=0.5))+
scale_y_continuous(limits = c(0,20))
Run Code Online (Sandbox Code Playgroud)
根据如何计算百分比的请求进行编辑。
library(ggplot2)
library(dplyr)
summary<-as.data.frame(iris %>%
group_by(Species,Sepal.Width) %>%
tally %>%
group_by(Sepal.Width) %>%
mutate(pct=(100*n)/sum(n)))
ggplot(data= summary, aes(x= Sepal.Width, y=n,fill= Species))+
geom_bar(stat='identity') +
geom_text(aes(label =round(pct,2)), position=position_stack(vjust=0.5),size=3)+
scale_y_continuous(limits = c(0,20))
Run Code Online (Sandbox Code Playgroud)