2015-08-21 21 views
0

假設我有一個名爲my_plot的保存繪圖,用ggplot生成。此外,讓我們說,用於水平軸my_plot[[1]]數據幀中的列被命名爲my_dates在函數中添加參數到ggplot生成的繪圖

現在,我想一些垂直線添加到情節,其中,當然,可以通過什麼來實現這樣的:

my_plot + 
    geom_vline(aes(xintercept = my_dates[c(3, 8)])) 

因爲我相當定期執行此任務,我想要寫一個函數 - 這樣的事情:

ggplot.add_lines <- function(given_plot, given_points) { 
    finale <- given_plot + 
     geom_vline(aes(xintercept = given_plot[[1]]$my_dates[given_points])) 
    return(finale) 
} 

其中,因爲它可能是有目共睹的,不工作:

> ggplot.add_lines(my_plot, c(3, 5)) 
Error in eval(expr, envir, enclos) : object 'given_plot' not found 

所以,我的問題是我做錯了什麼,以及如何解決它?下面是一個重複的例子,一些數據:

> dput(my_plot) 
structure(list(data = structure(list(my_dates = c(1, 2, 3, 4, 
5, 6, 7, 8, 9, 10), my_points = c(-2.20176409422924, -1.12872396340683, 
-0.259703895194354, 0.634233385649338, -0.678983982973015, -1.83157126614836, 
1.33360095418957, -0.120455389285709, -0.969431974863616, -1.20451262626184 
)), .Names = c("my_dates", "my_points"), row.names = c(NA, -10L 
), class = "data.frame"), layers = list(<environment>), scales = <S4 object of class structure("Scales", package = "ggplot2")>, 
mapping = structure(list(x = my_dates, y = my_points), .Names = c("x", 
"y"), class = "uneval"), theme = list(), coordinates = structure(list(
    limits = structure(list(x = NULL, y = NULL), .Names = c("x", 
    "y"))), .Names = "limits", class = c("cartesian", "coord" 
)), facet = structure(list(shrink = TRUE), .Names = "shrink", class = c("null", 
"facet")), plot_env = <environment>, labels = structure(list(
    x = "my_dates", y = "my_points"), .Names = c("x", "y" 
))), .Names = c("data", "layers", "scales", "mapping", "theme", 
"coordinates", "facet", "plot_env", "labels"), class = c("gg", 
"ggplot")) 

回答

2

this post,下面是我解決這個問題。 ** ply和ggplot中的環境問題令人討厭。

ggplot.add_lines <- function(given_plot, given_points) { 
    finale <- eval(substitute(expr = {given_plot + 
     geom_vline(aes(xintercept = my_dates[given_points]))}, env = list(given_points = given_points))) 
    return(finale) 
} 

以下代碼在我的機器上運行良好。 (我不能讓你的重複性工作,我的機器上...)

df <- data.frame(my_dates = 1:10, val = 1:10) 
my_plot <- ggplot(df, aes(x = my_dates, y = val)) + geom_line() 
my_plot <- ggplot.add_lines(my_plot, c(3, 5)) 
print(my_plot) 

更新:當使用兩個以上的點,上述解決方案將失敗。

看來,我們可以很容易地通過不包括aes(子集與aes導致問題一起)解決了這個問題:

ggplot.add_lines <- function(given_plot, given_points) {   
    finale <- given_plot + geom_vline(xintercept = given_plot[[1]]$my_dates[given_points]) 
    return(finale) 
} 

enter image description here

+0

感謝您的幫助和關心。但是你知道嗎,如果使用了兩個以上的點,它爲什麼會拋出一個錯誤,比如'c(3,5,7)':'data.frame錯誤(xintercept = c(3L,5L,6L),PANEL = c(1L,1L,1L,: 參數意味着不同的行數:3,10' –

+0

嗨,AS,更新的部分應該可以解決這個問題 –

+1

我不太確定第一個答案的問題,但你提供的代碼'my_plot + + geom_vline(aes(xintercept = my_dates [c(3,7,8)]))'不適用於三點以上...問題可能是由於在' aes()'。 –

1

我將採取以下措施:提取數據。並將其傳遞到新圖層,並將其傳遞到新層,

df <- data.frame(my_dates = 1:10, val = rnorm(10)) 
my_plot <- ggplot(df, aes(x = my_dates, y = val)) + geom_line() 

add_lines <- function(p, given_points=c(3,5), ...){ 
    d <- p[["data"]][given_points,] 
    p + geom_vline(data = d, aes_string(xintercept="my_dates"), ...) 
} 

add_lines(my_plot, c(3,5), lty=2) 

enter image description here

+0

是的,我已經完全忘記了'data'選項!謝謝! –