【问题标题】:How to reproduce the pareto.chart plot from the qcc package using ggplot2?如何使用 ggplot2 从 qcc 包中重现 pareto.chart 图?
【发布时间】:2012-04-13 10:15:09
【问题描述】:

我一直在使用 R 中 qcc 包中的 pareto.chart 函数,我非常喜欢它。现在我想移植我所有的图形来使用 ggplot2 包。然而,尽管有优秀的文档,但我对 ggplot2 的了解非常有限,所以我无法弄清楚所有细节。基本上我想要一个看起来像这样的情节

但是用 ggplot2 包代替。生成情节的代码如下:

    library(qcc)
    defect <- c(80, 27, 66, 94, 33)
    names(defect) <- c("price code", "schedule date", "supplier code", "contact num.", "part num.")
    pareto.chart(defect, ylab = "Error frequency", col=heat.colors(length(defect)))

有人对此有解决方案吗?帕累托图在here 之前已经讨论过,但结果看起来与我想要的并不相似。

【问题讨论】:

    标签: r plot ggplot2 qcc


    【解决方案1】:

    给你:

    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))
    

    【讨论】:

    • 太好了,谢谢安德烈。您是否还知道我如何能够扩展您的解决方案以添加正确的 y 轴?我指的是“累积百分比”轴。
    • ggplot 不支持添加第二个 y 轴,出于非常合理的理论原因。
    • 好的。谢谢你的解决方案。 :)
    猜你喜欢
    • 2018-06-17
    • 2021-06-09
    • 2022-08-15
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多