geom_rect不遵守for循环中的x和y限制

我正在尝试在geom_rect循环中使用for,但是它不遵守我的限制。如果我在for循环的上下文之外调用它,它将起作用。这是错误吗?还是我对geom_rect不了解? outPlot_freeoutPlot1应该相同(因为.2 = .2 / 1),但是outPlot1中的矩形被截断,有趣的是,它们与outPlot2相同,{ {1}}和outPlot3

outPlot4

geom_rect不遵守for循环中的x和y限制

library('ggplot2')
library('ggrepel')

sum_df <- data.frame(matrix(NA,nrow=10,ncol=3))
colnames(sum_df) <- c("Variable","Male","Female")
sum_df$Variable <- c("a","b","c","d","e","f","g","h","i","j")

covar = .7*.1*.1
Sigma = matrix(ncol=2,nrow=2,c(.2^2,covar,.2^2))
temp = eigen(Sigma)
SqrtSigma = temp$vectors%*%diag(sqrt(temp$values))%*%t(temp$vectors)
XYvec = c(0,0) + SqrtSigma%*%rnorm(2)

for(i in 1:10){
  XYvec = c(0,0) + SqrtSigma%*%rnorm(2)
  sum_df$Female[i] = XYvec[1]
  sum_df$Male[i] = XYvec[2]
}

outPlot_free <- ggplot(sum_df,aes(x=Male,y=Female)) + theme_minimal() + 
  geom_rect(aes(xmin=-.2,xmax=.2,ymin=-Inf,ymax=Inf),fill="grey97",color=NA,alpha=.5,size=0) +
  geom_rect(aes(ymin=-.2,ymax=.2,xmin=-Inf,xmax=Inf),size=0) +
  geom_point() + geom_text_repel(aes(label=Variable)) +
  scale_x_continuous(limits=c(-1,1),breaks=round(seq(-1,1,.1),digits=2)) + 
  scale_y_continuous(limits=c(-1,digits=2)) +
  geom_abline(intercept=0,slope=1,linetype="dotdash",alpha=.5) +
  scale_color_manual(values=c("grey60","black")) + xlab("Female") + ylab("Male") +
  geom_hline(yintercept=.2,linetype="dashed",color="slateblue") + geom_vline(xintercept=.2,color="slateblue") +
  geom_hline(yintercept=-.2,color="slateblue") + geom_vline(xintercept=-.2,color="slateblue") 

for (q in 1:4) {
  covar = .7*.1*.1
  Sigma = matrix(ncol=2,.2^2))
  temp = eigen(Sigma)
  SqrtSigma = temp$vectors%*%diag(sqrt(temp$values))%*%t(temp$vectors)
  XYvec = c(0,0) + SqrtSigma%*%rnorm(2)

  for(i in 1:10){
    XYvec = c(0,0) + SqrtSigma%*%rnorm(2)
    sum_df$Female[i] = XYvec[1]
    sum_df$Male[i] = XYvec[2]
  }


  outPlot <- ggplot(sum_df,y=Female)) + theme_minimal() + 
    geom_rect(aes(xmin=-.2/q,xmax=.2/q,size=0) +
    geom_rect(aes(ymin=-.2/q,ymax=.2/q,size=0) +
    geom_point() + geom_text_repel(aes(label=Variable)) +
    scale_x_continuous(limits=c(-1,digits=2)) + 
    scale_y_continuous(limits=c(-1,digits=2)) +
    geom_abline(intercept=0,alpha=.5) +
    scale_color_manual(values=c("grey60","black")) + xlab("Female") + ylab("Male") +
    geom_hline(yintercept=.2,color="slateblue") +
    geom_hline(yintercept=-.2,color="slateblue") 

  assign(paste0("outPlot",q),outPlot)
}

outPlot_free

geom_rect不遵守for循环中的x和y限制

outPlot1

geom_rect不遵守for循环中的x和y限制

outPlot2

geom_rect不遵守for循环中的x和y限制

outPlot3

geom_rect不遵守for循环中的x和y限制

reprex package(v0.3.0)于2019-11-09创建

outPlot4 outPlot_free除标绘点外应相同,因为它们是独立模拟的。

dianasry 回答:geom_rect不遵守for循环中的x和y限制

您遇到的问题是R中的惰性求值。这是编写包含循环的代码时的常见问题,特别是如果您从过程思维方式着手使用该语言。有关更多详细信息,请参见此处:http://adv-r.had.co.nz/Functions.html

在下面的示例中,第一个是您正在做的事情(实际上),第二个是您应该做的事情。

force()

reprex package(v0.3.0)于2019-11-09创建

在代码的上下文中,编写一个生成图的函数,在该函数的所有参数上调用for(),然后在library(ggplot2) library(cowplot) # this doesn't work,the line in the first plot should be placed # at y = 1 but is placed at y = 2 plots <- list() for (i in 1:2) { data <- data.frame(x = c(0,1)) plots[[i]] <- ggplot(data,aes(x,y = i)) + geom_line() + ylim(0,3) } plot_grid(plotlist = plots,labels = c(1:2)) 循环内调用该函数以创建特定图您需要的对象。请参见以下示例。

# this does work
plots <- list()
plot_fun <- function(i) {
  force(i)
  data <- data.frame(x = c(0,1))
  ggplot(data,3)
}
for (i in 1:2) {
  plots[[i]] <- plot_fun(i)
}

plot_grid(plotlist = plots,labels = c(1:2))

for

最后,一旦编写了生成曲线的函数,R中的惯用方法是不编写lapply()循环,而使用map()for。事实证明,如果您习惯于使用这些函数而不是# this replaces the for loop plots <- lapply(1:2,plot_fun) plot_grid(plotlist = plots,labels = c(1:2)) 循环,则由于R不是一种过程语言,因此您不太可能遇到所遇到的问题。

{{1}}

reprex package(v0.3.0)于2019-11-09创建

本文链接:https://www.f2er.com/3135126.html

大家都在问