Add a grid to an "ecdfplot" in R - r

I am using the latticeExtra library "ecdfplot" to plot my error. I want to add gridlines.
The following does not seem to work:
ecdfplot(err)
grid(ny=10)
It gives the following (gridless) result:
I really would love to give a "graphical summary" where the quantiles are indicated by lines, and their intersections with the data are shown on the x-axis.
Can you tell me how to add gridlines?
How about adding vertical lines at a particular x-location?

Try the argument axis = axis.grid
require(latticeExtra)
data(singer, package = "lattice")
ecdfplot(~height, data = singer, add=TRUE, axis = axis.grid, par.settings = theEconomist.theme())

Related

Multiple Pen's Parade Graphs on the same Plot

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:
I want obtain sometime as the following:
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)
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)
I would generalize the advice for plotting functions in this way:
Check the plotting function's help file. If it has an add argument, use that.
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")
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')
Essentially, you can do the same with add=T.

R icenReg package: Move plot legend for ic_np fit

I need to create a plot that compares interval censored survival curves for three species. I am able to generate a plot that shows all three curves using the ic_np function in the icenReg package in R. When I plot the output of this ic_np fit using base R plot(), a legend appears in the bottom left corner.
This example from the icenReg package documentation yields a similar figure:
library(icenReg)
data(miceData)
fit <- ic_np(cbind(l, u) ~ grp, data = miceData) #Stratifies fit by group
plot(fit)
However, having the caption in the bottom left covers the most interesting comparison of my survival curves, so I would like to move the legend to the top right.
I have seen this question about setting a legend position for basic plots in base R. Answers to this question seem to assume that I can generate a plot without the legend, but I have not been able to do that.
I have also seen this question about adding a legend to other types of survival analysis that do not seem to generate a legend by default, but I have not been able to implement these methods with interval censored data.
I have read that I can't move a legend that has already been added to a plot, but I don't know how to generate this particular plot without a legend so that I can add one back in where I want it (top right).
How can I either (a) generate this plot of interval censored Kaplan-Meier survival curves using ic_np without a legend -- maybe using some hidden parameter of plot() -- OR (b) generate this figure using a different plotting device, assuming the plot legend is then moveable?
There doesn't seem to be a help page in the package for the plot function so you need to determine the class of the fit-object and look at the code:
class(fit)
#[1] "ic_npList"
#attr(,"package")
#[1] "icenReg"
plot.ic_npList
#Error: object 'plot.ic_npList' not found
So it's not exported and we need to dig deeper (not suprising since exported functions do need to have help pages.)
getAnywhere(plot.ic_npList)
#-----------
A single object matching ‘plot.ic_npList’ was found
It was found in the following places
registered S3 method for plot from namespace icenReg
namespace:icenReg
with value
function (x, fitNames = NULL, lgdLocation = "bottomleft", ...)
{
addList <- list(xlim = x$xRange, ylim = c(0, 1), xlab = "time",
ylab = "S(t)", x = NA)
dotList <- list(...)
#.........
#..........
legend(lgdLocation, legend = grpNames, col = cols, lty = 1)
}
<bytecode: 0x7fc9784fa660>
<environment: namespace:icenReg>
So there is a location parameter for the legend placement and the obvious alternative to try is:
plot(fit, lgdLocation = "topright")

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

Issues adding labels to mosaic3d plot in r

I am able to see labels if I use the simple 2d mosaic() function based on this example below.
labs <- round(prop.table(Titanic), 2)
mosaic(Titanic, pop = FALSE)
labeling_cells(text = labs, margin = 0)(Titanic)
However if i try to change this to mosaic3d then all the labels disappear and i see some black dots. Any help on how to add labels to mosaic3d plot is much appreciated.

How to plot two Dataset in same graph using poweRlaw

I have two data set, I need to plot them in same graph. Here is the two dataset.
The following is the code I used to plot the data. How to plot above data in same plot ? How to set the graph legend on the x-axis? I tried setting it but it didn't work I got some error.
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)
Kindly let me know thanks.
You code works find for me when I used simulated data. However, I think your problem is with your data. In particular, you need to set the xlim values in your plot command. Something like:
min_x = min(sample_data1$V1, sample_data1$V2)
max_x = max(sample_data1$V1, sample_data1$V2)
plot(m_bs, xlim=c(min_x, max_x))
Should do the trick. To add a legend, just use the legend function
legend("bottomleft", col=1:2, legend = c("BS", "EQ"), lty=1)

Resources