r - How to use facet_grid() with geom_histogram() -
i tried using facet_grid()
first time. plotted histograms own data, , distribution seemed inaccurate when counted boxes manually on graph. replicated code using mtcars
data, , problem seemed persist.
here histogram produced ggplot:
dfrm <- mtcars dfrm$am <- factor(dfrm$am, levels = c(0,1), labels = c("automatic", "manual")) dfrm$vs <- factor(dfrm$vs, levels = c(0,1), labels = c("v-engine", "straight-engine")) require(ggplot2) ggplot(dfrm, aes(x=dfrm[,"mpg"], fill=dfrm[,"am"], colour=dfrm[,"am"])) + geom_histogram(colour="transparent", position = "identity", alpha=0.2, bins = 10) + facet_grid(. ~ dfrm[,"vs"])
when count manually on histogram, count:
- v-engine, automatic: 14
- v-engine, manual: 4
- straight engine, automatic: 5
- straight engine, manual: 9
this code counts how many of exist in actual data:
require(pastecs) by(data=dfrm$am, indices = dfrm$vs, table)
and results are:
- v-engine, automatic: 12
- v-engine, manual: 6
- straight engine, automatic: 7
- straight engine, manual: 7
am doing wrong? there better way facet, or bug?
i did histograms base package check if results match, , seem accurate when count boxes.
hist(mtcars[which(mtcars[,"am"]==0 & mtcars[,"vs"]==0),"mpg"], xlim=c(10, 35), col=rgb(0.1,0.1,0.1,0.5), breaks=10) hist(mtcars[which(mtcars[,"am"]==1 & mtcars[,"vs"]==0),"mpg"], col=rgb(0.8,0.8,0.8,0.5), breaks=10 ,add=t) hist(mtcars[which(mtcars[,"am"]==0 & mtcars[,"vs"]==1),"mpg"], xlim=c(10, 35), col=rgb(0.1,0.1,0.1,0.5), breaks=10) hist(mtcars[which(mtcars[,"am"]==1 & mtcars[,"vs"]==1),"mpg"], col=rgb(0.8,0.8,0.8,0.5), breaks=10 ,add=t)
thanks.
===edit===
the answer provided bdemarest solves problem. however, confused syntax ggplot2 prefers, , how put inside function. here going for:
myfunc <- function(varx, dfrm, facet = f){ require(ggplot2) p = ggplot(dfrm, aes(x=varx, fill=am)) + geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10) if(!is.logical(facet)){ p <- p + facet_grid(. ~ facet) } return(p) } myfunc("mpg", mtcars, facet = "vs")
i tried , without quotations, couldn't work.
=== edit2 ===
with of bdemarest in comments, made lot of progress, color fill fails, when ggplot inside function
here, works perfectly:
facet = "vs" p = ggplot(dfrm, aes_string(x="mpg", fill="am")) + geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10) if(!is.logical(facet)){ p <- p + facet_grid(reformulate(facet, ".")) } p
however, not:
myfunc <- function(varx, dfrm, facet = false){ require(ggplot2) p = ggplot(dfrm, aes_string(x=varx, fill="am")) + geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10) if(!is.logical(facet)){ p <- p + facet_grid(reformulate(facet, ".")) } return(p) } myfunc("mpg", mtcars, facet = "vs")
the problem here groups wont colored accordingly. missing?
not sure caused problem, seems solved cleaning , simplifying ggplot code. in particular, ggplot2 not designed use column selection syntax such dfrm$am
or dfrm[, "am"]
inside of aes()
function (nor in formula expression facet_wrap(. ~ dfrm[, "vs"])
). although these types of expressions seem work fine, in general should avoided.
library(ggplot2) table(dfrm$am, dfrm$vs) # # v-engine straight-engine # automatic 12 7 # manual 6 7 p = ggplot(dfrm, aes(x=mpg, fill=am)) + geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10) + facet_grid(. ~ vs) ggsave("hist.png", p, height=4, width=6, dpi=150)
Comments
Post a Comment