1

I'm doing stochastic dominance analysis with diferent income distributions using Pen's Parade. I can plot a single Pen's Parade using Pen function from ineq package, but I need a visual comparison and I want multiple lines in the same image. I don't know how extract values from the function, so I can't do this.

I have the following reproducible example:

set.seed(123) 
x <- rnorm(100)
y <- rnorm(100, mean = 0.2) 
library(ineq) 
Pen(x) 
Pen(y)

I obtain the following plots:

https://i.stack.imgur.com/qXRBl.png https://i.stack.imgur.com/CaoFA.png

I want obtain sometime as the following:

       https://i.stack.imgur.com/0Zma2.png
M--
  • 25,431
  • 8
  • 61
  • 93
fcochaux
  • 135
  • 1
  • 13
  • 1
    `Pen(x); par(new=TRUE); Pen(y)` – M-- Apr 08 '19 at 16:01
  • Just a comment on how to get what data actually `Pen` plots. You can create the data used by the function by looking into the function body: https://stackoverflow.com/questions/19226816/how-can-i-view-the-source-code-for-a-function – M-- Apr 08 '19 at 17:24

2 Answers2

2

You can use add = TRUE:

set.seed(123) 
x <- rnorm(100)
y <- rnorm(100, mean = 0.2) 
library(ineq) 
Pen(x); Pen(y, add = TRUE)

enter image description here

From help("Pen"):

add      logical. Should the plot be added to an existing plot?

While the solution mentioned by M-M in the comments is a more general solution, in this specific case it produces a busy Y axis:

Pen(x)
par(new = TRUE) 
Pen(y)

enter image description here

I would generalize the advice for plotting functions in this way:

  1. Check the plotting function's help file. If it has an add argument, use that.
  2. Otherwise, use the par(new = TRUE) technique

Update

As M-M helpfully mentions in the comments, their more general solution will not produce a busy Y axis if you manually suppress the Y axis on the second plot:

Pen(x)
par(new = TRUE) 
Pen(y, yaxt = "n")

enter image description here

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • 1
    Using `par(new=T)` almost always requires setting one of the plot's axis to `NULL`. That's just the how it is. But anyhow, nice explanation. Cheers +1. – M-- Apr 08 '19 at 16:12
1

Looking at ?ineq::Pen() it seems to work like plot(); therefore, followings work for you.

Pen(x) 
Pen(y, add=T) 

Note: However, add=T cuts out part of your data since second plot has points which fall out of the limit of the first.

Update on using par(new=T):

Using par(new=T) basically means overlaying two plots on top of each other; hence, it is important to make them with the same scale. We can achieve that by setting the same axis limits. That said, while using add=T argument it is desired to set limits of the axis to not loose any part of data. This is the best practice for overlaying two plots.

Pen(x, ylim=c(0,38), xlim=c(0,1))
par(new=T)
Pen(y, col="red", ylim=c(0,38), xlim=c(0,1), yaxt='n', xaxt='n')
                 https://i.stack.imgur.com/ZtPTF.png

Essentially, you can do the same with add=T.

M--
  • 25,431
  • 8
  • 61
  • 93