0

ggbiplot经常使用并且可以控制使用工具生成的情节的各个方面ggplot2,因为它继承自ggplot2...

ggplot2中,我通常用一行形式控制图例中的列数:

ggplot2::guides(fill=ggplot2::guide_legend(ncol=2))

但是,这似乎不起作用ggbiplot(而其他所有ggplot2相关的东西都起作用)。

请用数据检查下面的 MWE iris,我在这里唯一想做的就是为图例指定 2 列(为了说明目的,我知道只有 3 个物种级别,但这是我手头更多的示例)。

library(ggbiplot)
data(iris)
pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
P <- ggbiplot(pca.obj,
              obs.scale = 1,
              var.scale=1,
              ellipse=T,
              circle=F,
              varname.size=3,
              var.axes=T,
              groups=iris$Species, #no need for coloring, I'm making the points invisible
              alpha=0) + #invisible points, I add them below
ggplot2::theme_light() +
ggplot2::scale_color_manual("spec", values=c("red","black","pink"), guide=ggplot2::guide_legend(override.aes=list(shape=19, size=5, linetype=0))) +
ggplot2::guides(fill=ggplot2::guide_legend(ncol=2)) #THIS DOES NOT WORK HERE, WHY?
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test.png", height=600, width=600)
print(#or ggsave()
  P
)
dev.off()

这会产生以下双标图:

测试

看看图例中的列数如何永远不会改变......有没有办法指定图例中的列数ggbiplot?谢谢

4

1 回答 1

1

ncol = 2在第一次调用中包含guide_legend(). IE

library(ggbiplot)
data(iris)
pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
P <- ggbiplot(pca.obj,
              obs.scale = 1,
              var.scale=1,
              ellipse=T,
              circle=F,
              varname.size=3,
              var.axes=T,
              groups=iris$Species, #no need for coloring, I'm making the points invisible
              alpha=0) + #invisible points, I add them below
ggplot2::theme_light() +
ggplot2::scale_color_manual("spec", values=c("red","black","pink"), guide=ggplot2::guide_legend(ncol = 2, override.aes=list(shape=19, size=5, linetype=0))) # +
# ggplot2::guides(fill=ggplot2::guide_legend(ncol=2)) #THIS DOES NOT WORK HERE, WHY?
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test.png", height=600, width=600)
print(#or ggsave()
  P
)
dev.off()

在此处输入图像描述

正如 Henrik 所说,这归结为 和 之间的不fill = 匹配color =

于 2020-07-23T10:00:29.040 回答