Как построить две кривые Лоренца на одном участке? - PullRequest
0 голосов
/ 11 января 2019

Я хочу построить две кривые Лоренца на одном графике (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

Plot

1 Ответ

0 голосов
/ 12 января 2019

Вот минимальный воспроизводимый пример того, как отобразить несколько кривых на одном графике с помощью ggplot2. Ключевым моментом здесь является передача данных в «длинном виде» в функцию ggplot().

library(ggplot2)

# Construct example data.frame with separate columns for each curve.
x = seq(from=0, to=3, by=1)
dat = data.frame(x=x,
                 y1=9 * x,
                 y2=3 * x^2,
                 y3=x^3)

dat
#   x y1 y2 y3
# 1 0  0  0  0
# 2 1  9  3  1
# 3 2 18 12  8
# 4 3 27 27 27

# Convert data to long form, putting all y-values in a single column.
mdat = reshape2::melt(dat, id.vars="x", measure.vars=c("y1", "y2", "y3"))

mdat
#    x variable value
# 1  0       y1     0
# 2  1       y1     9
# 3  2       y1    18
# 4  3       y1    27
# 5  0       y2     0
# 6  1       y2     3
# 7  2       y2    12
# 8  3       y2    27
# 9  0       y3     0
# 10 1       y3     1
# 11 2       y3     8
# 12 3       y3    27

p = ggplot(data=mdat, aes(x=x, y=value, colour=variable, group=variable)) +
    geom_point() + 
    geom_line()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...