在甜甜圈图中排序图例不起作用ggplot2 R

时间:2018-07-24 21:28:59

标签: r ggplot2

我试图根据mylabelStatus 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)

enter image description here

0 个答案:

没有答案