格子小提琴图中每个因子值的网格线

时间:2012-07-02 17:51:58

标签: r lattice

我使用R的晶格包生成了点图和小提琴图。点图对每个因子值都有网格线,以便更容易找到相应的点。

是否有可能在格子小提琴情节中产生这样的线条而不显示这样的线条?

在我的情况下,数据分布在更广的范围内,因此有时更难找到因子的相应值(因为对于某些因子值,小提琴看起来更像是一个点而不是小提琴。)

以下是使用内置数据框mtcars的dotplot(具有连接点的垂直线)和小提琴图(其中“小提琴”未与因子的值图形连接)的最小示例:

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

1 个答案:

答案 0 :(得分:3)

这应该在小提琴情节后面加上灰线:

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