Skip to content

Instantly share code, notes, and snippets.

@pgensler
Last active August 3, 2018 02:41
Show Gist options
  • Save pgensler/da55810771714656e931491f6f256a3f to your computer and use it in GitHub Desktop.
Save pgensler/da55810771714656e931491f6f256a3f to your computer and use it in GitHub Desktop.
Stacked barchart with Counts and Total $
library(ggplot2)
library(tidyverse)
library(ggthemes)
install.packages("ggthemes")
summary <- diamonds %>%
group_by(cut) %>%
summarize(price = sum(price),
pos = n())
#With bar labels
ggplot(diamonds, aes(x = cut)) +
geom_bar(aes(fill = color), position = position_stack()) +
geom_text(aes(label = ..count.., y = ..count.., group = color), stat = "count", position = position_stack(vjust = 0.5)) +
geom_text(data = summary, aes(label = scales::dollar(price), y = pos), nudge_y = 1000)
##Pass in Summary as a vector already created into ggplot2
#Without bar labels
ggplot(diamonds, aes(x = cut)) +
geom_bar(aes(fill = color), position = position_stack()) +
geom_text(data = summary, aes(label = scales::dollar(price), y = pos), nudge_y = 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment