Skip to content

Instantly share code, notes, and snippets.

@ggarza
Created December 11, 2012 21:43
Show Gist options
  • Save ggarza/4262452 to your computer and use it in GitHub Desktop.
Save ggarza/4262452 to your computer and use it in GitHub Desktop.
An R script that creates a pareto diagram.
# tikz("./figures/pareto.tikz", width=6.0, height=3.50)
defects <- c(56, 45, 23, 12, 8, 6)
names(defects) <- c("Blem.", 'Scratch', 'Chip', 'Bend', 'Dent',
'Others')
defects_sort <- sort(defects, decreasing=TRUE)
# par(mar = c(5, 4, 4, 4) + 0.3)
par(mar = c(5, 4, 3, 4) + 0.3)
bp <- barplot(defects,
# panel.first=grid(ny=NULL, nx=NA, lty="dashed", col="black"),
ylim=c(0, sum(defects)* 1.05),
axes=F,
main="Product Defects",
)
grid(ny=NULL, nx=NA, lty="dashed", col="grey60")
lines(bp, cumsum(defects_sort),
type='o',
xlab='',
ylab='',
pch=20
)
box()
axis(side=2, at=seq(0, sum(defects), by=50), labels=T, las=1)
mtext("Count", side=2, line=3)
axis(side=4,
at=seq(0, sum(defects), length.out=5),
labels=seq(0, 100, length.out=5),
las=1)
mtext("Percent", side=4, line=3)
mtext("Defect",
at=0.0,
adj = 1 ,
side=1, line=1, col="blue")
percent <- round(100 * defects_sort/sum(defects_sort), 1)
cum_pct <- round(100 * cumsum(defects_sort/sum(defects_sort)), 1)
mtext(defects_sort,
side=1, line=2, at=bp)
mtext("Count",
adj = 1 ,
at=0.0,
side=1, line=2, col="blue")
mtext(percent,
side=1, line=3, at=bp)
mtext("Percent",
adj = 1 ,
at=0.0,
side=1, line=3, col="blue")
mtext(cum_pct,
side=1, line=4, at=bp)
mtext("Cum. \\%",
adj = 1 ,
at=0.0,
side=1, line=4, col="blue")
# dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment