为什么我不能在主成分分析中使用箭头函数作为变量图? (用ggplot2)

时间:2022-09-06 08:08:01

I trying put arrows in my plot of variables in a principal components analysis (I use PCA function of a FactoMineR library) this:

我在主成分分析中尝试将箭头放在我的变量图中(我使用FactoMineR库的PCA函数):

# Generate random values for the data
data<-data.frame(runif(100,0,1),rnorm(100,2,9),runif(100,0,8),rnorm(100,10,25))
pca<-PCA(data,ncp=2,graph=F)

vPC1<-pca$var$coord[,1]
vPC2<-pca$var$coord[,2]
vlabs<-rownames(pca$var$coord)
vPCs<-data.frame(cbind(vPC1,vPC2))
rownames(vPCs)<-vlabs
colnames(vPCs)<-colnames(PCs)
circleFun<-function(center=c(0,0),diameter=1,npoints=100){
  r=diameter/2
  tt<-seq(0,2*pi,length.out = npoints)
  xx<-center[1]+r*cos(tt)
  yy<-center[2]+r*sin(tt)
  return(data.frame(x=xx,y=yy))
}
dat<-circleFun(c(0,0),2.3,npoints=100)
ggplot(dat,aes(x,y))+
  geom_path()+
  geom_text(data=vPCs,aes(x=vPC1*1.1,y=vPC2*1.1,label=rownames(vPCs)),size=3)+
  coord_fixed()+
  xlab("PC1")+
  ylab("PC2")+
  geom_segment(data=vPCs,aes(x=0,y=0,xend=vPC1,yend=vPC2),arrow=arrow(length=unit(0.5,"picas")),color="grey30")

I obtain the next error:

我得到了下一个错误:

Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, : could not find function "arrow"

do.call出错(“layer”,列表(mapping = mapping,data = data,stat = stat,:找不到函数“arrow”

I don't understand if I have installed the package "graphics" that contains the function "arrow".

我不明白我是否安装了包含“箭头”功能的包“图形”。

Thank you very much in advance.

非常感谢你提前。

1 个解决方案

#1


1  

Arrow is now in the grid package so you just need to load the grid library:

Arrow现在位于网格包中,因此您只需加载网格库:

library(grid)

Some of the confusion may be due to the fact that grid was loaded automatically by previous versions of ggplot (making grid functions visible/accessible to the user); now it's referred to via namespace imports instead, so you need to explicitly load grid if you want to use grid functions (or look at their help pages).

一些混乱可能是由于以前版本的ggplot自动加载网格(使网格功能对用户可见/可访问);现在它通过命名空间导入引用,因此如果要使用网格函数(或查看其帮助页面),则需要显式加载网格。

#1


1  

Arrow is now in the grid package so you just need to load the grid library:

Arrow现在位于网格包中,因此您只需加载网格库:

library(grid)

Some of the confusion may be due to the fact that grid was loaded automatically by previous versions of ggplot (making grid functions visible/accessible to the user); now it's referred to via namespace imports instead, so you need to explicitly load grid if you want to use grid functions (or look at their help pages).

一些混乱可能是由于以前版本的ggplot自动加载网格(使网格功能对用户可见/可访问);现在它通过命名空间导入引用,因此如果要使用网格函数(或查看其帮助页面),则需要显式加载网格。