So I have a data frame that in the simplified form will look like this.
|Period|AA|
| Day1 | 1 |
| Day1 | 0 |
| Day1 | 2 |
| Day1 | 2 |
| Day2 | 0 |
| Day2 | 1 |
| Day3 | 1 |
| Day3 | 2 |
| Day3 | 0 | .
.
.
.
| Day99 | 0 |
And now I want to plot a graph for "AA" where "Period" is the x-axis, "0-100" is the y-axis plotting the total frequency of each "0", "1", "2" in percentage, like a percentage stacked bar chart.
I tried something like this for a single-column AA, but it doesn't help me with what I want.
library("ggplot2")
library("dplyr")
Count=1
data <- aggregate(Count ~ ., data, sum)
data <- group_by(data, Period) %>%
mutate(percent = Count / sum(Count)) %>%
as.data.frame()
ggplot(data,
aes(x = Period,
y = percent,
fill = AA)) +
geom_bar(position = "fill", stat = "identity")