Get rid of gray line in R density plot density at

2019-02-28 23:41发布

问题:

I have a density plot that I want to save in an eps file.

My density plot is something like

setEPS()
postscript("myfile.eps")
plot(density(rnorm(1000)))
dev.off()

just with my data and a little bit more complex (changing labels and margins, more lines on top of it, etc).

My problem is that I get an annoying, horizontal thin gray line on top of my density plot at y = 0, and I'd like to get rid of it.

When plotting directly to the X device I don't get this horizontal gray line, but in postscript I always do.

I've tried using dev.copy2eps and also cairo_ps() instead of postscript(), but I still get the grey line.

I've also tried using different (and random) data, with a very simple plot like the code block above, and still get the grey line in the postscript.

Does anyone know how to get rid of this line?

回答1:

Read the help page:

?plot.density

zero.line.......logical; if TRUE, add a base line at y = 0

I will admit that I didn't discover this by reading the help page. I had looked at the code for density.default, found nothing useful, then tried plot.density, got the nothing, found message, and then tried:

> getAnywhere(plot.density)

Which produced this ... making me realize that I should have started with the help page:

A single object matching ‘plot.density’ was found
It was found in the following places
  registered S3 method for plot from namespace stats
  namespace:stats
with value

function (x, main = NULL, xlab = NULL, ylab = "Density", type = "l", 
    zero.line = TRUE, ...)