Skip to content

Instantly share code, notes, and snippets.

@drewhendrickson
Created December 1, 2013 23:28
Show Gist options
  • Select an option

  • Save drewhendrickson/7742291 to your computer and use it in GitHub Desktop.

Select an option

Save drewhendrickson/7742291 to your computer and use it in GitHub Desktop.
Sample plot showing how to transform ggplot2 histogram from frequency to percent.
require(ggplot2)
agg_progress <- data.frame(id=rep(letters[1:10], each=6),
Var1=rep(LETTERS[1:6], times=10),
Freq=floor(100 * runif(60)))
# As frequency
ggplot(agg_progress) + theme_bw() +
geom_histogram(aes(x=Freq)) +
labs(y="Count",
x="Frequency") +
facet_wrap( ~ Var1)
# As percent
ggplot(agg_progress) + theme_bw() +
geom_histogram(aes(x=Freq, y=(..count..)/sum(..count..))) +
labs(y="Percent",
x="Frequency") +
facet_wrap( ~ Var1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment