Reputation: 593
Given two vectors (e.g. describing the distributions of wealth in two countries), I need to represent the share of wealth at various percentiles of the population using a barplot.
Starting from two sample distribution
e1 <- sort(rexp(100,rate=1))
e2 <- sort(rexp(200,rate=.5))
here is what I did
i=c(rep("0 to 50",50),rep("51 to 90",40),rep("91 to 99",9),"top 1%")
i2=rep(i,each=2)
d1 <- data.frame(rep("Italy",100),e1/sum(e1),i)
d2 <- data.frame(rep("France",200),e2/sum(e2),i2)
colnames(d1)=c("geo","wealth","class")
colnames(d2)=c("geo","wealth","class")
dd <- rbind(d1,d2)
ggplot(dd,aes(x=geo,fill=class,weight=wealth))+geom_bar(width=.5)+scale_fill_brewer(palette="Blues")
I wonder if there is an easier/more direct way to reach the same result
Upvotes: 2
Views: 2098
Reputation: 7119
Maybe you can try to look at the quantile
function to prepare your df1 and df2:
q1 <- quantile(e1, c(.5, .90, .99, 1))
df1 <- data.frame(geo = "Italy", class=names(q1), wealth=q1)
q2 <- quantile(e2, c(.5, .90, .99, 1))
df2 <- data.frame(geo = "France", class=names(q2), wealth=q2)
I'm new to R
so wiser people can offer better answers.
Upvotes: 1