What I would like to do is use both arguments position = "fill"and position = "dodge" geom_bar(). Using some sample data
set.seed(1234)
df <- data.frame(
Id = rep(1:10, each = 12),
Month = rep(1:12, times = 10),
Value = sample(1:2, 10 * 12, replace = TRUE)
)
I can create the following chart
df.plot <- ggplot(df, aes(x = as.factor(Month), fill = as.factor(Value))) +
geom_bar(position = "fill") +
scale_x_discrete(breaks = 1:12) +
scale_y_continuous(labels = percent) +
labs(x = "Month", y = "Value")

I like the scaling and labeling of this graph, but I want to be able to unfasten it. However, when I do the following
df.plot2 <- ggplot(df, aes(x = as.factor(Month), fill = as.factor(Value))) +
geom_bar(position = "dodge", aes(y = (..count..)/sum(..count..))) +
scale_x_discrete(breaks = 1:12) +
scale_y_continuous(labels = percent) +
labs(x = "Month", y = "Value")

The bars are in the position and scaling that I want, but the Y axis labels represent the percentage of each bar relative to the total score, not the count for each month.
In general, I want to visualize the second graph with the labeling of the first graph. Is there a relatively easy way to automate this?
source
share