r - Symmetrical, violin plot-like histogram? -


how can make histogram in center of each bar lies along common axis? violin plot step-shaped edges.

i'd in lattice, , don't mind customizing panel functions, etc., happy use base r graphics or ggplot2. (i haven't yet thrown myself ggplot2, take plunge @ point.)

(why want this? think might useful replacement violin plot when data discrete , occurs @ few [5-50] evenly-spaced numeric values. each bin represents point. of course, generate normal histogram. think it's useful display both box-and-whisker plot , violin plot. discrete data @ regular intervals, symmetrical histogram same orientation boxplot allows comparison of detailed structure of data boxplot, violin plot does. in case symmetrical histogram more informative violin plot. (a beanplot might alternative described, although in fact data not literally discrete--it converges near series of regular values. makes r's beanplot package less useful me, unless normalize values mapping them nearest regular value.))

here 30-observation subset of of data, generated agent-based simulation:

df30 <- data.frame(crime.v=c(0.2069526, 0.2063516, 0.06919754, 0.2080366, -0.06975912, 0.206277, 0.3457634, 0.2058985, 0.3428499, 0.3428159, 0.06746109, -0.07068694, 0.4826098, -0.06910966, 0.06769761, 0.2098732, 0.3482267, 0.3483602, 0.4829777, 0.06844112, 0.2093492, 0.4845478, 0.2093505, 0.3482845, 0.3459249, 0.2106339, 0.2098397, 0.4844956, 0.2108985, 0.2107984), bias=c("beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "beast", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus", "virus")) 

a dataframe named df full set of 600 observations in rdata file can downloaded link: cvexample.rdata.

the crime.v values near 1 of following, i'll call foci:

[1] -0.89115386 -0.75346155 -0.61576924 -0.47807693 -0.34038463 -0.20269232 -0.06500001 [8]  0.07269230  0.21038460  0.34807691  0.48576922  0.62346153  0.76115383  0.89884614 

(the crime.v values averages of 13 variables, values can range -1 1, end converging values in neighborhood of .9 or -.9. averages of 13 values @ around .9 or -.9 near foci. in practice determined appropriate values foci examining data, since there's additional variation involved.)

a violin plot can produced with:

require(lattice) bwplot(crime.v ~ bias, data=df30, ylim=c(-1,1), panel=panel.violin) 

if run larger dataset, you'll see 1 of violin plots produced multimodal, while other isn't. however, doesn't seem reflect difference in data underlying 2 violin plots; it's artifact due locations of foci in relation plot, far can tell. can smooth away difference tweaking parameters of density passed panel.violin, clearer represent how many points there in each cluster.

thanks!

here 1 possibility using base graphics:

tmp <- tapply( iris$petal.length, iris$species, function(x) hist(x, plot=false) )  plot.new() tmp.r <- do.call( range, lapply(tmp, `[[`, 'breaks') ) plot.window(xlim=c(1/2,length(tmp)+1/2), ylim=tmp.r) abline(v=seq_along(tmp))  for( in seq_along(tmp) ) {     h <- tmp[[i]]     rf <- h$counts/sum(h$counts)     rect( i-rf/2, head(h$breaks, -1), i+rf/2, tail(h$breaks, -1) ) }  axis(1, at=seq_along(tmp), labels=names(tmp)) axis(2) box() 

you can tweak different parts preferences , whole thing wrapped function.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -