首页 > 解决方案 > R - 仅在 ggplot2 的图例中更改形状

问题描述

我的情节目前看起来像这样:

阴谋

我想只为指示各自颜色的点更改图例中的形状(当前为“a”)。到目前为止,这是我的代码:

ggplot(data=pca2.data, aes(x=X, y=Y, label=Sample, colour = col)) +
geom_text() +
xlab(paste("PC1 - ", pca2.var.per[1], "%", sep="")) +
ylab(paste("PC2 - ", pca2.var.per[2], "%", sep="")) +
theme_bw() +
ggtitle("My PCA Graph") +
geom_hline(yintercept = 0) + 
geom_vline(xintercept = 0) +
scale_color_manual(values=c("black", "red", "green"), labels = c("No significant difference", "Sharpe Decrease", "Sharpe Increase")) +
theme(legend.position = 'bottom') + guides(color=guide_legend(""))

我已经尝试在“guide_legend”中添加“shape = c(20, 20, 20)”,但没有任何改变。

标签: rggplot2

解决方案


只需放置一个空点图层,不要绘制图例geom_text

由于您没有提供数据,因此我使用mtcars了数据集,但它应该可以转化为您的问题

ggplot(mtcars, aes(mpg, cyl, label=rownames(mtcars), color=factor(carb))) +geom_point(shape=NA)+
  geom_text( show.legend = F ) + guides(colour=guide_legend(override.aes = list(shape = 16)))

推荐阅读