How to reproduce the pareto.chart plot from the qcc package using ggplot2?

前端 未结 1 578
闹比i
闹比i 2020-12-30 12:36

I have been using the pareto.chart function from the qcc package in R and I really like it. Now I would like to port all my graphics to utilize the ggplot2 package instead.

相关标签:
1条回答
  • 2020-12-30 13:18

    Here you go:

    library(ggplot2)
    
    counts  <- c(80, 27, 66, 94, 33)
    defects <- c("price code", "schedule date", "supplier code", "contact num.", "part num.")
    
    dat <- data.frame(
      count = counts,
      defect = defects,
      stringsAsFactors=FALSE
    )
    
    dat <- dat[order(dat$count, decreasing=TRUE), ]
    dat$defect <- factor(dat$defect, levels=dat$defect)
    dat$cum <- cumsum(dat$count)
    dat
    
    ggplot(dat, aes(x=defect)) +
      geom_bar(aes(y=count), fill="blue", stat="identity") +
      geom_point(aes(y=cum)) +
      geom_path(aes(y=cum, group=1))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题