简体   繁体   中英

How to plot a histogram using grid graphics in R?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM