Adding legend to scatter plot

Hi All,

I would like to add the legend to a scatter plot of mine. I have tried with the following code but it is not working:

# built scatter plot 
install.packages("ggplot2")
library(ggplot2)

Chart = ggplot( Combination_of_certificates, aes( x = Portfolio_Volatility, y = Portfolio_Yield ) ) +
  geom_point() + 
  geom_point(data=Portfolio_1, aes(x = Portfolio_Volatility, y = Portfolio_Yield ), color = 'red',size=2) +
  geom_point(data=Portfolio_2, aes(x = Portfolio_Volatility, y = Portfolio_Yield ), color = 'blue',size=2) +
  geom_point(data=Portfolio_3, aes(x = Portfolio_Volatility, y = Portfolio_Yield ), color = 'green',size=2) +
  legend("bottom",c(Portfolio_1,Portfolio_2,Portfolio_3),cex=.8,col=c("red","blue","green"),pch=c(1,2,3))

# Install and load formattable
install.packages("formattable")
library("formattable")

Chart + expand_limits(x=0, y=0) + scale_y_continuous(labels = percent) + scale_x_continuous(labels = percent)

That is, I have a scatter plot of many stocks portfolios in the Volatility - Return chart, with the 3 optimized portfolios highlighted. I would like to add the legend for these 3 portfolios, but it doesn't work.

Could someone know what I'm doing wrong?

Many thanks!

You are mixing base r plotting function (legend) with ggplot2 syntax but they are not compatible.
I ggplot2 there is guide_legend and other things.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.