绘制双标图与R中GGPLOT2轴(Drawing biplot axes with ggplot2

2019-10-21 01:28发布

我试图模仿例如在GGPLOT2以下基图形(用于构建校准双标图轴):

## Base graphics
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)

到多行文字()的调用添加变量名的情节裕度和表示各轴的正方向。

然而,似乎没有成为一个GGPLOT2相当于基图形的多行文字()函数。 因此,我一直无法弄清楚如何把变量名中的情节的保证金。 有没有人对如何做到这一点的想法?

最好我不想禁用裁剪。 下面是上面的版本GGPLOT2一些等效代码:

## ggplot2 attempt
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)) 

在最后一行的geom_text()调用必须进行修改。

文章来源: Drawing biplot axes with ggplot2 in R
标签: r plot ggplot2