2013-04-10 108 views
4

我有一個兩個對數軸的情節。我想爲該圖的某個位置添加一個圓。我試圖使用plotrix,但是這不會給出「log-radius」的選項。如何在R中的對數圖中繪製圓?

# data to plot 
x = 10^(-1 * c(5:0)) 
y = x ^-1.5 

#install.packages("plotrix", dependencies=T) 
# use require() within functions 
library("plotrix") 

plot (x, y, log="xy", type="o") 
draw.circle(x=1e-2, y=1e2, radius=1e1, col=2) 

如何添加一個圓到我的對數圖?

回答

6

正如krlmlr所示,最簡單的解決方案是稍微修改plotrix::draw.circle()。對數 - 對數座標系扭曲以線性標度給出的圓的座標;以抵消這一點,你只需要exponentiate計算的座標,我在下面的代碼打上## <-行所做的:

library("plotrix") 

draw.circle.loglog <- 
function (x, y, radius, nv = 100, border = NULL, col = NA, lty = 1, 
    lwd = 1) 
{ 
    xylim <- par("usr") 
    plotdim <- par("pin") 
    ymult <- (xylim[4] - xylim[3])/(xylim[2] - xylim[1]) * plotdim[1]/plotdim[2] 
    angle.inc <- 2 * pi/nv 
    angles <- seq(0, 2 * pi - angle.inc, by = angle.inc) 
    if (length(col) < length(radius)) 
     col <- rep(col, length.out = length(radius)) 
    for (circle in 1:length(radius)) { 
     xv <- exp(cos(angles) * log(radius[circle])) * x[circle]   ## <- 
     yv <- exp(sin(angles) * ymult * log(radius[circle])) * y[circle] ## <- 
     polygon(xv, yv, border = border, col = col[circle], lty = lty, 
      lwd = lwd) 
    } 
    invisible(list(x = xv, y = yv)) 
} 

# Try it out 
x = 10^(-1 * c(5:0)) 
y = x ^-1.5 

plot (x, y, log="xy", type="o") 
draw.circle.loglog(x = c(1e-2, 1e-3, 1e-4), y = c(1e2, 1e6, 1e2), 
        radius = c(2,4,8), col = 1:3) 

enter image description here

+0

[看到這裏](http://stackoverflow.com/questions/9265588/r-plotting-a-point-on-the-y-axis-when-the-x-axis-is-using-a-log-比例/ 9265840#9265840)進行相關討論。根據你想要的半徑單位的含義,你可能想在兩個座標指數行中用'10 ^(...)'代替'exp(...)'。 – 2013-04-13 07:57:05

+0

好的,我已經修復了處理radius參數的方式。現在,半徑爲10意味着在x軸上以1爲中心的圓將其左邊緣設爲0.1,其右邊緣設爲10.(注意,一個半徑爲1的圓將被繪製爲一個點(即,右邊和左邊的x座標)。) – 2013-04-14 14:40:55

+0

不錯,...你的draw.circle.loglog()應該definitley被添加到plotrix庫中。 – 2013-04-15 06:47:38

5

解決方法是明確應用log10

plot (log10(x), log10(y), type="o") 
draw.circle(x=log10(1e-2), y=log10(1e2), radius=log10(1e1), col=2) 

編輯(使用symbols):

plot (x, y, log="xy", type="o",xlim=c(1e-5,1), ylim=c(1,1e8)) 
par(new=T) 
symbols(x=1e-2, y=1e2, circles=1e1, xlim=c(1e-5,1), ylim=c(1,1e8), 
     xaxt='n', yaxt='n', ann=F, log="xy") 
+0

還有這裏另外一個答案,這表明使用'符號',但不幸的是我不能改變圓的半徑。我也不想重寫我所有的繪圖腳本(添加漂亮的軸刻度,...),只能添加一個圓。是不是有另一種方式,如「添加一個帶有線性軸和透明網的第二層,並在那裏繪製一個圓」或者「計算圓函數的座標並繪製它」?我很確定,有一個簡單的方法,... – 2013-04-10 11:52:04

+0

難道這不好嗎? – krlmlr 2013-04-12 09:29:12

+0

@Sven我已編輯我的答案使用'符號'。 – Nishanth 2013-04-15 04:34:59

3

功能draw.circleplotrix包看起來像我的系統上:

> draw.circle 
function (x, y, radius, nv = 100, border = NULL, col = NA, lty = 1, 
    lwd = 1) 
{ 
    xylim <- par("usr") 
    plotdim <- par("pin") 
    ymult <- (xylim[4] - xylim[3])/(xylim[2] - xylim[1]) * plotdim[1]/plotdim[2] 
    angle.inc <- 2 * pi/nv 
    angles <- seq(0, 2 * pi - angle.inc, by = angle.inc) 
    if (length(col) < length(radius)) 
     col <- rep(col, length.out = length(radius)) 
    for (circle in 1:length(radius)) { 
     xv <- cos(angles) * radius[circle] + x 
     yv <- sin(angles) * radius[circle] * ymult + y 
     polygon(xv, yv, border = border, col = col[circle], lty = lty, 
      lwd = lwd) 
    } 
    invisible(list(x = xv, y = yv)) 
} 
<environment: namespace:plotrix> 

這裏發生本質上的是圓由100個頂點的多邊形近似(參數nv)。你可以做以下任一操作:

  • 創建您自己的版本的draw.circle,做必要的座標變換「撤銷」軸的數變換。

  • 該函數不可見地返回用於繪圖的座標列表。 (如果您將矢量作爲radius傳遞,則只返回最後一個圓的座標。)您可能可以對這些座標應用變換並對結果調用polygon。通過border,col,lty和/或lwd的適當值可以隱藏函數本身繪製的多邊形。

第一個版本聽起來對我來說更容易。 只需將+ x替換爲循環內的* x,與y相同,即可完成。等同地,對於第二個版本,您減去x,然後乘以x的每個座標,對於y也是如此。編輯:這些轉換有些不對,請參閱Josh的正確答案。

+0

我從你接近問題的方式中學到了很多東西。雖然我接受了喬希的回答,但你會得到+50,因爲:(1)你的答案是喬希答案的基礎,你是第一(2)你承認喬希的答案(3)你的名譽較低。希望這對Josh很好。 – 2013-04-15 06:58:14

+0

@Sven - 對我來說這完全沒問題。我通常不會在這裏回答問題,除非我通過這樣做來學習某些東西,這個問題在這方面很有用。另外,krlmlr的回答對'draw.circle'中發生的事情有更好的敘述性描述,這是值得的。歡呼你倆。 – 2013-04-15 07:09:43