我试图根据mylabel
或Status Count
列的顺序(降序)在数据框中定义per
列的级别。不知何故,我无法使它正常工作,并且图例也没有按照我想要的方式排序。任何想法为什么这不起作用?
df <- structure(list(Status = structure(1:4, .Label = c("Cancelled",
"Closed", "SAP", "Open"), class = "factor"), `Status Count` = c(46L,
290L, 814L, 75L)), .Names = c("Status", "Status Count"), row.names = c(NA,
4L), class = "data.frame")
df$per <- round(100* df$`Status Count`/sum(df$`Status Count`),0)
df$mylabel <- paste(df$Status,'(', df$`Status Count`,') ',df$per,'%')
df$mylabel <- factor(df$mylabel,levels = reorder(df$mylabel,df$`Status Count`))
# Barplot
ggplot(df, aes(x="", y=`Status Count`,fill=mylabel)) +
geom_bar(width = 0.3, stat = "identity") +
labs(fill="Status", x=NULL, y=NULL) +
coord_polar(theta="y", start=0)