doing facets in ggplot I would often like the percentage to be used instead of counts.
e.g.
test1 <- sample(letters[1:2], 100, replace=T)
test2 &
Here's a solution that should get you moving in the right direction. I'm curious to see if there are more efficient ways to go about doing this as this seems a bit hacky and convoluted. We can use the built in ..density.. argument for the y aesthetic, but factors don't work there. So we also need to use scale_x_discrete to appropriately label the axis once we converted test2 into a numeric object.
ggplot(data = test, aes(x = as.numeric(test2)))+
geom_bar(aes(y = ..density..), binwidth = .5)+
scale_x_discrete(limits = sort(unique(test$test2))) +
facet_grid(~test1) + xlab("Test 2") + ylab("Density")
But give this a whirl and let me know what you think.
Also, you can shorten your test data creation like so, which avoids the extra objects in your environment and having to cbind them together:
test <- data.frame(
test1 = sample(letters[1:2], 100, replace = TRUE),
test2 = sample(letters[3:8], 100, replace = TRUE)
)