strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
603 stars 91 forks source link

Enabling weights column in sjplot data selection #939

Open joelem opened 1 month ago

joelem commented 1 month ago

Hi Daniel,

First, thank you for such an amazing R package, it is so useful! I wanted to request the ability to include weights in the sjplot function, which is meant to be pipe friendly.

As an example, right now this is the way we would plot a cross tab with weights included, basically the weight column is fed by referring back to the original DF:

data %>% 
  select(A, B) %>% 
  sjplot(fun = 'xtab',
         weight.by = data$weight)

However, in a piping pipeline, we often want to filter or preprocess before sending the data to sjplot, so having the ability to also select and pipe the weight column would be really helpful (otherwise we would need to have additional/redundant, and thus potentially error prone, code that also filters the original DF for the weight column). The following tries throw out error "Can't extract columns past the end":

data %>% 
  select(A, B, weights) %>% 
  sjplot(fun = 'xtab',
         weight.by = weights)

data %>% 
  select(A, B, weights) %>% 
  sjplot(fun = 'xtab',
         weight.by = .$weights)

But it could be that maybe there is already a way to do this and I am missing it.

Thank you for any help!