티스토리 뷰

차기작 : R을 배우자

frequency polygons

quantlab 2014. 2. 22. 10:47
frequency polygons

frequency polygons

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

Statistics 202: Statistical Aspects of Data Mining, Summer 2007

It drew my attention and I watched the first video and the 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 the 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 are 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")

plot of chunk unnamed-chunk-2

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

plot of chunk unnamed-chunk-2

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

plot of chunk unnamed-chunk-2

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

plot of chunk unnamed-chunk-2

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

plot of chunk unnamed-chunk-2

You see some warnings. Just ignore it.

Function col2rgb tells you how to figure argument to superimpose two frequency polygons.

col2rgb("red")
##       [,1]
## red    255
## green    0
## blue     0
col2rgb("blue")
##       [,1]
## red      0
## green    0
## blue   255



'차기작 : R을 배우자' 카테고리의 다른 글

R source for lempel-ziv complexity  (0) 2014.08.02
R studio, Git, BitBucket  (0) 2014.02.25
Python  (0) 2014.02.15
and R  (0) 2014.02.15
Python and R  (0) 2014.02.15
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함