如何从 ggplot2 获取 geom_vline 和facet_wrap 在函数内工作

发布于 2024-08-21 05:18:04 字数 1627 浏览 1 评论 0原文

我正在使用 ggplot2 来探索不同军事行动对谋杀率的影响。为了显示效果,我在手术发生时画了一条垂直线,并在手术前后画了一条平滑的谋杀率线。

我写了一个facet_wrap 图来显示一大堆县的情况。它工作得很好,但是当转换为函数时,在使用局部变量绘制垂直线时出现错误。

下面是一些示例代码:

drawTS <- function(df, dates, text) {
    p <- ggplot(df, aes(date, murders)) +
      facet_wrap(~ county, ncol = 1,
                 scale="free_y") +
      scale_x_date() +
      geom_smooth(aes(group = group), se = FALSE)
    for(i in 1:length(dates)) {
      #If it's not a global variable I get an object not found error
      temp[i] <<- dates[i]
      p <- p + geom_text(aes(x,y), label = text[i],
                  data = data.frame(x = dates[i], y = -10),
                  size = 3, hjust = 1, vjust = 0) +
           #Here's the problem
           geom_vline(xintercept=temp[i], alpha=.4)
    }
    p
}

library(ggplot2)
df <- data.frame(date = rep(seq(as.Date("2007/1/01"),
                          length=36, by='1 month'),4),
               murders = round(runif(36*4) * 100),
               county = rep(rep(factor(1:4),9),each=4),
               group = rep(c(rep(1,6), rep(2,12),rep(3,18))), each=4)
dates <- c(as.Date("2007/6/15"), as.Date("2008/6/15"))
temp <- c()
drawTS(df, dates, c("Op 1","Op 2"))

全局变量没有错误,但看起来很难看。

如果我在 geom_vline() 中使用 dates[i] 而不是 temp[i] 变量,我会得到以下结果:

NextMethod("[") 中的错误:未找到对象“i”

如果我将变量 dates[i] 包装在 aes() 中,我得到:

eval(expr, envir, enclos) 中出现错误:未找到对象“county”

有人知道如何解决这个问题吗?

I'm using ggplot2 to explore the effects of different military operations on murder rates. To show the effect I draw a vertical line when the operation occurred and a smoothed line of the murder rate before and after the operation.

I've written a facet_wrap plot to show this for a whole bunch of counties. It works beautifully, but when converted to a function I get an error when using a local variable to draw the vertical line.

Here's some example code:

drawTS <- function(df, dates, text) {
    p <- ggplot(df, aes(date, murders)) +
      facet_wrap(~ county, ncol = 1,
                 scale="free_y") +
      scale_x_date() +
      geom_smooth(aes(group = group), se = FALSE)
    for(i in 1:length(dates)) {
      #If it's not a global variable I get an object not found error
      temp[i] <<- dates[i]
      p <- p + geom_text(aes(x,y), label = text[i],
                  data = data.frame(x = dates[i], y = -10),
                  size = 3, hjust = 1, vjust = 0) +
           #Here's the problem
           geom_vline(xintercept=temp[i], alpha=.4)
    }
    p
}

library(ggplot2)
df <- data.frame(date = rep(seq(as.Date("2007/1/01"),
                          length=36, by='1 month'),4),
               murders = round(runif(36*4) * 100),
               county = rep(rep(factor(1:4),9),each=4),
               group = rep(c(rep(1,6), rep(2,12),rep(3,18))), each=4)
dates <- c(as.Date("2007/6/15"), as.Date("2008/6/15"))
temp <- c()
drawTS(df, dates, c("Op 1","Op 2"))

There's no error with the global variable, but it looks ugly.

If instead of the temp[i] variable I use dates[i] inside geom_vline(), I get this:

Error in NextMethod("[") : object 'i' not found

If I wrap the variable dates[i] in aes(), I get:

Error in eval(expr, envir, enclos) : object 'county' not found

Anybody know how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-08-28 05:18:04

我不知道是什么导致了错误,但我可以想出的解决方案是用这样的数据框替换 for 循环:

date.df<-data.frame(d=dates,t=text)
p <- p + geom_text(aes(x=d,label=t),y=0,
                   data = date.df,
                   size = 3, hjust = 1, vjust = 0)
p<-p+geom_vline(aes(xintercept=d),data=date.df,alpha=.4)

I don't know what is causing the error, but a fix that I could come up with is to replace the for loop with a data frame like this:

date.df<-data.frame(d=dates,t=text)
p <- p + geom_text(aes(x=d,label=t),y=0,
                   data = date.df,
                   size = 3, hjust = 1, vjust = 0)
p<-p+geom_vline(aes(xintercept=d),data=date.df,alpha=.4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文