r中使用ggplot传递变量的函数(Passing variables to a function

2019-10-28 15:59发布

我已经提供了做这在下文中所示的PCA曲线的函数。 我想通过主成分(变量a和b)的名字,但还没有想出一个办法做到这一点。 我一直在使用aes_string尝试。 接收到的错误是在错误的eval(expr中,ENVIR,enclos):对象” .names'未找到

每下面的建议,我已经把一个具体的例子。 其中一个功能的工作原理和其他地方没有。 目标是将变量传递给这个函数。

 #data
  d = iris[1:4]
  #########################################################################
  # PCA_Plot functions
  #########################################################################

  PCA_Plot = function(pcaData)
  {
    library(ggplot2)

    theta = seq(0,2*pi,length.out = 100)
    circle = data.frame(x = cos(theta), y = sin(theta))
    p = ggplot(circle,aes(x,y)) + geom_path()

    loadings = data.frame(pcaData$rotation, .names = row.names(pcaData$rotation))
    p + geom_text(data=loadings, mapping=aes(x = PC1, y = PC2, label = .names, colour = .names, fontface="bold")) +
      coord_fixed(ratio=1) + labs(x = "PC1", y = "PC2")
  }
  # non-working function with two extra variables
  PCA_Plot2 = function(pcaData, var1, var2)
  {
    library(ggplot2)

    theta = seq(0,2*pi,length.out = 100)
    circle = data.frame(x = cos(theta), y = sin(theta))
    p = ggplot(circle,aes(x,y)) + geom_path()

    loadings = data.frame(pcaData$rotation, .names = row.names(pcaData$rotation))
    p + geom_text(data=loadings, mapping=aes(x = var1, y = var2, label = .names, colour = .names, fontface="bold")) +
      coord_fixed(ratio=1) + labs(x = var1, y = var2)
  }


  #pca
  library(stats)
  p = prcomp(d)

  PCA_Plot(p) #works

  PCA_Plot2(p, "PC1", "PC2") # ERROR Error: Discrete value supplied to 
  continuous scale

Answer 1:

你几乎有与aes_string ! 您提供var1var2为字符串,而不是label审美.names 。 如果您更改至".names" ,你的函数将工作。

我改变你的函数一点:

  1. 返回ggplot对象,而不是直接分配一个中间变量
  2. 合并后的绘图调用
  3. 移动功能(无需加载每次调用该函数时库)外的库调用。

     library(stats) library(ggplot2) PCA_Plot2 = function(pcaData, var1, var2) { theta = seq(0,2*pi,length.out = 100) circle = data.frame(x = cos(theta), y = sin(theta)) loadings = data.frame(pcaData$rotation, .names = row.names(pcaData$rotation)) ggplot(circle, aes(x,y)) + geom_path() + geom_text(aes_string(x = var1, y = var2, label = ".names"), inherit.aes = FALSE, data = loadings) + coord_fixed(ratio=1) + labs(x = var1, y = var2) } PCA_Plot2(p, "PC1", "PC2") 



Answer 2:

无需您的数据,这是一个使用了一些基础R数据集的功能的一个简单的例子。 我觉得你有一个quosure问题:你不能直接只需使用对应于列名的字符串来访问列。 相反,你要使用类似q_var <- quo(var)然后!!q_var在其裸露的名字指的是列。 我有像这样的书写功能麻烦了相当数量,但看到小插曲“与dplyr编程”更多的解释。

library(tidyverse)

make_plot <- function(full_df, var1, var2) {
    # use quo to access the values in the strings var1, var2
    quo_var1 <- quo(var1)
    quo_var2 <- quo(var2)

    df <- full_df %>% rownames_to_column("name") %>% select(name, x = !!quo_var1, y = !!quo_var2)

    ggplot(df, aes(x = x, y = y)) +
        geom_text(aes(label = name), size = 2.5) +
        # still have var1, var2 strings--can use as labels
        labs(x = var1, y = var2)
}

make_plot(mtcars, "mpg", "disp")

make_plot(mtcars, "hp", "qsec")

make_plot(as.data.frame(state.x77), "Income", "Life Exp")

由创建于二零一八年四月二十零日reprex包 (v0.2.0)。



文章来源: Passing variables to a function using ggplot in r
标签: r ggplot2