r - ggbiplot - 更改标签的颜色

标签 r ggplot2 pca ggbiplot princomp

默认情况下,ggbiplot 函数给出一个图形,其中载荷为红色箭头,单位标签为黑色:

library(ggbiplot)
data("USArrests")
us <- princomp(USArrests)
ggbiplot(us, labels = rownames(us$scores))

Here是该代码的结果

如何更改这些标签的颜色,以及它们的大小或字体?

最佳答案

library(ggbiplot)
library(grid)
data("USArrests")
us <- princomp(USArrests)

# Cut the third score into 4 intervals using quantiles
cols <- cut(us$scores[,3], quantile(us$scores[,3], probs=seq(0,1,0.25)), include.lowest=T)

# Change label colors using the "group" option
# Change label font size using the "label.size" option
p <- ggbiplot(us, labels = rownames(us$scores), groups=cols, labels.size=4)

# Change label font family 
g <- ggplotGrob(p)
g$grobs[[6]]$children[[4]]$gp$fontfamily <- "mono"
grid.draw(g)

enter image description here

要更改整体标签颜色:

p <- ggbiplot(us, labels = rownames(us$scores), groups=1, labels.size=4) +
     theme(legend.position = "none")

# Change label colors 
g <- ggplotGrob(p)
g$grobs[[6]]$children[[4]]$gp$col <- "#FF9900"
grid.draw(g)

enter image description here

关于r - ggbiplot - 更改标签的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818712/

相关文章:

r - svd(x, nu = 0) 中的错误 : infinite or missing values in 'x' (checked no negative values exist)

r - 从二项分布生成相关随机数

r - 更改填充颜色方向

javascript - 在 Google Earth Engine 中通过 imageCollection 对每个图像执行 PCA

r - 控制ggplot2中facet_grid/facet_wrap的顺序?

r - 使用ggplot和aes_string制作绘图函数

python - 应用PCA和聚类方法后,如何找到每个簇中数据的索引?

regex - 在R中使用正则表达式根据模式替换一些数字

r - 在 R 中,使用 & 与方括号 [][] 组合列有什么区别?

r - 为多个绘图设置默认选项