1

I am creating a stacked barplot showing different types of treatment for ovarian cancer. Each 'bar' represents a different treatment. Some patients are treated with the same combinational therapy, but not neccessarily in continued lines.

I've looked at this answer # 2.

But it doesn't cut it.

I've attached a sample patient

   record_id    line                                treatment   value
134        47     1                  Carboplatin og Docetaxel     1
135        47     2                     Carboplatin og Caelyx     1
136        47     3                     Carboplatin og Caelyx     1
137        47     4 AVANOVA, arm 2 - Bevacizumab og NIraparib     1
138        47     5                     Carboplatin og Caelyx     1

Using the following ggplot for the patients generates

library(tidyverse)
library(ggplot2)

stack %>% 
  ggplot(aes(x = record_id, y = value, fill = interaction(treatment,-line))) +
  geom_bar(stat = "identity", position = "stack", data = stack %>% filter(record_id == 47)) +
  guides(fill = guide_legend("ordering"))

Stacked barplot using fill=interaction

I have also tried using the fill = reorder - same code as above. The result is

Stacked barplot using fill=reorder

I was hoping to get a result looking the the first picture (with fill = interaction), but where the colors appear the same for the same treatment (in this example 'Carboplatin and Caelyx').

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

1 Answers1

0

It sounds like you want the bars stacked in the first order, but with their fill solely based on treatment. I think this can be done by using group and fill together:

library(tidyverse)
stack %>% 
  ggplot(aes(x = record_id, y = value, 
             group = interaction(treatment,-line),
             fill  = treatment)) +
  geom_bar(stat = "identity", position = "stack", 
           data = stack %>% filter(record_id == 47), 
           color = "white") +
  guides(fill = guide_legend("ordering"))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53