Last active
August 3, 2018 02:41
-
-
Save pgensler/da55810771714656e931491f6f256a3f to your computer and use it in GitHub Desktop.
Stacked barchart with Counts and Total $
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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