Я хочу построить две кривые Лоренца на одном графике (data1 и data2), поэтому, пожалуйста, помогите мне с кодом?
У меня есть один код, который отлично работает с одной кривой (data1), но я хочу провести сравнение с этими кривыми (data1 и data2) на одном графике.
#Packages
library(ineq)
library(ggplot2)
library(scales)
library(grid)
#DATA SETS
set.seed(1)
data1<-sample(1000)
data2<-c(1000:2000) # I want to put this data set into second lorenz curve
# compute lorenz curve
lcolc <- Lc(data1)
# bring lorenz curve in another format easily readable by ggplot2
# namely reverse the L column so that lorenz curve is mirrored on diagonal
# p stays p (the diagonal)
# Uprob contains the indices of the L's, but we need percentiles
lcdf <- data.frame(L = rev(1-lcolc$L), p = lcolc$p, Uprob = c(1:length(lcolc$L)/length(lcolc$L)))
# basic plot with the diagonal line and the L line
p <- ggplot(lcdf, aes(y = Uprob, x = L)) + geom_line(colour = hcl(h=15, l=65, c=100)) + geom_line(aes(y = p, x = p))
# compute annotation lines at 50 percent L (uses a heuristic)
index <- which(lcdf$L >= 0.499 & lcdf$L <= 0.501)[1]
ypos <- lcdf$L[index]
yposs <- c(0,ypos)
xpos <- index/length(lcdf$L)
xposs <- c(0,xpos)
ypositions <- data.frame(y = xposs, x = c(ypos,ypos))
xpositions <- data.frame(y = c(xpos,xpos), x = yposs)
# add annotation line
p <- p + geom_line(data = ypositions, aes(x = x, y = y),
linetype="dashed") + geom_line(data = xpositions, aes(x = x, y = y),
linetype="dashed")
# set axes and labels (namely insert custom breaks in scales)
p <- p + scale_x_continuous(breaks=c(0, xpos,0.25,0.5,0.75,1),
labels = percent_format()) + scale_y_continuous(
labels = percent_format())
# add minimal theme
p <- p + theme_minimal() + xlab("Percent of Population") + ylab("Percent of Income")
# customize theme
p <- p + theme(plot.margin = unit(c(0.5,1,1,1), "cm"),
axis.title.x = element_text(vjust=-1),
axis.title.y = element_text(angle=90, vjust=0),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = rgb(0.99,0.99,0.99), linetype=0))
# print plot
p
