Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting changes affect only legend and not bar graph using swimplot and ggplot2 packages

Update- this issue was solved, updated code is at the end of the post.

I am trying to create a swimmer plot to visualize individual patient duration of treatment with a drug administered at multiple dose levels (DLs). Each patient will be be assigned to treatment with only one DL, but multiple patients can be assigned to a given DL (e.g. 3 patients at DL1, 3 patients and DL2, etc.). I would like to color code the bars in the swimmer plot according to DL.

I am using the swimplot package for R and have been following the guide located here (https://cran.r-project.org/web/packages/swimplot/vignettes/Introduction.to.swimplot.html).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This guide has been sufficient for most things I have tried, up until I tried to change the colors of the bars in the plot and corresponding legend. Following the section in that guide titled "Modifying Colours and shapes" under "Making the plots more aesthetically pleasing with ggplot manipulations", I was able to change the bar colors in the legend, but not the bars themselves.

Example here

I have been using the following code.

    library(ggplot2)
    library (swimplot)
    library (gdata)
    library (readxl)
        
    ClinicalTrial.Arm <- read_excel("Swimmer_Test_Data1.xls")
    
    ClinicalTrial.Arm <- as.data.frame(ClinicalTrial.Arm)
        
    arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt',width=.85+ scale_fill_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))+ scale_color_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))
        
    arm_plot

I have tried a number of things to fix this, but am quite new to R and don’t think I really know enough to troubleshoot effectively. I have tried various syntax changes (e.g. removing quotation marks) and have tried using the geom bar command but wasn’t sure how/what to map to X and Y (it also seems like I shouldn’t need to do this).

I have also tried using the following code, but get an error.

    Colors <- c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600")       
            
    arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt',width=.85, fill = Colors)+ scale_fill_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))+ scale_color_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))
    
    Error in `check_aesthetics()`:
    ! Aesthetics must be either length 1 or the same as the data (20): fill
    Run `rlang::last_error()` to see where the error occurred.

Any help here would be greatly appreciated.

Solved! Updated, working code

library(ggplot2)
library (swimplot)
library (gdata)
library (readxl)

ClinicalTrial.Arm <- read_excel("Swimmer_Test_Data1.xls")
ClinicalTrial.Arm <- as.data.frame(ClinicalTrial.Arm)

Colors <- c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600")                                                               


arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt', name_fill = "Arm", width=.85) + scale_fill_manual(name="Arm",values = Colors) + 
scale_color_manual(name="Arm",values=Colors)

>Solution :

To make your code work you first have to map a variable on the fill aesthetic which using swimplot could be achieved via the name_fill argument:

Note: As I use the ClinicalTrial.Arm dataset from the swimplot package I adjusted your color palette to make it work with the three categories of the Arm column in this dataset.

library(ggplot2)
library(swimplot)

#pal <- c("DL1" = "#003f5c", "DL2" = "#374c80", "DL3" = "#7a5195", "DL4" = "#bc5090", "DL5" = "#ef5675", "DL6" = "#ff764a", "DL7" = "#ffa600")

pal <- c("Arm A" = "#003f5c", "Arm B" = "#bc5090", "Off Treatment" = "#ffa600")

swimmer_plot(df = ClinicalTrial.Arm, id = "id", end = "End_trt", name_fill = "Arm", width = .85) +
  scale_fill_manual(name = "Arm", values = pal)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading