I want to make a plot that is similar to beanplot
or violin
plot, but instead of the symmetric shapes, i want to plot two different distributions on the different sides of the center line. Something like figure 4 in this article [pdf]http://www.jstatsoft.org/v28/c01/paper.
I want to use the grid
graphics engine in R
. Any pointers will be useful. I looked at the lattice
package and the histogram
function in lattice
package, but that is not what i want to do.
Any help/pointers are appreciated.
You can reasonably easily get the half-violin plot with fairly simple modifications to the function, panel.violin
in Sarkar's lattice package. There are four lines in that function that can be altered inside the grid.polygon
call to change from "two-sided" density plotting to single sided density plotting. First require(lattice)
and require(grid)
. Then type panel.violin
. I'll show you the "horizontal" alteration:
require(grid)
panel.violin2 <-
# snipped all the arguments and processing
grid.polygon(x = c(dx.list[[i]] ),
# Notice I removed: ... ,rev(dx.list[[i]])
y = c(2*dy.list[[i]] ), default.units = "native",
# Removed: ... , -rev(dy.list[[i]])
name = trellis.grobname(identifier, type = "panel",
group = group), gp = gpar(fill = col, col = border,
lty = lty, lwd = lwd, alpha = alpha))
Also remove it from the corresponding section in the else{ ...} clause. Now you can run it with the example in help(panel.violin)
bwplot(voice.part ~ height, singer,
panel = function(..., box.ratio) {
panel.violin2(..., col = "transparent",
varwidth = FALSE, box.ratio = box.ratio)
panel.bwplot(..., fill = NULL, box.ratio = .1)
} )
If you wanted the density on the other side, all you would need to do would be remove the dx.list[[i]] and dy.list[[i]] and leave in the rev(dx.list[[i]])
and -rev(dy.list[[i]])
.