简体   繁体   中英

R - Changing shape only in legend in ggplot2

My plot currently looks like this:

阴谋

I want to change the shape in the legend (which is currently "a") for only points that indicate the respective colours. 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.

Just put an empty point layer and don't plot legend for geom_text

As you didn't provide data, I've used mtcars dataset but it should translate to your problem

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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