R:在ggplot2中将校准轴添加到PCA双标图中

时间:2015-08-12 14:03:43

标签: r plot ggplot2 ggbiplot

我正在使用ggplot2制作一个受戒指套餐。现在我正在以传统方式构建biplots,负载用箭头表示。我也感兴趣的是使用校准轴并将加载轴表示为通过原点的线,并且加载标签显示在绘图区域外。在基础R中,这是在

中实现的
library(OpenRepGrid)
biplot2d(boeker)

enter image description here

但我正在寻找ggplot2解决方案。有人会想到如何在ggplot2中实现这样的目标吗?在绘图区域之外添加变量名称可以like here我想,但是如何绘制绘图区域外的线段?

目前我所拥有的是

install.packages("devtools")
library(devtools)
install_github("fawda123/ggord")
library(ggord)
data(iris)
ord <- prcomp(iris[,1:4],scale=TRUE)
ggord(ord, iris$Species)

enter image description here

加载位于ord$rotation

                    PC1         PC2        PC3        PC4
Sepal.Length  0.5210659 -0.37741762  0.7195664  0.2612863
Sepal.Width  -0.2693474 -0.92329566 -0.2443818 -0.1235096
Petal.Length  0.5804131 -0.02449161 -0.1421264 -0.8014492
Petal.Width   0.5648565 -0.06694199 -0.6342727  0.5235971

如何通过原点,外部刻度线和轴区域外的标签添加线条(可能包括上面为重叠标签应用的冷抖动)?

注意我不想关闭剪辑,因为我的一些情节元素有时可能会超出边界框

编辑:Someone else apparently asked a similar question before,虽然问题仍然没有答案。它指出在基础R中做这样的事情(虽然以一种丑陋的方式),人们可以做到这一点。

plot(-1:1, -1:1, asp = 1, type = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
abline(a = 0, b = -0.75)
abline(a = 0, b = 0.25)
abline(a = 0, b = 2)
mtext("V1", side = 4, at = -0.75*par("usr")[2])
mtext("V2", side = 2, at = 0.25*par("usr")[1])
mtext("V3", side = 3, at = par("usr")[4]/2)

enter image description here

ggplot2中最小的可行示例

library(ggplot2)
df <- data.frame(x = -1:1, y = -1:1)
dfLabs <- data.frame(x = c(1, -1, 1/2), y = c(-0.75, -0.25, 1), labels = paste0("V", 1:3))
p <- ggplot(data = df, aes(x = x, y = y)) +  geom_blank() +
  geom_abline(intercept = rep(0, 3), slope = c(-0.75, 0.25, 2)) +
  theme_bw() + coord_cartesian(xlim = c(-1, 1), ylim = c(-1, 1)) +
  theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(),
        panel.grid = element_blank())
p + geom_text(data = dfLabs, mapping = aes(label = labels))

enter image description here

但是你可以看到标签没有运气,我正在寻找一种不需要关闭剪辑的解决方案。

EDIT2:相关问题的一点是我如何在X轴顶部和Y轴右侧添加自定义中断/刻度标记和标签(例如红色),以显示因子载荷的坐标系? (如果我将它相对于因子分数进行缩放以使箭头更清晰,通常与单位圆相结合)

1 个答案:

答案 0 :(得分:6)

也许作为替代方案,您可以完全删除默认的面板框和轴,并在绘图区域中绘制一个较小的矩形。剪切线条不与文本标签冲突有点棘手,但这可能有效。

enter image description here

df <- data.frame(x = -1:1, y = -1:1)
dfLabs <- data.frame(x = c(1, -1, 1/2), y = c(-0.75, -0.25, 1), 
                     labels = paste0("V", 1:3))
p <- ggplot(data = df, aes(x = x, y = y)) +  
  geom_blank() +
  geom_blank(data=dfLabs, aes(x = x, y = y)) +
  geom_text(data = dfLabs, mapping = aes(label = labels)) +
  geom_abline(intercept = rep(0, 3), slope = c(-0.75, 0.25, 2)) +
  theme_grey() +
  theme(axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks = element_blank(),
        panel.grid = element_blank()) + 
  theme()

library(grid)
element_grob.element_custom <- function(element, ...)  {
  rectGrob(0.5,0.5, 0.8, 0.8, gp=gpar(fill="grey95"))
}

panel_custom <- function(...){ # dummy wrapper
  structure(
    list(...), 
    class = c("element_custom","element_blank", "element") 
  ) 

}

p <- p + theme(panel.background=panel_custom())


clip_layer <- function(g, layer="segment", width=1, height=1){
  id <- grep(layer, names(g$grobs[[4]][["children"]]))
  newvp <- viewport(width=unit(width, "npc"), 
                    height=unit(height, "npc"), clip=TRUE)
  g$grobs[[4]][["children"]][[id]][["vp"]] <- newvp

  g
}

g <- ggplotGrob(p)
g <- clip_layer(g, "segment", 0.85, 0.85)
grid.newpage()
grid.draw(g)