Description Usage Arguments Value See Also Examples
View source: R/diffdensity.plot.R
Plot the difference of densities between two distributions 1 and 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | diffdensity.plot(
x1,
x2,
y1,
y2,
bw = bw.nrd,
n = 100,
ztransform = sqrt_transform,
zlim = NULL,
zlim.mult = 1,
relative = FALSE,
plot.d1 = FALSE,
plot.d2 = FALSE,
plot.diff = TRUE,
draw.contour = TRUE,
contour.levels = NULL,
xlim = extendrange(range(c(x1, x2)), f = extendrange.x),
ylim = extendrange(range(c(y1, y2)), f = extendrange.y),
extendrange = 0,
extendrange.x = extendrange,
extendrange.y = extendrange,
col1 = "blue",
col2 = "red",
bg.col = "white",
ncol = 101,
add = FALSE,
...
)
|
x1, x2, y1, y2 |
the x and y values for distributions 1 and 2. |
bw |
a bandwidth selection function such as |
n |
the number of grid points. |
ztransform |
transformation of the z axis of the 2D density. The default |
zlim |
overrides the limits in the z direction, setting the value where either |
zlim.mult |
a multiplication factor for zlim, especially useful in |
relative |
whether to adjust |
plot.d1, plot.d2 |
whether to plot the 2d densities of distributions 1 and 2, respectively. Disabled by default. |
plot.diff |
whether to plot the differential densities. Enabled by default. |
draw.contour |
whether to overlay a |
contour.levels |
levels at which to draw contour lines, see the |
xlim, ylim |
limits of the plot on x and y. |
extendrange, extendrange.x, extendrange.y |
extend |
col1, col2 |
colors to represent the two distributions. |
bg.col |
the background color. |
ncol |
the number of colors of the palette. Should be an odd number to avoid weird effects around 0 |
add |
if TRUE, add to the current plot instead of creating a new one. |
... |
further arguments from and two other methods, in particular graphical parameters for |
Invisibly
, a list
like kde2d
with z
the difference in densities, and the following additional āzā elements:
d1.z, d2.zDensities of distributions 1 and 2
sum.zThe sum of the density distributions 1 and 2
The Stack Overflow answer that inspired this function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # Get some gaussian distributions
x1 <- rnorm(10000)
y1 <- rnorm(10000)
x2 <- rnorm(10000, mean = 1, sd = 0.5)
y2 <- rnorm(10000, mean = 0.5, sd = 2)
# First shot at the plot
diffdensity.plot(x1, x2, y1, y2)
# Make it smoother
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10)
# Improve contour
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
contour.levels = c(0.01, 0.1, 0.2, 0.3, 0.4))
# Change the colors
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
col1 = "green", col2 = "yellow", bg.col = "black")
# Also plot d1 and d2
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
plot.d1 = TRUE, plot.d2 = TRUE, relative = TRUE)
# Increase resolution
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
n = 500)
# Try an assymetric distribution
y1 <- rexp(10000, rate = 1)
y2 <- rexp(10000, rate = 1.5)
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10)
# A bit off the density is clipped in y, so extend the limits
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
extendrange.y = .1)
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
extendrange = .1, extendrange.x = 0)
# Extend in x as well
diffdensity.plot(x1, x2, y1, y2, bw = function(x) bw.nrd(x) * 10,
extendrange = .1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.