jjchern / gglorenz

Plotting Lorenz curves with the blessing of ggplot2
https://jjchern.github.io/gglorenz/
Other
31 stars 2 forks source link

plot lorenz curve with weight #4

Closed CWen001 closed 4 years ago

CWen001 commented 4 years ago

Hello, I'm trying to use this package to plot lorenz with weight considered, but I could find documentation about that. Is there a way to consider weight in plotting?

It is a a common use case to analyze data of the census units, which have both income and population variables. Therefore, it would be nice to plot lorenz curve for income with population as weight. Thank you very much.

cortinah commented 4 years ago

Hi, yes, you can do this, but we need to add it to the stat_lorenz() help page...

stat_lorenz() can take a parameter called n with the frequency of the observations. For example:

ggplot(data, aes(x=value, n=freq)) +stat_lorenz()

this should work. See pull request: https://github.com/jjchern/gglorenz/pull/1

I will update the documentation soon.

CWen001 commented 4 years ago

Thank you very much, and the n parameter works as expected. One more thing, the package has a handy function annotate_ineq[] to label the Gini value. Is it also possible to improve it by adding a weighting parameter?

cortinah commented 4 years ago

We haven't implemented this yet, but you can do this to get the same result:

ggplot(data,aes(x=val,n=freq)) +stat_lorenz() +annotate_ineq(rep.int(data$val,data$freq))

CWen001 commented 4 years ago

Many thanks for your kind help! closing this issue as all problems solved.