【问题标题】:How to add abline() to pareto.chart() / barplot()?如何将 abline() 添加到 pareto.chart() / barplot()?
【发布时间】:2016-06-20 06:24:18
【问题描述】:

我正在使用 this 作为帕累托图(库 qcc):

## abcd is a data frame, and I am producing chart for column products
Product <- abcd$products
names(Product)<-abcd$customerid
pareto.chart(Product, ylab = "Number of Products", xlab="Customer", xaxt="n")
abline(v = 1000)

由于我的数据很大,我想在 x 轴 10% 的间隔后添加 abline,但不知何故我没有得到线条。

请让我知道某些功能是否可以在这里工作或者abline 不允许在帕累托中使用?

【问题讨论】:

  • 刚刚添加了这个或其他示例:abline(v=1000,col="purple")
  • 对不起,我是 R 新手,但我认为无论我的 v=1000 哪里都会显示一条垂直线。不正确吗?我还对所有十个间隔使用了排序,例如 v=seq(0, 100, by = 25) 但没有成功。所以以 v=1000 为例,看看有没有上线但没有上线
  • x 的长度以百万为单位
  • 由于我的服务器挂起,我无法获取图像。我不知道这些东西是如何工作的,伙计。如果它与我的声誉有关(我不知道那是什么),请继续减少它,以便我将被阻止或其他什么,我可以转移到其他网站。但是,到目前为止,我只能向您展示这么多信息
  • 非常感谢您的帮助。我现在得到了图像,但不知何故我无法在这里发布它。对此我深表歉意,但帕累托是正常的 80 20 曲线,其中我需要间隔 10% 的垂直线。对于您的问题,x 轴是客户 ID,y 轴是他们正在使用的产品数量。 abcd 是表名。

标签: r plot bar-chart


【解决方案1】:

排列图和条形图

pareto.chart()是基于barplot()的,所以只要你知道abline()怎么加到barplot()上,你就知道pareto.chart()怎么用了。为了证明他们的关系,请考虑以下几点:

## example from ?pareto.chart
defect <- c(80, 27, 66, 94, 33)
names(defect) <- c("price code", "schedule date", "supplier code", "contact num.", "part num.")
y <- pareto.chart(defect, ylab = "Error frequency")
barplot(0.2 * defect, add = TRUE, col = "grey")

现在您可以看到这些条重合了。

酒吧在哪里?

pareto.chart() 不返回这些条的位置是一个陷阱。之前我们将pareto.chart()的结果保存在y中,现在这就是y的全部了:

> str(y)
num [1:5, 1:4] 94 80 66 33 27 94 174 240 273 300 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5] "contact num." "price code" "supplier code" "part num." ...
  ..$ Pareto chart analysis for defect: chr [1:4] "Frequency" "Cum.Freq." "Percentage" "Cum.Percent."

这就是要打印的全部内容:

> y               

Pareto chart analysis for defect
                Frequency Cum.Freq. Percentage Cum.Percent.
  contact num.         94        94   31.33333     31.33333
  price code           80       174   26.66667     58.00000
  supplier code        66       240   22.00000     80.00000
  part num.            33       273   11.00000     91.00000
  schedule date        27       300    9.00000    100.00000

这样,我们必须调用barplot()才能获得酒吧位置:

x <- as.numeric(barplot(defect, plot = FALSE))
# > x
# [1] 0.7 1.9 3.1 4.3 5.5

现在,如果我们在这些位置上执行abline()

pareto.chart(defect, ylab = "Error frequency")
abline(v = x, col = 2)  ## red

每 0.1 个分位数添加 abline() / segments()

我建议使用:

x <- range(as.numeric(barplot(Product, plot = FALSE)))
x0 <- seq(x[1], x[2], length = 11)  ## 11 break points for 10 intervals
y <- pareto.chart(Product, ylab = "Number of Products", xlab="Customer", xaxt="n")[, 2]
y0 <- y[round(seq(from = 1, to = length(y), length = 11))]
## abline(v = v0, col = "purple")
segments(x0, rep(0, 11), x0, y0, col = "purple")

作为演示,我使用

set.seed(0); Product <- rbinom(100, 20, 0.3)

【讨论】:

  • 非常感谢!我的整个代码: Product
  • 嗨,伙计,你能告诉我为什么你在 y
  • 嗨,李,如果我想要这些垂直线在 x 中选择 x1=50% 和 x2=90% 时该怎么办
  • 谢谢李。我明白了。还有一件事,如果我想要 Y 轴的 50% 和 90% 或任意 2%。上面的在 x 轴上给出了 50 90。 Y轴的50%.90%该怎么办
  • 你好,李。我试图逆函数来获得已知 y 的 x。我失败了。您是否建议任何其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多