【问题标题】:representing the name of variables in a scatterplot表示散点图中变量的名称
【发布时间】:2017-06-14 11:35:43
【问题描述】:

我需要编写一个函数来绘制变量图。问题是它不打印变量的名称。

visual<-function( x , y){      
   df<-cbind(x,y)
   df<-scale(df, center = TRUE, scale = TRUE)
   df<-as.data.frame(df)
   ggpairs(df, columns=1:2,xlab = colnames(df)[1],ylab =colnames(df)[2])
}

如果我们有这些向量:

a <- c(128.095014,  71.430997,  88.704595,  48.180638)
b <- c(10.584888,  10.246740,   4.422322,   9.621246)
visual(a,b)

这有什么问题?

【问题讨论】:

  • ggpairs 来自哪里?是从包裹里拿出来的吗?

标签: r plot ggplot2


【解决方案1】:

您可以使用substitute 来获取传递给您的函数的对象的名称。

visual<-function(x, y){
  xname <- substitute(x)
  yname <- substitute(y)

  df<-cbind(x,y)

  df<-scale(df, center = TRUE, scale = TRUE)

  df<-as.data.frame(df)

  names(df) <- c(xname, yname)

  GGally::ggpairs(df, columns=1:2, xlab = colnames(df)[1], ylab =colnames(df)[2])
}

b<-c(128.095014,  71.430997,  88.704595,  48.180638)
a<-c(10.584888,  10.246740,   4.422322,   9.621246)

visual(a,b)

输出

【讨论】:

  • 非常感谢。太棒了。
猜你喜欢
  • 2017-11-22
  • 2020-09-11
  • 2016-08-05
  • 2018-07-15
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
相关资源
最近更新 更多