Reply
Frequent Visitor
Posts: 7
Registered: ‎03-08-2017

R Control Chart

HelloPBI Community,

 

I havent found too much info on R Control Charts in this forum and am wondering if there is anyone out there that could copy&paste one?

 

 

Cheers

Member
Posts: 69
Registered: ‎11-11-2015

Re: R Control Chart

Here's is an example of creating a histogram with a density distribution line.

 

hist(dataset$Age, 
     main = "Customer Count by Age",
     ylab="Customer Count", 
     xlab="Customer Age",
     xlim = c(18, 100),
     border="black",
     breaks=20,
     col=c("lightyellow", "lightblue"), 
     las=1,
     probability = TRUE
)

lines(density(dataset$Age),lty="dotdash", lwd=4, col="red")

Here's an example of creating a barblot

 

barplot(dataset$'Sales Revenue',        
        names.arg = dataset$'Age Group', 
        main = "Sales Revenue by Customer Age Group",
        col = c("red","yellow","orange","blue", "green")
)

minValue <- 0
maxValue <- max(as.vector(dataset$'Sales Revenue'))
yTicks <- seq(from=minValue, to = maxValue, length.out = 10)
yTicks <- pretty(yTicks)
yTickLabels <- paste("$",format(yTicks/1000, , big.mark=","), "K",sep="")
axis(2, at=yTicks, labels = yTickLabels, lty = 1, las=1, cex.axis=0.7 )

Of course, these are simple examples using the built-in R graphics functionality. You can also use a richer graphics package such as lattice or ggplot2 to create some really detailed charts and graphs.

 

Is this what you are looking for?

Frequent Visitor
Posts: 7
Registered: ‎03-08-2017

Re: R Control Chart

Thanks Ted, this is along the same lines, but I am interested more in an "xbar" chart and also looking for a simple R script to create a Pareto chart for my customers.

 

Thank you for the scripts for the Histogram and Bar Plot, I will also be adding this to my arsenal.

Member
Posts: 63
Registered: ‎02-10-2016

Re: R Control Chart

RE:  also looking for a simple R script to create a Pareto chart

In order to use any chart -that is part of a R Visualization package library - it's logical to first make sure it's installed.

install.packages('<yourPackage>')


Now considering the huge amount of R package visualization libraries, there may be a few ways to reproduce what you want. Note that I don't consider myself an R guru at this stage, still here's a couple of basic samples.

qcc library...

qccPareto.png

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

ggplot2 library...

ggplot2Pareto.png

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$**bleep** <- cumsum(dat$count)
dat

ggplot(dat, aes(x=defect)) +
  geom_bar(aes(y=count), fill="blue", stat="identity") +
  geom_point(aes(y=**bleep**)) +
  geom_path(aes(y=**bleep**, group=1))


I haven't played much with x-Bar chart though I'll check if I can find something... Anyway hope this helps


Member
Posts: 63
Registered: ‎02-10-2016

Re: R Control Chart

RE : xBarChart

In true life scenario, I'd say the following data would probably be obtained as a result
of fancy mathemathic algorithm formula... though I'll leave that to Math PhDs...Smiley Happy
So for for simplicity I just added a list of subgroup manually...

So here we go...

#declare qcc library:

library(qcc)

# Load a mock list of 10 subgroup data manually:

sg1 <- c(1.397742,1.399917,1.278918,1.279828) # Fill in subgroup 1 data!
sg2 <- c(1.283877,1.307215,1.341566,1.396107) # Fill in subgroup 2 data!
sg3 <- c(1.313634,1.278839,1.242498,1.331201) # Fill in subgroup 3 data!
sg4 <- c(1.245943,1.303432,1.390168,1.298949) # Fill in subgroup 4 data!
sg5 <- c(1.188624,1.226905,1.217450,1.284127) # Fill in subgroup 5 data!
sg6 <- c(1.287875,1.283185,1.234186,1.314055) # Fill in subgroup 6 data!
sg7 <- c(1.276711,1.284376,1.305309,1.249184) # Fill in subgroup 7 data!
sg8 <- c(1.312219,1.297509,1.272367,1.371223) # Fill in subgroup 8 data!
sg9 <- c(1.378350,1.312981,1.381944,1.268875) # Fill in subgroup 9 data!
sg10 <- c(1.332196,1.268824,1.299608,1.329053) # Fill in subgroup 10 data!

# Include those subgroups into a my.data mock list through rbind

my.data <- rbind(sg1,sg2,sg3,sg4,sg5,sg6,sg7,sg8,sg9,sg10)

# Draw the R Chart and calculate relevant metrics

q1 <- qcc(my.data, type="R", nsigmas=3)

which should generate similar R chart:

RChart.png

then add following to ldraw the X-BAR chart...

# Draw the X-Bar Chart and calculate relevant metrics
q2 <- qcc(my.data, type="xbar", nsigmas=3)

which should generate following X-Bar Chart:
xBar.png

Finally:

# Establish the LSL and USL as set by customer specs, then
# draw the process capability chart and calculate metrics:

lsl <- 1.31 # Fill in a mock LSL here!
usl <- 1.32 # Fill in a mock USL here!

process.capability(q2, spec.limits=c(lsl,usl))

which should end up with following:
ProcessCap.png

Hope this helps...

Frequent Visitor
Posts: 7
Registered: ‎03-08-2017

Re: R Control Chart

Thank you for all of this information! I am going to try and take some time this weekend to put into play your examples.

 

