Ggplot2 histogram binwidth

I would like to create multiple histograms within one chart (using facet_wrap). This could be some example code:

df <- data.frame(p1 = rnorm(100,5,2), p2 = rnorm(100,80,20), group = rep(LETTERS[1:4],25))

library(ggplot2)
library(reshape)

plotData <- melt(df, id.vars = "group", measure.vars = c("p1","p2")  )

m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + geom_bar(position=position_dodge())
m <- m + facet_wrap( ~ variable,scales = "free_x")
print(m)

      

Now I would like to change the graph it generates for each parameter ("p1," p2 "), let's say 10 trays.

Until now, I could not find a way to do this, since the computation of binaries / rifts must depend on a subset of the data.

Is it even possible?


I want to share my solution (taken from the answer question linked above) extended to overlay histograms with density curves scaled to the number of histograms:

df <- data.frame(p1 = rnorm(1000,5,2), p2 = rnorm(1000,80,20), group = rep(LETTERS[1:4],25))

library(ggplot2)
library(reshape)
library(plyr)

plotData <- melt(df, id.vars = "group", measure.vars = c("p1","p2")  )

nBins <- 10

groupedData <- dlply(plotData, .(variable))
groupedBinWidth <- llply(groupedData, .fun = function(data, nBins) {
  r <- range(data$value, na.rm = TRUE, finite = TRUE)
  widthOfBins = (r[2] - r[1])/nBins
  if (is.na(widthOfBins) || is.infinite(widthOfBins) || (widthOfBins <= 0)) widthOfBins <- NULL
  widthOfBins
}, nBins = nBins)

densData <- dlply(plotData, .(variable, group), .fun = function(subData){
  param <- subData$variable[1]
  group <- subData$group[1]
  d <- density(subData$value)
  bw <- groupedBinWidth[[param]]
  data.frame(x = d$x, y = d$y * nrow(subData) * bw , group = group, variable = param)
})

hls <- mapply(function(x, b) geom_bar(aes(x = value), position = position_dodge(), data = x, binwidth = b), 
              groupedData, groupedBinWidth)

dLay <- mapply(function(data) geom_density(data = data, aes(x = x, y = y), stat = "identity", fill = NA, size = 1), 
               densData)

m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + hls
m <- m + dLay
m <- m + facet_wrap( ~ variable,scales = "free")
print(m) 

      

enter image description here

+3


source to share


1 answer


Try this - really ugly code, but works if I understand you correctly. You might want to play with geom_density and maybe remove the fill to make it more readable.

nbin<- 5
m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + geom_histogram(data = subset(plotData, variable == "p1"), binwidth=diff(range(subset(plotData, variable == "p1")$value))/nbin)
m <- m + geom_histogram(data = subset(plotData, variable == "p2"),  binwidth=diff(range(subset(plotData, variable == "p2")$value))/nbin)
m <- m + facet_wrap( ~ variable,scales = "free_x")
print(m)

      



enter image description here

0


source







All Articles