2013-04-10 74 views
0

時間軸我在尋找一個答案,一個非常具體的問題。 我發現在ggplot2的頁面上疊加一個函數的方法:http://docs.ggplot2.org/current/stat_function.html疊加在GGPLOT2函數與R中

但在我的情況下,我有一個在Y軸上使用時間的圖。 我的數據是:

dput(flN) 
structure(list(eCenter = c(52, 85, 141, 227, 645), eLow = c(42, 
64, 112, 178, 546), eHigh = c(65, 112, 178, 290, 761), arrivalTime = structure(c(957173699, 
957173635, 957173496, 957173418, 957173338), class = c("POSIXct", 
"POSIXt"), tzone = ""), timeError = c(6.436288, 2.075383, 1.321365, 
1.270163, 3.422232), vCenter = c(125839365.727275, 154297213.515671, 
186197068.826928, 216301111.588418, 268912290.324273), vLow = c(114601800.781488, 
137454241.369541, 171493844.86893, 201095521.048661, 262431046.389897 
), vHigh = c(138347098.798059, 171493844.86893, 201095521.048661, 
230862999.254391, 274537514.959924)), .Names = c("eCenter", "eLow", 
"eHigh", "arrivalTime", "timeError", "vCenter", "vLow", "vHigh" 
), row.names = c("E1'", "E2'", "E3'", "E4'", "FP5'"), class = "data.frame") 

它看起來像:

> flN 
    eCenter eLow eHigh   arrivalTime timeError vCenter  vLow  vHigh 
E1'  52 42 65 2000-05-01 10:34:59 6.436288 125839366 114601801 138347099 
E2'  85 64 112 2000-05-01 10:33:55 2.075383 154297214 137454241 171493845 
E3'  141 112 178 2000-05-01 10:31:36 1.321365 186197069 171493845 201095521 
E4'  227 178 290 2000-05-01 10:30:18 1.270163 216301112 201095521 230862999 
FP5'  645 546 761 2000-05-01 10:28:58 3.422232 268912290 262431046 274537515 

而其它量是:

m = 574.2538 
c = 3E8 
y0 = flN$arrivalTime - m*c/flN$vCenter 
y01 = y0[1] 

我試過的代碼是:

#this works and plots a part of what I want: 
p <- ggplot(flN, aes(x=c/vCenter, y=arrivalTime)) + geom_point(aes(y=arrivalTime)) + geom_errorbarh(aes(xmin=c/vHigh, xmax=c/vLow)) + xlim(0, 3) + ylim(as.POSIXct('2000/05/01 10:20'), as.POSIXct('2000/05/01 10:40')) 

# the function I want to plot. It's a simple straight line function 
test <- function(x) {y01 +m*x} 

# the code to plot this function over my previous plotted data that doesn't work: 
p + stat_function(fun = test, colour="#F8766D", linetype="dashed") 
> Error: Discrete value supplied to continuous scale 

現在,從錯誤,我相信問題必須是針對Y使用時間軸,但我該如何解決這部分問題?

+0

什麼是C符合Y0 = $ FLN arrivalTime - M * C /民族解放陣線$的vCenter - 它不是在你的代碼中定義。 – 2013-04-10 17:17:31

+0

@DidzisElferts不好意思啊,它只是在3E8米/秒的光速。我現在把它添加到了帖子中。 – jbssm 2013-04-10 17:18:13

回答

2

stat_function()似乎給出了錯誤,因爲它找不到在全局環境中定義的變量y01m,並計算其他值或給出某些不連續的值。解決這個問題的方法之一是通過增加y01m作爲參數,然後在stat_function()添加args=c(y01,m)修改test()功能 - 這將確保參數被發現和正確使用。我也是從你的ggplot()通話中移除ylim()(這是外部的數據區至少對我來說)。

test <- function(x,y01,m) {y01 +m*x} 

p <- ggplot(flN, aes(x=c/vCenter, y=arrivalTime)) + 
    geom_point(aes(y=arrivalTime)) + 
    geom_errorbarh(aes(xmin=c/vHigh, xmax=c/vLow)) + xlim(0, 3) 

p + stat_function(fun = test,args=c(y01,m), colour="#F8766D", linetype="dashed") 

enter image description here

+0

謝謝,這個作品完美!我以前的答案http://stackoverflow.com/questions/13174998/draw-a-function-in-ggplot2-with-more-than-x-as-parameter?rq=1得到的是一種黑客的的做它,但你的方法更簡單。 – jbssm 2013-04-12 11:09:54