I have a data set like this ->
library(ggplot2)
response <- c("Yes","No")
gend <- c("Female","Male")
purchase <- sample(response, 20, replace = TRUE)
gender <- sample(gend, 20, replace = TRUE)
df <- as.data.frame(purchase)
df <- cbind(df,gender)
so head(df)
looks like this ->
purchase gender
1 Yes Female
2 No Male
3 No Female
4 No Female
5 Yes Female
6 No Female
Also, so you can validate my examples, here is table(df)
for my particular sampling.
(please don't worry about matching my percentages)
gender
purchase Female Male
No 6 3
Yes 4 7
I want a "histogram" showing Gender, but split by Purchase. I have gone this far ->
ggplot(df) +
geom_bar(aes(y = (..count..)/sum(..count..)),position = "dodge") +
aes(gender, fill = purchase)
which generates ->
histogram with split bins, by percentage, but not the aggregate level I want
The Y axis has Percentage as I want, but it has each bar of the chart as a percentage of the whole chart.
What I want is the two "Female" bars to each be a percentage of there respective "Purchase". So in the chart above I would like four bars to be,
66%, 36%, 33%, 64%
, in that order.
I have tried with geom_histogram to no avail. I have checked SO, searched, ggplot documentation, and several books.
Regarding the suggestion to look at the previous question about facets; that does work, but I had hoped to keep the chart visually as it is above, as opposed to split into "two charts". So...
Anyone know how to do this?
Thanks.