2016-06-20 50 views
2

enter image description here如何將abline()添加到pareto.chart()/ barplot()?

我使用的是本作帕累託圖(庫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) 

由於我的數據是巨大的我想10%的時間間隔爲x軸,但不知之後添加abline我沒有得到線。

請讓我知道,如果一些功能可以在這裏工作或abline不允許在pareto?

+0

剛剛加入這個或其他的例子,因爲這: abline(V = 1000,山口=「紫色」) –

+0

對不起,我是新於R,但我想只要我的V = 1000它會告訴我一個垂直線。這不正確嗎?我也用排序的所有十個區間這樣v = seq(0,100,by = 25),但沒有解決。因此,以v = 1000爲例,看看這條線是否出現,但沒有任何信息 –

+0

x的長度以百萬爲單位 –

回答

5

帕累託圖和柱狀圖中

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 and bar

現在你可以看到吧一致。

酒吧在哪裏?

這是一個陷阱,pareto.chart()不會返回這些酒吧的位置。此前我們已經保存在ypareto.chart()的結果,現在這都是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 

通過這種方式,我們必須調用

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

現在,如果我們在這些位置做abline():爲了得到酒吧的位置barplot()

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

abline

添加abline()/segments()每0.1分位數

我會建議使用:

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) 

plot

+0

非常感謝! 我的整個代碼: 產品<-abcd $產品 名稱(產品)< - ABCD $客戶ID pareto.chart(產品,ylab = 「商品數量」,xlab = 「客戶」,xaxt =「N 「, cumperc = seq(0,100,by = 25), col = heat.colors(length(Product)),plot = TRUE) abline(v = 1000) –

+0

嗨,夥伴,你能告訴我爲什麼你在y <-pareto中添加了一個[,2]。抱歉太天真了。 –

+0

嗨李,如果我想要這些垂直線說如何x的兩個選擇x1 = 50%和x2 = 90%怎麼辦 –

相關問題