Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Last active August 29, 2015 14:02
Show Gist options
  • Save muschellij2/b3b803df7a4cd165dbd4 to your computer and use it in GitHub Desktop.
Save muschellij2/b3b803df7a4cd165dbd4 to your computer and use it in GitHub Desktop.
Quick updown histogram
library(ggplot2)
df = data.frame(x = rnorm(100), x2 = rnorm(100, mean=2))
g = ggplot(df, aes(x)) + geom_histogram( aes(x = x, y = ..density..), fill="blue") +
geom_histogram( aes(x = x2, y = -..density..), fill= "green")
print(g)
## using base
h1 = hist(df$x)
h2 = hist(df$x2)
h2$counts = - h2$counts
hmax = max(h1$counts)
hmin = min(h2$counts)
X = c(h1$breaks, h2$breaks)
xmax = max(X)
xmin = min(X)
plot(h1, ylim=c(hmin, hmax), col="green", xlim=c(xmin, xmax))
lines(h2, col="blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment