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]])
.