将变量传递给包含geom_vline和facetted plot的循环中的aes?

时间:2018-04-18 10:57:50

标签: r ggplot2

我学会了如何在here的分面图上放置垂直和水平线。现在我想修改代码,以便我可以使用变量传递xinterceptyintercept,但是,我认为我没有正确使用aesaes_

首先,让我们复制原始示例(截取略有不同:

# set up data
library(ggplot2)
tmp_intercepts <- data.frame(x = c(5, 3, 1))
iris$species_num <- as.numeric(iris$Species)
p <- 
    ggplot(iris, aes(Sepal.Length, Petal.Length)) + facet_wrap(~Species, scales="free") + geom_point()

## original plot works fine

for (i in 1:3) {


    if (i == 1) {
        p_orig <- 
            p +
            geom_vline(data=filter(iris, species_num == i), aes(xintercept=5), colour="pink")
    } else if (i == 2) {
        p_orig <- 
            p_orig +
            geom_vline(data=filter(iris, species_num == i), aes(xintercept=3), colour="blue")
    } else {
        p_orig <- 
            p_orig +
            geom_hline(data=filter(iris, species_num == i), aes(yintercept=1), colour="green") 


    }

}

我现在将制作两个图,一个使用带aes的循环,另一个使用aes_。两者都无法重现第一个图(查看垂直线的位置)。

## but once you try to pass in a variable it fails

for (i in 1:3) {


    if (i == 1) {
        p_loop <- 
            p +
            geom_vline(data=filter(iris, species_num == i), aes(xintercept=tmp_intercepts$x[i]), colour="pink")
    } else if (i == 2) {
        p_loop <- 
            p_loop +
            geom_vline(data=filter(iris, species_num == i), aes(xintercept=tmp_intercepts$x[i]), colour="blue")
    } else {
        p_loop <- 
            p_loop +
            geom_hline(data=filter(iris, species_num == i), aes(yintercept=tmp_intercepts$x[i]), colour="green") 


    }

}



## if you do it one by one it is not fine either!

tmp_x  <- 5
p_obo <- 
    p +
    geom_vline(data=filter(iris, species_num == 1), aes_(xintercept=quote(tmp_x)), colour="pink")

tmp_x <- 3
p_obo <- 
    p_obo +
    geom_vline(data=filter(iris, species_num == 2), aes_(xintercept=quote(tmp_x)), colour="blue")

tmp_x <- 1
p_obo <- 
    p_obo +
    geom_hline(data=filter(iris, species_num == 3), aes_(yintercept=quote(tmp_x)), colour="green") 

通过plot(gridExtra::arrangeGrob(p_orig, p_loop, p_obo, ncol = 1))

绘图,使原稿位于顶部

enter image description here

我认为我错误地使用了aes

2 个答案:

答案 0 :(得分:1)

当你想到它时 - 拦截只是非常狭窄的矩形。如果您不需要花式线型,那么您可以使用它们(此解决方案 删除循环部分):

# Create intercept data    
dataInt <- data.frame(Position = c(5, 3, 1),
                      Species  = levels(iris$Species),
                      Type     = c("x", "x", "y"))
#  Position    Species Type
#         5     setosa    x
#         3 versicolor    x
#         1  virginica    y

library(ggplot2)
ggplot() + 
    geom_point(data = iris, 
               aes(Sepal.Length, Petal.Length)) +
    geom_rect(data = dataInt,
              aes(xmin = ifelse(Type == "x", Position, -Inf), 
                  xmax = ifelse(Type == "x", Position, Inf), 
                  ymin = ifelse(Type == "x", -Inf, Position), 
                  ymax = ifelse(Type == "x", Inf, Position), 
                  color = Species),
              size = 0.5) +
    facet_wrap(~ Species, scales = "free") +
    theme(legend.position = "none")

enter image description here

答案 1 :(得分:0)

我的问题是我没有正确理解aes_ and aes_string require you to explicitly quote the inputs either with "" for aes_string(), or with quote or ~ for aes_()上的ggplot2 2. Set up. 2.4 Configuration options。具体来说,我遵循了aes()指令。

现在,在循环中调用变量或不起作用的原因是因为aes_ help page。实际上,tmp_x <- 5 p_f <- p + geom_vline(data=filter(iris, species_num == 1), aes_(xintercept=tmp_x), colour="pink") tmp_x <- 3 p_f <- p_f + geom_vline(data=filter(iris, species_num == 2), aes_(xintercept=tmp_x), colour="blue") tmp_x <- 1 p_f <- p_f + geom_hline(data=filter(iris, species_num == 3), aes_(yintercept=tmp_x), colour="green") plot(p_f) 是强制评估变量的方法。但是,如果变量位于全局环境中而不是传递给ggplot的数据框内,那么您似乎不必“引用”其名称。

以下产生与第一个相同的情节:

session.save_path

does not perform evaluation of its arguments