Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将文本添加到ggpairs()散点图?_R_Ggplot2_Scatter Plot_Ggally_Ggpairs - Fatal编程技术网

是否将文本添加到ggpairs()散点图?

是否将文本添加到ggpairs()散点图?,r,ggplot2,scatter-plot,ggally,ggpairs,R,Ggplot2,Scatter Plot,Ggally,Ggpairs,愚蠢但令人发狂的问题:如何在ggpairs(…)plot中向散点图点添加文本标签ggpairs(…)来自GGally库。正常的geom_text(…)函数似乎不是一个选项,因为它使用x、y参数和ggpairs创建了一个不同样式图的NxN矩阵 不显示数据,但假设我有一个名为“ID”的列,其中包含散点图中显示的每个点的ID 如果有帮助,很乐意添加数据,但不确定是否有必要。也许答案很简单,就是无法将文本标签添加到ggpairs(…) 注:添加标签仅供我个人参考。所以没必要告诉我这看起来像是一团糟。会

愚蠢但令人发狂的问题:如何在
ggpairs(…)
plot中向散点图点添加文本标签
ggpairs(…)
来自
GGally
库。正常的
geom_text(…)
函数似乎不是一个选项,因为它使用x、y参数和
ggpairs
创建了一个不同样式图的NxN矩阵

不显示数据,但假设我有一个名为“ID”的列,其中包含散点图中显示的每个点的ID

如果有帮助,很乐意添加数据,但不确定是否有必要。也许答案很简单,就是无法将文本标签添加到
ggpairs(…)

注:添加标签仅供我个人参考。所以没必要告诉我这看起来像是一团糟。会的。我只是想找出我的异常值


谢谢

这当然是可能的。查看
?GGally::ggpairs
的文档,有三个参数,
上限
下限
诊断
,从文档的详细信息可以看出:

上面和下面的列表可能包含变量“continuous”、“combo”、“discrete”和“na”。列表中的每个元素可以是函数或字符串

。。。(更多说明)

如果函数作为upper、lower或diag的选项提供,它应该实现
函数(数据、映射等){#make ggplot2 plot}
的函数api。如果特定函数需要设置其参数,
wrap(fn,param1=val1,param2=val2)

因此,“制作标签”的方法是覆盖绘图的默认值。例如,如果我们想在上面的三角形中写“hello world”,我们可以这样做:

library(ggplot2)
library(GGally)
#' Plot continuous upper function, by adding text to the standard plot
#' text is placed straight in the middle, over anything already residing there!
continuous_upper_plot <- function(data, mapping, text, ...){
  p <- ggally_cor(data, mapping, ...)
  if(!is.data.frame(text))
    text <- data.frame(text = text)
  lims <- layer_scales(p)
  p + geom_label(data = text, aes(x = mean(lims$x$range$range), 
                                  y = mean(lims$y$range$range), 
                                  label = text), 
                 inherit.aes = FALSE) 
}
ggpairs(iris, upper = list(continuous = wrap(continuous_upper_plot, 
                                                     text = 'hello world')))
库(ggplot2)
图书馆(GGALY)
#'通过向标准打印添加文本,打印连续上限函数
文字是笔直地放在中间,超过已经存在的任何东西!

连续上图这肯定是可能的。查看
?GGally::ggpairs
的文档,有三个参数,
上限
下限
诊断
,从文档的详细信息可以看出:

上面和下面的列表可能包含变量“continuous”、“combo”、“discrete”和“na”。列表中的每个元素可以是函数或字符串

。。。(更多说明)

如果函数作为upper、lower或diag的选项提供,它应该实现
函数(数据、映射等){#make ggplot2 plot}
的函数api。如果特定函数需要设置其参数,
wrap(fn,param1=val1,param2=val2)

因此,“制作标签”的方法是覆盖绘图的默认值。例如,如果我们想在上面的三角形中写“hello world”,我们可以这样做:

library(ggplot2)
library(GGally)
#' Plot continuous upper function, by adding text to the standard plot
#' text is placed straight in the middle, over anything already residing there!
continuous_upper_plot <- function(data, mapping, text, ...){
  p <- ggally_cor(data, mapping, ...)
  if(!is.data.frame(text))
    text <- data.frame(text = text)
  lims <- layer_scales(p)
  p + geom_label(data = text, aes(x = mean(lims$x$range$range), 
                                  y = mean(lims$y$range$range), 
                                  label = text), 
                 inherit.aes = FALSE) 
}
ggpairs(iris, upper = list(continuous = wrap(continuous_upper_plot, 
                                                     text = 'hello world')))
库(ggplot2)
图书馆(GGALY)
#'通过向标准打印添加文本,打印连续上限函数
文字是笔直地放在中间,超过已经存在的任何东西!

连续上标图非常有用的信息。添加
id
列作为散点图标签可以通过
ggpairs(df,lower=list(continuous=function(data,mapping,…)ggally_points(data,mapping,…)+geom_text(aes(label=id))
完成。答案。非常感谢。问这样一个蹩脚的问题让我感觉很糟糕,但这非常有帮助,我会确保与同事分享这些信息。当开始使用
GGally
时,这肯定不是直观的,但确实大大减少了工作量。很高兴我能提供帮助,并感谢@Paul为您的案例提供了一个具体的例子。:-)非常有用的信息。添加
id
列作为散点图标签可以通过
ggpairs(df,lower=list(continuous=function(data,mapping,…)ggally_points(data,mapping,…)+geom_text(aes(label=id))
完成。答案。非常感谢。问这样一个蹩脚的问题让我感觉很糟糕,但这非常有帮助,我会确保与同事分享这些信息。当开始使用
GGally
时,这肯定不是直观的,但确实大大减少了工作量。很高兴我能提供帮助,并感谢@Paul为您的案例提供了一个具体的例子。:-)