I plot periodogram using spec.pgram but I don't want to use logarithmic Y scale. How can I remove it?
[
If I understand your question correctly, then spec.pgram produces on object of the class spec which are plotted using the plot.spec method, which is described in the documentation as follows:
Plotting Spectral Densities Plotting method for objects of class
"spec". For multivariate time series it plots the marginal spectra of
the series or pairs plots of the coherency and phase of the
cross-spectra.
This method uses an argument log to specify whether or not the y-axis will use the logarithmic Y scale. You can lose the logarithmic scale by setting it to "no".
# code based on the example in the plot.spec documentation
require(graphics)
spectrum(mdeaths, spans = c(3,3))
par(mfrow = c(1,2))
plot(mfdeaths.spc, log = "yes")
plot(mfdeaths.spc, log = "no")
dev.off
Related
I have just started to use Facebook's Prophet library in R, I have a question regarding the dyplot.prophet graphing function. I have created a plot using this code:
dyplot.prophet(m,forecast,uncertainty=TRUE)
This works fine, but the resulting graph has no x or y axis labels or a main title, I would like to add some to the plot.
The documentation describes the following
dyplot.prophet(x, fcst, uncertainty = TRUE, ...)
Where:
x is the Prophet object,
fcast is the data frame returned by predict(m, df), and
uncertainty is the Boolean indicating if the uncertainty interval for yhat is to be plotted. There are then … indicating additional arguments, I would like to know what these arguments are, and if any of them relate to plot titles or axis legends. Can anyone assist?
It's now possible to add titles.
dyplot.prophet(m,forecast,main="Graph")
I would like to plot a segment of an ROC curve over a specific range of x values, instead of plotting the entire curve. I don't want to change the range of the x axis itself. I just want to plot only part of the ROC curve, within a range of x values that I specify.
library(pROC)
data(aSAH)
rocobj <- roc(aSAH$outcome, aSAH$wfns)
plot(rocobj)
That code plots the whole ROC curve. Let's say I just wanted to plot the curve from x=1 to x=.5. How could I do that? Thank you.
The default plot function for roc objects plots the rocobj$sensitivities as a function of rocobj$specificities.
So
plot(rocobj$specificities,rocobj$sensitivities,type="l",xlim=c(1.5,-0.5))
abline(1,-1)
achieves the same as
plot(rocobj)
And
plot(rocobj$specificities[2:6],rocobj$sensitivities[2:6],type="l",xlim=c(1.5,-0.5),ylim=c(0,1))
abline(1,-1)
Gets close to what I think you are after (plots from 0.514 to 1.0). I don't know enough about the package to know if the sensitivities can be calculated at a specific range, or resolution of specificities.
The plot function of pROC uses the usual R semantics for plotting, so you can use the xlim argument as you would for any other plot:
plot(rocobj, xlim = c(1, .5))
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")
I am trying to simulate a minefield by plotting two Poisson distributed samples in the same plot, one with a higher intensity and smaller area than the other. This is the minefield and the other is just noise (stones, holes, metal) seen as points. I cannot get R to plot the points with the same units in the axis. Whatever I do, the points span the entire plot, even though I only want the X points to cover a quarter of the plot. My R-code is just the following:
library(spatstat)
Y = rpoispp(c(5),win=owin(c(0,10),c(0,10)))
X = rpoispp(c(10),win=owin(c(0,5),c(0,5)))
Please let me know if you can help me.
My guess is that you are doing something like:
> plot(Y)
> plot(X)
to plot the points.
The problem with this is that the default behavior of the plot function for the class ppp (which is what the rpoispp function returns) is to create a new plot with just its points. So the second plot call essentially erases the first plot, and plots its own points in a differently scaled window. You can override this behavior by setting the option add=TRUE for the second plot. So the code
> plot(Y)
> plot(X, add=TRUE, cols="red")
should get you something like:
Check out the docs (help(plot.ppp)) for more explanation and other options to prettify the plot.
I'm trying to plot some data with 2d density contours using ggplot2 in R.
I'm getting one slightly odd result.
First I set up my ggplot object:
p <- ggplot(data, aes(x=Distance,y=Rate, colour = Company))
I then plot this with geom_points and geom_density2d. I want geom_density2d to be weighted based on the organisation's size (OrgSize variable). However when I add OrgSize as a weighting variable nothing changes in the plot:
This:
p+geom_point()+geom_density2d()
Gives an identical plot to this:
p+geom_point()+geom_density2d(aes(weight = OrgSize))
However, if I do the same with a loess line using geom_smooth, the weighting does make a clear difference.
This:
p+geom_point()+geom_smooth()
Gives a different plot to this:
p+geom_point()+geom_smooth(aes(weight=OrgSize))
I was wondering if I'm using density2d inappropriately, should I instead be using contour and supplying OrgSize as the 'height'? If so then why does geom_density2d accept a weighting factor?
Code below:
require(ggplot2)
Company <- c("One","One","One","One","One","Two","Two","Two","Two","Two")
Store <- c(1,2,3,4,5,6,7,8,9,10)
Distance <- c(1.5,1.6,1.8,5.8,4.2,4.3,6.5,4.9,7.4,7.2)
Rate <- c(0.1,0.3,0.2,0.4,0.4,0.5,0.6,0.7,0.8,0.9)
OrgSize <- c(500,1000,200,300,1500,800,50,1000,75,800)
data <- data.frame(Company,Store,Distance,Rate,OrgSize)
p <- ggplot(data, aes(x=Distance,y=Rate))
# Difference is apparent between these two
p+geom_point()+geom_smooth()
p+geom_point()+geom_smooth(aes(weight = OrgSize))
# Difference is not apparent between these two
p+geom_point()+geom_density2d()
p+geom_point()+geom_density2d(aes(weight = OrgSize))
geom_density2d is "accepting" the weight parameter, but then not passing to MASS::kde2d, since that function has no weights. As a consequence, you will need to use a different 2d-density method.
(I realize my answer is not addressing why the help page says that geom_density2d "understands" the weight argument, but when I have tried to calculate weighted 2D-KDEs, I have needed to use other packages besides MASS. Maybe this is a TODO that #hadley put in the help page that then got overlooked?)