2015-11-05 96 views
0

嘿,大家我希望有人在那裏可以幫我繪製一個定製的P在R在我的情況下,我試圖創建一個關於水質的圖形隨着時間的推移。監測水質的一種方法是使用secchi光盤,該光盤基本上是一個金屬圓盤,黑色和白色圓盤的四分之一(參見下圖)。自動調整自定義繪圖角色的大小?

到目前爲止,我已經能夠成功創建光盤並將其放置在情節中,但是根據情節的尺寸,我的問題伴隨着拉伸效果。

下面是我用創建至今光盤的功能:

# A circle function to draw a circle (pulled form another stackoverflow page) 

circleFun <- function(center=c(0,0), diameter=1, npoints=100, start=0, end=2, filled=TRUE){ 
    tt <- seq(start*pi, end*pi, length.out=npoints) 
    df <- data.frame(
    x = center[1] + diameter/2 * cos(tt), 
    y = center[2] + diameter/2 * sin(tt) 
) 
    if(filled==TRUE) { #add a point at the center so the whole 'pie slice' is filled 
    df <- rbind(df, center) 
    } 
    return(df) 
} 

# and my function to create a secchi disc 
secchiDisc = function(x,y,diameter = 1){ 
    quarterCircle1 = circleFun(c(x,y),diameter = diameter, start=0, end=0.5) 
    quarterCircle3 = circleFun(c(x,y),diameter = diameter, start=1, end=1.5) 
    fullCircle = circleFun(c(x, y), diameter, start=0, end=2) 
    polygon(quarterCircle1$x,quarterCircle1$y,col="black") 
    polygon(quarterCircle3$x,quarterCircle3$y,col="black") 
    polygon(fullCircle$x,fullCircle$y) 
} 

# make a plot to show what it looks like 
par(mar = c(5, 4, 4, 2) + 0.1) 
plot(0,0,pch = "") 
secchiDisc(0,0) 

enter image description here

# create data frame 
data = as.data.frame(list(Year = 1970:2015, 
          Depth = rnorm(46,3,1))) 

# and create a time series plot showing changes in depth over time 
plot(data$Year,data$Depth,pch="",ylim = c(7,0)) 
for(i in 1:nrow(data)){ 
    secchiDisc(data$Year[i],data$Depth[i]) 
} 



所以這裏是一個時間序列繪製時,它的外觀: enter image description here

A很明顯,我可以水平拉伸繪圖直到清理完成,但是我想知道有沒有人對如何根據情節自動讓光盤重新調整大小有任何建議? enter image description here

我試過自定義圓圈函數以允許拉伸效果,但無濟於事。我認爲「拉伸效應」方法存在的一個問題是,我仍然希望光盤呈圓形,但我無法獲得圓的直徑以獨立於x或y維度進行更改。

我的另一個想法是將secchi光盤的空白圖保存爲png文件並嘗試繪製導入的文件。關於這種方法的想法?

感謝您的任何幫助。

回答

1

我想我有一個解決方案。如果不是,我必須非常接近。問題在於,在您的功能中,x軸和y軸的直徑是相同的,與數據範圍無關。換句話說,Years軸的範圍是45,Depth軸的範圍是〜5。當您嘗試將相同直徑的單位應用於兩個軸時,圓的形狀會發生變形。我的解決方案是計算x軸和y軸範圍的比例。然後將該比率應用於circleFun中的sin行。我不確定這個比率是否應該除以sqrt(2),但它是有效的。另外,在繪製圖表後,您不應該手動調整繪圖窗口大小。在windows中使用win.graph或在plot中使用mac中的quartz()

# A circle function to draw a circle (pulled form another stackoverflow page) 

circleFun <- function(center=c(0,0), diameter=1, npoints=100, start=0, end=1, filled=TRUE,ratio=1){ 
    tt <- seq(start*pi, end*pi, length.out=npoints) 
    df <- data.frame(
    x = center[1] + diameter/2 * cos(tt), 
    y = center[2] + diameter/ratio/2 * sin(tt) 
) 
    if(filled==TRUE) { #add a point at the center so the whole 'pie slice' is filled 
    df <- rbind(df, center) 
    } 
    return(df) 
} 

# and my function to create a secchi disc 
secchiDisc = function(x,y,diameter = 1,ratio=1){ 
    quarterCircle1 = circleFun(c(x,y),diameter = diameter, start=0, end=0.5,ratio=ratio) 
    quarterCircle3 = circleFun(c(x,y),diameter = diameter, start=1, end=1.5,ratio=ratio) 
    fullCircle = circleFun(c(x, y), diameter, start=0, end=2,ratio=ratio) 
    polygon(quarterCircle1$x,quarterCircle1$y,col="black") 
    polygon(quarterCircle3$x,quarterCircle3$y,col="black") 
    polygon(fullCircle$x,fullCircle$y) 
} 

data = as.data.frame(list(Year = 1970:2015, 
          Depth = rnorm(46,3,1))) 

xx <-diff(range(data$Year)) 
yy <-diff(range(data$Depth)) 
ratio=(xx/yy)/sqrt(2) 
plot(data$Year,data$Depth,pch="") 
for(i in 1:nrow(data)){ 
    secchiDisc(data$Year[i],data$Depth[i],diameter = 1.5,ratio=ratio) 
} 

enter image description here

+0

這看起來太棒了。非常感謝你的幫助。 – anotherFishGuy