ALiX
ALiX

Reputation: 1031

ggplot2: Add a stacked bar showing the total distribution

The following snippet creates stacked bars showing the distribution of "clarity" by "cut".

data(diamonds)
qplot(cut, data=diamonds, geom="bar", fill=clarity, position="fill")

Another plot shows the total distribution of "clarity" for the entire dataset.

qplot(x=factor(""), data=diamonds, geom="bar", fill=clarity, position="fill")

Is there a way to add the second plot as an extra bar to the first plot, say with label "total"?

bycut total

Upvotes: 1

Views: 179

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 110024

Many approaches but here's one:

diamonds2 <- diamonds
diamonds2$cut <- "Total"
diamonds3 <- rbind(diamonds, diamonds2)

qplot(cut, data=diamonds3, geom="bar", fill=clarity, position="fill")

enter image description here

Upvotes: 3

Related Questions