在R

2015-04-15 85 views
2

我想寫一個函數,將產生我認爲實際點的情節(不像克利夫蘭品種,我需要一個單點散點圖堆積的點爲(幾乎)相等的值)。我已經接近:在R

enter image description here

在該圖中,你看到的點實際上是旋轉小寫文本字符串「O」秒。這樣做是因爲如果繪圖重新縮放,我需要點間距保持不變。但是,我希望比小寫的「o」更好,例如填充點而不是圓圈。如果我可以訪問用於標準繪圖符號的字體(中的plot函數和親屬),則可以完成此操作。然後,我可以用該字體創建一個文本字符串並獲取所需內容。有誰知道如何做到這一點?

PS - 不,具有大量垃圾箱的直方圖不是可接受的替代品。

+1

的警告你不能用'points()'添加點到一個圖表嗎?沒有真正的陰謀「字體」。沒有代碼或數據,這真的不是一個我們可以提供幫助的編程問題。 – MrFlick

+0

@MrFlick - 不,因爲點之間的間距取決於圖的垂直尺寸。正如我試圖解釋的那樣,無論縮放比例如何,我都希望這些點的字符寬度是分開的。 – rvl

回答

1

我確實找到了一種使用低級圖形參數(即「usr」,繪圖區域的實際用戶座標以及「cxy」,字符大小)獲取所需點圖的方法。在調整圖形大小時,recordGraphics()函數會封裝需要更改的部分。這裏的功能:如果你想點之間更緊密或鬆散的差距

dot.plot = function(x, pch = 16, bins = 50, spacing = 1, xlab, ...) { 
    if(missing(xlab)) 
    xlab = as.character(substitute(x)) 

    # determine dot positions 
    inc = diff(pretty(x, n = bins)[1:2]) 
    freq = table(inc * round(x/inc, 0)) 
    xx = rep(as.numeric(names(freq)), freq) 
    yy = unlist(lapply(freq, seq_len)) 

    # make the order of the dots the same as the order of the data 
    idx = seq_along(x) 
    idx[order(x)] = idx 
    xx = xx[idx] 
    yy = yy[idx] 

    # make a blank plot 
    plot(xx, yy, type = "n", axes = FALSE, xlab = xlab, ylab = "") 

    # draw scale 
    axis(1) 
    ylow = par("usr")[3] 
    abline(h = ylow) # extend to full width 

    # draw points and support resizing 
    recordGraphics({ 
     yinc = 0.5 * spacing * par("cxy")[2] 
     points(xx, ylow + yinc * (yy - .5), pch = pch, ...) 
    }, 
    list(), 
    environment(NULL)) 

    invisible() 
} 

spacing參數可以使用。一個例子...

with(iris, dot.plot(Sepal.Length, col = as.numeric(Species))) 

Dot plot of sepal length colored by species

這是不是試圖用文字做一個更好的解決方案,也有點嚇人,因爲你的文檔中看到recordGraphics