Several histograms on top of eachother without bins

Let's say I have this two-level framework. LC and HC. Now I want to get 2 stories, as shown below on top of each other.

data <- data.frame( welltype=c("LC","LC","LC","LC","LC","HC","HC","HC","HC","HC"), value=c(1,2,1,2,1,5,4,5,4,5)) 

Code to get the following graph =

 x <- rnorm(1000) y <- hist(x) plot(y$breaks, c(y$counts,0), type="s",col="blue") 

(thanks to Joris Mays)

So how can I start with this. Since I'm used to java, I thought of a for loop, but I was told not to do it that way.

enter image description here

+7
source share
2 answers

You can use the same code, except for points instead of a graph, to add additional lines to the graph.

Creating some data

 set.seed(5) d <- data.frame(x=c(rnorm(1000)+3, rnorm(1000)), g=rep(1:2, each=1000) ) 

And doing it is quite simple:

 x1 <- d$x[d$g==1] x2 <- d$x[d$g==2] y1 <- hist(x1, plot=FALSE) y2 <- hist(x2, plot=FALSE) plot(y1$breaks, c(y1$counts,0), type="s",col="blue", xlim=range(c(y1$breaks, y2$breaks)), ylim=range(c(0,y1$counts, y2$counts))) points(y2$breaks, c(y2$counts,0), type="s", col="red") 

Or in a more R-ish way:

 col <- c("blue", "red") ds <- split(d$x, d$g) hs <- lapply(ds, hist, plot=FALSE) plot(0,0,type="n", ylim=range(c(0,unlist(lapply(hs, function(x) x$counts)))), xlim=range(unlist(lapply(hs, function(x) x$breaks))) ) for(i in seq_along(hs)) { points(hs[[i]]$breaks, c(hs[[i]]$counts,0), type="s", col=col[i]) } 

EDIT: Inspired by Joris's answer, I want to note that the lattice can also easily execute overlapping density plots.

 library(lattice) densityplot(~x, group=g, data=d) 
+4
source

In addition to the method provided by Aaron, there is also a ggplot solution (see below), but I would strongly advise you to use density, since they will give more pleasant plots and it is much easier to build:

 # make data wells <- c("LC","HC","BC") Data <- data.frame( welltype=rep(wells,each=100), value=c(rnorm(100),rnorm(100,2),rnorm(100,3)) ) ggplot(Data,aes(value,fill=welltype)) + geom_density(alpha=0.2) 

gives: enter image description here

For the plot you requested:

 # make hists dataframe hists <- tapply(Data$value,Data$welltype, function(i){ tmp <- hist(i) data.frame(br=tmp$breaks,co=c(tmp$counts,0)) }) ll <- sapply(hists,nrow) hists <- do.call(rbind,hists) hists$fac <- rep(wells,ll) # make plot require(ggplot2) qplot(br,co,data=hists,geom="step",colour=fac) 

enter image description here

+13
source

All Articles