4

I produced dot plots and violin plots using the lattice package of R. The dotplots have grid lines for each value of a factor to make it easier to find the respective point.

Is it possible to produce such lines in a lattice violin plot which does not show such lines?

In my case the data is spread over a wider range making it sometimes more difficult to find the corresponding value of the factor (because the violin looks more like a point than a violin for some values of the factor).

Here is a minimal example for the dotplot (having vertical lines connecting the dots) and for the violin plot (where the "violin" is not graphically connected with the values of the factors) using the built-in data frame mtcars:

library("lattice")
dotplot( mpg ~ as.factor( cyl ), data=mtcars )
bwplot( mpg ~ as.factor(cyl), data=mtcars, panel = function( ..., box.ratio ) { panel.violin( ..., box.ratio ) } )
porst17
  • 377
  • 3
  • 16

1 Answers1

3

This should put gray lines behind the violin plots:

bwplot( mpg ~ as.factor(cyl), data=mtcars, 
  panel = function(x, y, ..., box.ratio ) {
    panel.abline(v = x, col = "gray")
    panel.violin(x, y, ..., box.ratio ) 
  } )

Violin plot with vertical lines

BenBarnes
  • 19,114
  • 6
  • 56
  • 74