Pregunta

I like to weight cases to plot graphs in ggplot. I have a specific weight factor for each case, for instance:

value weight
2     0.34
5     0.75
6     2.31

and so on... Plotting simple grouped bars ("cross tabulation") is easy, I can use the xtabs function:

ftab <- round(xtabs(weightBy ~ varCount + varGroup),0)

When I want to plot histograms, simple bars or single box plots with weighted cases, I want to keep the distribution, so I use following function to weight the cases:

weightby <- function(var, weight) {
  items <- unique(var)
  newvar <- c()
  for (i in 1:length(items)) {
    newcount = round(sum(weight[which(var==items[i])]))
    newvar <- c(newvar, rep(items[i], newcount))
  }
  return (newvar)
}
if (!is.null(weightBy)) {
  variable <- weightby(variable, weightBy)
}

However, this function ignores the original case order, the "cases" are now numbered ascending according to the related categories. But... If I want to plot grouped box plots, I need a) the weighted variable with weighted counts b) the weighted variable with weighted groups c) the weighted means, median and quantiles within each group

How can do I do this? I have the correct weighted cross tabulation, but no weighted means from each sub group, because I cannot use the function shown above for creating tables (because of the lost correct case order).

Any hints are very appreciated!

¿Fue útil?

Solución

Very unclear what you are asking here. But the comment at the bottom of your question suggests you want to calculate mean and median value from weighted data. The easiest way to do this is via the survey package. You need to create a survey design object with svydesign - you can ignore all the stratification and clustering information and just include the weights - then use functions like svyby() or svyquantile() to calculate whatever you need.

If the question is just about ggplot2(), you can probably get by by just using the weight= aesthetic, unless it doesn't do what I think it does.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top