2

I have a data which can be download from here https://gist.github.com/anonymous/5f1135e4f750a39b0255

I try to plot a PCA with ggbiplot using the following function

data <- read.delim("path to the data.txt")
data.pca <- prcomp (data, center = TRUE, scale =TRUE)
library(ggbiplot)
g <- ggbiplot(data.pca, obs.scale =1, var.scale=1, ellipse = TRUE, circle=TRUE)
g <- g + scale_color_discrete(name='')
g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')
print(g)

however, it is very difficult to see the biplot lines names,

is there any way to make it more clear or show it better ?

4

1 に答える 1

1

varname.sizeより明確にする方法は、引数と引数を使用してラベルのサイズと位置を調整することだと思いvarname.adjustます。ただし、多くの変数があるため、まだ混み合っているように見えます。矢印の長さを長くすると( に似てstats::biplot()います)、見栄えが多少良くなります(imo)

# install ggbiplot
#require(devtools)
#install_github('ggbiplot','vqv')

library(httr) 
library(ggbiplot)

# read data
url <- "https://gist.githubusercontent.com/anonymous/5f1135e4f750a39b0255/raw/data.txt"
dat <- read.table(text=content(GET(url), as="text"), header=TRUE)

# pca 
data.pca <- prcomp (dat, center = TRUE, scale =TRUE)

# original plot + increase labels size and space from line
p <- ggbiplot(data.pca, obs.scale=1, 
              var.scale=1, circle=F, 
              varname.size=4, varname.adjust=2)  
p

ここに画像の説明を入力

# use coord_equal() to change size ratio of plot (excludes use of circle)
p <- p + coord_equal(1.5) + theme_classic()
p

ここに画像の説明を入力

矢印を拡張するには、x 座標と y 座標を再計算する必要があります。これらを使用して、関連するグロブを編集し、他のパラメーター (色、サイズ、回転など) を変更できます。ggplotGrob(p)(アプローチ全体を使用できますが、以下を使用しgrid.edit()てください。)

# function to rescale the x & y positions of the lines and labels
f <- function(a0, a1, M=M)
      {
      l <- lapply(as.list(environment()), as.numeric)
      out <- M* (l$a1 - l$a0) + l$a0
      grid::unit(out, "native")
      }  

# get list of grobs in current graphics window
grobs <- grid.ls(print=FALSE)  

# find segments grob for the arrows
s_id <- grobs$name[grep("segments", grobs$name)]

# edit length and colour of lines
seg <- grid.get(gPath(s_id[2]))     
grid.edit(gPath(s_id[2]),  
            x1=f(seg$x0, seg$x1, 2), 
            y1=f(seg$y0, seg$y1, 2),
            gp=gpar(col="red"))


# find text grob for the arrow labels
lab_id <- grobs$name[grep("text", grobs$name)]

# edit position of text, and rotate and colour labels
seg2 <- grid.get(gPath(lab_id)) 
grid.edit(gPath(lab_id),  
            x=f(seg$x0, seg2$x, 2), 
            y=f(seg$y0, seg2$y, 2),
            rot=0,
            gp=gpar(col="red"))

ここに画像の説明を入力

これにより改善されるかどうかは主観的であり、おそらくbiplot()新しい関数を使用または定義するだけの方が簡単です

于 2015-05-27T22:28:33.960 に答える