Q:

Adding manual legend to a ggplot

I have the following code that produces the plot attached. I am wondering how to include a legend in the formation of the graph as I can't seem to be able to do this. The manual legend I want is the "cols" vector, detailing the different colours for each sample size.
N <- 100
gamma <- 1/12 #scale parameter
beta <- 0.6 #shape parameter
u <- runif(N)
v <- runif(N)

tau <- -gamma*log(u)*(sin(beta*pi)/tan(beta*pi*v)-cos(beta*pi))^(1/beta)

OX <- sort(tau)
CumWealth <- cumsum(OX)/sum(tau)
PoorPopulation <- c(1:N)/N
index <- c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99,0.999,0.9999,0.99999,0.999999,1)*N
QQth <- CumWealth[index]
x <- PoorPopulation[index]
Lorenzdf <- data.frame(x, QQth)
cols <- c("100"="blue","1000"="green","10000"="red", "1e+05" = "black")
colors = c("green", "red", "black")
g <- ggplot(data=Lorenzdf, aes(x=x, y=QQth)) +
  geom_point(color = "blue") + 
  geom_line(color = "blue") +
  ggtitle(paste("Convergence of empirical Lorenz curve for Beta = ", beta, sep = " ")) + 
  xlab("Cumulative share of people from lowest to highest wealth") +
  ylab("Cumulative share of wealth") +
  scale_color_manual(name="Sample size",values=cols)

sample_sizes <- c(1000, 10000, 100000)
for (i in 1:3) {
    
gamma <- 1/12 #scale parameter
beta <- 0.6 #shape parameter
u <- runif(sample_sizes[i])
v <- runif(sample_sizes[i])

tau <- -gamma*log(u)*(sin(beta*pi)/tan(beta*pi*v)-cos(beta*pi))^(1/beta)

OX <- sort(tau)
CumWealth <- cumsum(OX)/sum(tau)
PoorPopulation <- c(1:sample_sizes[i])/sample_sizes[i]
index < c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99,0.999,0.9999,0.99999,0.999999,1)*sample_sizes[i]
    QQth <- CumWealth[index]
    x <- PoorPopulation[index]
    Lorenzdf <- data.frame(x, QQth)
  g <- g + geom_point(data = Lorenzdf, aes(x = x, y = QQth), color = colors[i])
  g <- g + geom_line(data = Lorenzdf, aes(x = x, y = QQth), color = colors[i])

}
g

A:

To get a legend simply make one dataframe out of your five and do the plotting in one step instead of adding layers in a loop.
My approach uses purrr::map to set up the datasets by sample size and bind them using dplyr::bind_rows. After these preparation steps we get the legend automatically by mapping sample size on color. Try this:
library(ggplot2)
library(dplyr)
library(purrr)

N <- 100
gamma <- 1/12 #scale parameter
beta <- 0.6 #shape parameter

make_lorenz <- function(N, gamma, beta) {
  u <- runif(N)
  v <- runif(N)
  
  tau <- -gamma*log(u)*(sin(beta*pi)/tan(beta*pi*v)-cos(beta*pi))^(1/beta)
  
  OX <- sort(tau)
  CumWealth <- cumsum(OX)/sum(tau)
  PoorPopulation <- c(1:N)/N
  index <- c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99,0.999,0.9999,0.99999,0.999999,1)*N
  QQth <- CumWealth[index]
  x <- PoorPopulation[index]
  data.frame(x, QQth)
}

sample_sizes <- c(100, 1000, 10000, 100000)

Lorenzdf <- purrr::map(sample_sizes, make_lorenz, gamma = gamma, beta = beta) %>% 
  setNames(sample_sizes) %>% 
  bind_rows(.id = "N")

cols <- c("100"="blue","1000"="green","10000"="red", "1e+05" = "black")

g <- ggplot(data=Lorenzdf, aes(x=x, y=QQth, color = N)) +
  geom_point() + 
  geom_line() +
  ggtitle(paste("Convergence of empirical Lorenz curve for Beta = ", beta, sep = " ")) + 
  xlab("Cumulative share of people from lowest to highest wealth") +
  ylab("Cumulative share of wealth") +
  scale_color_manual(name="Sample size",values=cols)
g

Created on 2020-06-27 by the reprex package (v0.3.0)

