将手动图例添加到ggplot [英] Adding manual legend to a ggplot

查看:61
本文介绍了将手动图例添加到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以生成绘图.我想知道如何在图的形成中包含图例,因为我似乎无法做到这一点.我想要的手动图例是"cols"向量,详细说明每种样本大小的不同颜色.

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

推荐答案

要获得图例,只需从五个中创建一个数据框,然后一步进行绘制即可,而不是在循环中添加层.

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.

我的方法使用 purrr :: map 来按样本大小设置数据集,并使用 dplyr :: bind_rows 绑定它们.在完成这些准备步骤之后,我们通过将样本大小映射到颜色上来自动获得图例.试试这个:

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

reprex软件包(v0.3.0)创建于2020-06-27

这篇关于将手动图例添加到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