ggplot2: Add a stacked bar showing the total distribution

ALiX

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

Tyler Rinker

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Showing data values on stacked bar chart in ggplot2

Showing Percentage and Total In stacked Bar Chart of chart.js

merge total of Stacked Bar Chart

ggplot2: add distribution jitter near the legend bar

Showing total (sum) values each group on the top of stacked bar chart in ggplot2

Add percentage labels to stacked bar chart ggplot2

Advanced stacked bar chart ggplot2

Show percent of total on top of geom_bar in ggplot2 while showing counts on y axis

ggplot2 - Stacked bar with different width

How to visualize a borehole profile (stacked bar plot) with ggplot2?

Stacked bar chart, reorder by total (sum up of values) instead of value ggplot2 + dplyr

Add label to entire bar within ggplot stacked bar chartr

plot confusion matrix as stacked bar chart with ggplot2

Label ggplot geom_bar with total stacked bar values

Stacked bar in ggplot2, with 2 series of bars (different columns)

Add total count on top of stacked barplot in R ggplot

Absolute labels for proportional stacked bar chart in ggplot2

Why does stacked bar plot change when add facet in r ggplot2

r stacked bar total value

add error bars to stacked bar plot in ggplot2 R - Solved

Stacked bar plot based in 4 variables with ggplot2

Showing sums for stacked bar chart

How to add text label to show total n in each bar of stacked proportion bars in ggplot?

Why is some data missing in ggplot2 stacked bar graph?

Cumulative stacked bar plot with the same variable with ggplot2

Stacked bar plot with ggplot2

Total label for stacked bar

ggplot2 custom stacked bar plot

add Total label on top of stacked bar in Vega-Lite