You are on page 1of 3

frequency polygons

As i surfed around the web, I came across this lecture

Statistics 202: Statistical Aspects of Data Mining, Summer 2007


The Lecture The Web It drew my attention and I watched the first video andthe whole slides from the web. It wasn't so hard. Anyway, the Lecturer introduces frequnecy polygon, which looks cool. So I implemented it as a function, freqPolygon. Here's code. freqPolygon = function(data, new = T, lty = "solid", col, border = col, ...) {

histDflt <- hist(data, plot = F, ...)

counts <- histDflt$counts breaks <- histDflt$breaks

breaksInterval <- breaks[2] - breaks[1]

breaksNew <- c(breaks[1], breaks[-(length(breaks))] + breaksInterval/2, breaks[length(breaks)]) countsNew <- c(0, counts, 0)

if (new == T) { plot(breaksNew, countsNew, col = col, pch = 19, ...) } else { points(breaksNew, countsNew, col = col, pch = 19, ...) }

polygon(breaksNew, countsNew, col = col, lty = lty, border = border, ...)

result = list() result$breaks = breaksNew result$counts = countsNew

invisible(result) }

And Here's some examples freqPolygon(c(2, 3, 4, 2, 5, 2, 3), col = "blue", new = T) freqPolygon(c(1, 2, 1, 1, 2, 3, 2), col = "red", new = F, lty = "dotted")

freqPolygon(c(2, 3, 4, 2, 5, 2, 3), col = "blue", new = T) freqPolygon(c(1, 2, 1, 1, 2, 3, 2), col = "red", new = F, lty = "dotted")

freqPolygon(c(2, 3, 4, 2, 5, 2, 3), col = rgb(0, 0, 1, 0.2), new = T) freqPolygon(c(1, 2, 1, 1, 2, 3, 2), col = rgb(1, 0, 0, 0.2), new = F, lty = "dotted")

freqPolygon(c(2, 3, 4, 2, 5, 2, 3), col = rgb(0, 0, 1, 0.2), new = T, xlim = c(0, 6))

## Warning: argument 'xlim' is not made use of

freqPolygon(c(1, 2, 1, 1, 2, 3, 2), col = rgb(1, 0, 0, 0.2), new = F, lty = "dotted", xlim = c(0, 6))

## Warning: argument 'xlim' is not made use of

freqPolygon(c(2, 3, 4, 2, 5, 2, 3), col = rgb(0, 0, 1, 0.2), new = T, xlim = c(0, 6), ylim = c(0, 6), freq = F)

## Warning: arguments 'freq', 'xlim', 'ylim' are not made use of

## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter

freqPolygon(c(1, 2, 1, 1, 2, 3, 2), col = rgb(1, 0, 0, 0.2), new = F, lty = "dotted", xlim = c(0, 6), ylim = c(0, 6), freq = F)

## Warning: arguments 'freq', 'xlim', 'ylim' are not made use of ## Warning: "freq" is not a graphical parameter ## Warning: "freq" is not a graphical parameter

You see some warnings. Just ignore it. Function col2rgb tells you how to figure col2rgb("red") argument to superimpose two frequency polygons.

## ## red ## green ## blue

[,1] 255 0 0

col2rgb("blue")

## ## red ## green ## blue

[,1] 0 0 255

You might also like