ggplot2 legend not showing up

Viewed 74

Could someone help me figure out how to put a legend in the bottom right corner related to the red dot? I just need the name "My Portfolio" and a red indicator. Im not sure why no legend is showing up to begin with

library(ggplot2)


x <- c(1,2,3)
y <- c(4,5,6)

ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
  geom_line() +
  geom_point(aes(2.5,5.5, colour = "red"), 
             shape = 18, 
             size = 3) +
  ggtitle("Efficient Frontier") +
  xlab("Volatility (Weekly)") +
  ylab("Expected Returns (Weekly)") +
  theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
        axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
        axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        legend.position = "bottomright")

Thank you

1 Answers

Use legend.justification:

library(tidyverse)
ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
  geom_line() +
  geom_point(aes(2.5,5.5,  colour = "red"), 
             shape = 18, 
             size = 3) +
  ggtitle("Efficient Frontier") +
  xlab("Volatility (Weekly)") +
  ylab("Expected Returns (Weekly)") +
  theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
        axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
        axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        legend.position = "bottom", 
        legend.justification = "right")

enter image description here

Related