简体   繁体   English

R - 仅在 ggplot2 的图例中更改形状

[英]R - Changing shape only in legend in ggplot2

My plot currently looks like this:我的 plot 目前看起来像这样:

阴谋

I want to change the shape in the legend (which is currently "a") for only points that indicate the respective colours.我想只为指示各自颜色的点更改图例中的形状(当前为“a”)。 This is my code so far:到目前为止,这是我的代码:

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(""))

I already tried adding "shape = c(20, 20, 20)" inside of "guide_legend", but it changed nothing.我已经尝试在“guide_legend”中添加“shape = c(20, 20, 20)”,但没有任何改变。

Just put an empty point layer and don't plot legend for geom_text只需放置一个空点图层,不要 plot 图例为geom_text

As you didn't provide data, I've used mtcars dataset but it should translate to your problem由于您没有提供数据,因此我使用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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM