0

I'm trying to plot the below dataset using plot function. Im unable to plot both the graphs in same plot.

Using the dataset I tried to plot the graph.

m_bs = conpl$new(sample_data1$V1)
m_eq = conpl$new(sample_data2$V1)

est = estimate_xmin(m_bs, xmax=5e+5)
est_eq = estimate_xmin(m_eq, xmax=Inf)

m_bs$setXmin(est_bs)
m_eq$setXmin(est_eq)

plot(m_bs)
lines(m_bs)
d = plot(m_eq, draw =FALSE)
points(d$x, d$y, col=2)
lines(m_eq,col=2,lwd=2)

I got the below graph, it only shows one graph. Sorry for posting the question again I didnt get proper answer earlier post.

enter image description here

user128956
  • 123
  • 1
  • 14
  • It seems to me you get both plots but because you plot the one with the smaller x axis range first the data from the other data frame is outside the plotting region. You can see one red dot from the second plot. Try setting xlim on your first plot so it matches the scale of both datasets – ekstroem Sep 22 '15 at 07:01
  • You should post a reproducible example, too. You call functions not in base R. What packages did you use (i.e. add the `library` calls to your post). – hrbrmstr Sep 22 '15 at 10:22
  • i only imported library('poweRlaw') – user128956 Sep 22 '15 at 17:18

1 Answers1

1

I looked up the source code of the plot function used by poweRlaw and modified it:

lines_ <- function (x, y, ...) 
{
  .local <- function (x, cut = FALSE, draw = TRUE, ...) 
  {
    xmin = x$getXmin()
    cut_off = cut * xmin
    x_values = x$dat
    if (!cut) 
      x$setXmin(min(x_values))
    y = dist_data_cdf(x, lower_tail = FALSE, xmax = max(x_values) + 1)
    cut_off_seq = (x_values >= cut_off)
    x_axs = x_values[cut_off_seq]
    if (is(x, "discrete_distribution")) 
      x_axs = unique(x_axs)
    x$setXmin(xmin)
    x = x_axs
    if (draw) 
      lines(x, y, ...)
    invisible(data.frame(x = x, y = y))
  }
  .local(x, ...)
}

#----------------------------------------------------------

points_ <- function (x, y, ...) 
{
  .local <- function (x, cut = FALSE, draw = TRUE, ...) 
  {
    xmin = x$getXmin()
    cut_off = cut * xmin
    x_values = x$dat
    if (!cut) 
      x$setXmin(min(x_values))
    y = dist_data_cdf(x, lower_tail = FALSE, xmax = max(x_values) + 1)
    cut_off_seq = (x_values >= cut_off)
    x_axs = x_values[cut_off_seq]
    if (is(x, "discrete_distribution")) 
      x_axs = unique(x_axs)
    x$setXmin(xmin)
    x = x_axs
    if (draw) 
      points(x, y, ...)
    invisible(data.frame(x = x, y = y))
  }
  .local(x, ...)
}

The functions lines_ and points_

  • draw the same graph as the plot function of the poweRlaw package, but
  • behave like the standard lines and points functions in that they don't destroy the current graph.

First m_bs and 'm_eq' separately:

> plot(m_bs, lwd=9, col="black")

> lines_(m_bs, lwd=5, col="green")

enter image description here

> plot(m_eq, lwd=9, col="black")

> lines_(m_eq, lwd=5, col="blue")

enter image description here

The x-ranges of these to graphs do not overlap. Hence xlim has to be chosen appropriately to show both graphs in the same picture.

> plot( m_eq, lwd=8, col="black",
+       xlim=c(min(m_bs$dat,m_eq$dat),max(m_bs$dat,m_eq$dat)))

> lines_(m_eq, lwd=5, col="blue")

> points_(m_bs,lwd=8,col="black")

> lines_(m_bs, lwd=5, col="green")
> 

enter image description here

mra68
  • 2,960
  • 1
  • 10
  • 17