-1

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")
Marble
  • 125
  • 1
  • 9
  • 1
    Does this answer your question? [Making a stacked bar plot for multiple variables - ggplot2 in R](https://stackoverflow.com/questions/6693257/making-a-stacked-bar-plot-for-multiple-variables-ggplot2-in-r) – AnilGoyal May 01 '21 at 12:37

2 Answers2

2

I think this will solve your problem

df <- data.frame(
  stringsAsFactors = FALSE,
            Period = c("Day1","Day1","Day1","Day1",
                       "Day2","Day2","Day3","Day3","Day3","Day3","Day4",
                       "Day4","Day4","Day4"),
                AA = c(1L, 0L, 2L, 2L, 0L, 1L, 1L, 2L, 0L, 0L, 1L, 2L, 2L, 2L)
)

suppressMessages(library(tidyverse))

df %>% count(Period, AA) %>%
  ggplot() +
  geom_col(aes(x= Period, y = n, fill= as.character(AA)), position = 'fill')

Created on 2021-05-01 by the reprex package (v2.0.0)

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
  • I did something very similar just using the y = frequency(variable) function within ggplot. However, no I seem unable to add labels, is that something you did by any chance? – canIchangethis Nov 18 '22 at 20:55
1

Let's say your dataset is like this:

data <- data.frame(period = rep(paste(rep("Day",5) , 1:5 , sep="") , each=3) , 
                   AA = sample(0:2 , 15 , replace=T),
                   BB = sample(0:2 , 15 , replace=T),
                   CC = sample(0:2 , 15 , replace=T))

Then you can get it into the shape you need like so. Here I'm extracting the number from period so that it makes a nice numeric axis later on.

data <- data %>%  group_by(period) %>% 
  summarise(across(1:3 , sum)) %>% 
  gather(class, value , 2:4) %>% 
  mutate(period = str_extract(period , "\\d+"))

Then you can get your graph like so. Add + coord_flip() if you want it as a bar chart rather than column.

ggplot(data , aes(period , value , fill=class))+geom_col(position = "fill")

If you need to calculate the percentages before plotting, then...

data <- data %>%  group_by(period) %>% 
      summarise(across(1:3 , sum)) %>% 
      gather(class, value , 2:4) %>% 
      group_by(period) %>% 
      mutate(value = value/sum(value)) %>% 
      mutate(period = str_extract(period , "\\d+"))
Brendan
  • 166
  • 1
  • 4
  • Hey, I ran your program, I was not able to make you understand what I am looking for, there will be different graphs for AA, BB, CC(i should have not given the values in BB CC). The category is based on the value of 1, 0, 2, not AA,BB, CC. There is only 1 or 2 or 0 not a range of values in the column AA. – Marble Apr 29 '21 at 17:35