Much appreciated! Ill reply back with any progress I can make.

 

Cheers.

Frequent Visitor
Posts: 7
Registered: ‎03-08-2017

Re: R Control Chart

I have been able to create the R and XBar charts in PowerBI R Visuals, but still need some adjustments as it doesnt look right to me (data not displaying as expected).

 

library(qcc)

months<- c("Jan", "Feb", "Mar", "Apr","May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec")

qcc(dataset, type="R", nsigmas=1, labels=months, xlab= "Month", ylab = "Service Level %", title = "Phone Call SVL", digits=3)

 

R Chart 2017-03-20_9-34-56.jpg

 

Any idea how to get the Y axis to show as percentages? My data is in percentages as you can see when selecting a simple Line Chart from the PowerBI Visuals.

 

Line 2017-03-20_9-45-45.jpg

 

Thanks for all your assistance!

 

 

 

Member
Posts: 63
Registered: ‎02-10-2016

Re: R Control Chart

Sorry for late response. For the sake of simplicity, I mainly used mock data which I entered
manually. The idea was basically to create a series of 4 weeks subsets over a 12 months period sort if thing...
We both agree that a real life scenario would have gone through a much fancier scenario such as querying data
from a server and even adding conversion at some stage. I used numeric for simplicity.

Also I noticed that even if qcc library offers a wide variety of statisitic chart [R, xBar,...], it doesn't seem to have
any qcc - y axis formatting options parameter as you would probably find using scaling_y_continuous in ggplot2 library.
So in this case I don't believe y axis percentage formatting could be done in one shot.

 

My best bet would be to define the qcc : q1 R Chart and q2 xBar  in respectice class with a plot=False attribute

 library(qcc)
 Jan <- c(0.837742,0.839917,0.728918,0.729828) # Fill in subgroup January data!
 Feb <- c(0.783877,0.807215,0.841566,0.836107) # Fill in subgroup February data!
 Mar <- c(0.813634,0.728839,0.742498,0.831201) # Fill in subgroup March data!
 Apr <- c(0.745943,0.803432,0.830168,0.798949) # Fill in subgroup April data!
 May <- c(0.688624,0.726905,0.717450,0.784127) # Fill in subgroup May data!
 Jun <- c(0.787875,0.783185,0.714186,0.814055) # Fill in subgroup June data!
 Jul <- c(0.726711,0.784376,0.805309,0.749184) # Fill in subgroup July data!
 Aug <- c(0.812219,0.797509,0.722367,0.871223) # Fill in subgroup August data!
 Sep <- c(0.878350,0.812981,0.881944,0.768875) # Fill in subgroup September data!
 Oct <- c(0.832196,0.768824,0.799608,0.729053) # Fill in subgroup October data!
 Nov <- c(0.813634,0.728839,0.742498,0.831201) # Fill in subgroup November data!
 Dec <- c(0.726711,0.784376,0.805309,0.749184) # Fill in subgroup December data!
 
 # Include those subgroups into a my.data mock list through rbind
 
 dataset <- rbind(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
 months<- c("Jan", "Feb", "Mar", "Apr","May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec") 

# assign q1 q1 <- qcc(dataset, type="R", nsigmas=1, labels=months, xlab= "Month", ylab = "Service Level %", title = "Phone Call SVL", digits=3, label.limits = c("5%", "14%"),plot=FALSE) # assign q2
q2 <- qcc(dataset, type="xbar", nsigmas=1, labels=months, xlab= "Month", ylab = "Service Level %", title = "Phone Call SVL", digits=3, label.limits = c("76%", "80%"), plot=FALSE)

and then use each q1 & q2 with a plot method allowing to erase the y axis and redifine a new one with somekind of thicks. 

plot(q1, yaxt="n", ... , xlab="Months", ylab="Service Levels %", title="Phone Call SVL", label.limits= c("5%", "13%"))

RChart.png

plot(q2, yaxt="n", ...,  xlab="Months", ylab="Service Levels %", title="Phone Call SVL", label.limits= c("76%", "80%"))

xBar.png

Tom Hopper has written a blog on rewriting qcc plot using ggplot2 and grid,
  https://tomhopper.me/2014/03/03/rewriting-plot-qcc-using-ggplot2-and-grid/

though I haven't got a chance to dive deep into it. I wish I could bring you an easiy solution on y axis percentage formatting
though I haven't found one with qcc. Sorry.

 

 

Frequent Visitor
Posts: 2
Registered: ‎10-23-2017

Re: R Control Chart

[ Edited ]

edited: learned how to refer to individual column in dataset and how to define x-axis labels. still, the sample order is wrong..

edited2: got it solved myself. details below to other learners like me.

 

Hello,

 

I've been trying to create xbar.one (individual) chart with power bi / qcc, with no luck so far. It seems values inside dataset somehow automatically get rearranged to ascending order. If I provide the values in script manually, chart looks fine. But if they come from Excel or SQL table (power bi's automatically formed dataset), values get rearranged. I'd also like to bind the values to labels on x-axis, but haven't found the way how.

 

 

I'm new to R.

 

Samples:

360.1

360.2

360.0

359.8

 

 

Capture.PNG 

Any help is greatly appreciated.

 

 

Solved: 

 

newdata <- dataset[order(dataset$SERIALID),]

 

library(qcc)

x <- c(newdata$SERIALID)

s <- c(newdata$NUM_VALUE)

 

qcc(s, type="xbar.one", labels=x)

 

 

- kari