0

I have 80+ patients data with almost 5-6 categories assign to them within 200-300 days. So each patient has 300 days and the status name of each day. I want to make a stacked bar plot.

Problem: The problem is the ggplot sorting the categories alphabetically, I want to sort them exactly like it has in the source ALSO if for patient 1, on day 5th the category is "Medical Appointment" and the same category is repeating on day 50, ggplot does not differentiate it and takes all in one. I would like to plot on the day 50th with the "Medical Appointment" as well on the day 5th.

I counted the frequency for each patient's category. This is what I have so far:

tibble: 72 x 3
   Category    `Patient Number`     n
   <fct>       <chr>            <int>
 1 AD          Patient 1           34
 2 AD          Patient 2           15
 3 Admin delay Patient 2           30
 4 CC          Patient 2           20
 5 CD          Patient 1           52
 6 CD          Patient 2           88
 7 CP          Patient 1           52
 8 CP10        Patient 1            1
 9 CP11        Patient 1            1
10 CP12        Patient 1            1

I want to plot them into a staked bar chart on the x axis it should be Patient number on y axis should be N(Count) with respective category and it should not be sorted. I have 87 patients number.

Patient and category for each day: plot

ggplot(data = df, aes(x = `Patient Number`, y = n, fill = as.factor(Category))) + 
  geom_bar(position = position_stack(reverse = TRUE), stat="identity", na.rm = FALSE) +
  scale_fill_discrete(breaks = Category)

Thanks in advance. I'd appreciate if anybody could solve this.

Edit:

Here is the plot image what you @pieterbons have suggested me:

@pieterbons's soultions

OTStats
  • 1,820
  • 1
  • 13
  • 22
tashu
  • 11
  • 5

1 Answers1

0

If I understand the first part of your question correctly you want to make a stacked chart which shows which treatment the patient had on which day. This can be achieved by using geom_tile(), which does not aggregate the data per category the way geom_bar() does so you can get the same category multiple times in the plot if for example it occurs on day 5 and again on day 50. See below for a little example where "AA" occurs on day 1 for patient "a" and again on day 3:

library(dplyr)
library(ggplot2)

df <- data.frame(Patient = c("a","a","a","b","b","b"),
                 Day = c(1,2,3,1,2,3),
                 Category = c("AA", "AD", "AA", "CD", "AA", "AD"))

df %>% 
  ggplot(aes(x = Patient, y = Day)) + 
  geom_tile(aes(fill = Category))
pieterbons
  • 1,604
  • 1
  • 11
  • 14
  • Thank you for your reply, but if I Plot this, it does not make much sense to me. Please find the image of my data after applying your method also the sorting this is still an issue. How can i share the plot image? – tashu Jul 14 '20 at 13:48
  • I edited my post and pasted the picture of the graph, I tagged you. – tashu Jul 14 '20 at 13:55
  • Hi tashu. I assumed you had data for each patient on each day. In that case there would be no holes in the picture. The order of the colors in the graph is the order in which it occurred in the original dataframe. The legend might be alphabetically ordered, but that is not a problem, since it only contains the meaning of each color right? – pieterbons Jul 14 '20 at 18:37
  • Hi, I counted the data using group by category for each patient, because that way it is much easier to audit for all patients. ``` Category Patient Number n 1 AD Patient 1 34 2 AD Patient 2 15 ``` This is my data now, And I want to plot the data into staked bar chart and the sort should not be sorted alphabetically. I already put the data table in my previous post. We have assigned the catreament category for each patient depending on their progress so those categories might repeat for the same patients multiple times. – tashu Jul 15 '20 at 08:42