如何使用ggplot2从qcc包中重现pareto.chart图? [英] How to reproduce the pareto.chart plot from the qcc package using ggplot2?
问题描述
我一直使用R的qcc包中的pareto.chart函数,我非常喜欢它。现在我想移植我的所有图形来代替使用ggplot2包。然而,尽管有出色的文档,我对ggplot2的了解仍然非常有限,所以我无法弄清楚所有细节。基本上我想要一个看起来像这样的情节
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. However, my knowledge of ggplot2 is very limited despite the excellent documentation so I cannot figure out all the details. Basically I want a plot looking like this
,但用ggplot2软件包替代。下面列出了生成该图的代码:
but made with the ggplot2 package instead. The code for producing the plot is listed below:
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)))
有没有人有解决方案? 此处之前已经讨论过帕累托图,但结果确实如此看起来不像我想要的东西。
Does anyone have a solution for this? The pareto chart has been discussed before here but the result does not look anything similar to what I want.
推荐答案
在这里你可以:
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))
这篇关于如何使用ggplot2从qcc包中重现pareto.chart图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!