Swimmer_plot function not working

Hello,

I'm trying to obtain a Swimmer plot, using the swimmer_plot function. I enter the following code:
as.character <- HN_outcomes_no_columns$Patient number

as.numeric <-HN_outcomes_no_columns$Time
swimmer_plot(df=HN_outcomes_no_columns, id=Patient number, end=Time, fill='lightblue',width=.85)

but get the following error message : Can't subset columns that don't exist.
x Locations 5, 6, 7, 8, 9, etc. don't exist.
i There are only 4 columns

Here is my Reproducible dataset as below (using datapasta package)

data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
Patient number = c(1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19),
Time = c(1596,1514,356,2085,1921,
318,83,328,815,1854,159,240,821,720,2315,1800,99,
205,177),
Status = c("2","2","2","2","2","2",
"2","2","2","2","2","2","2","2","2","2","2","2",
"2"),
Notes = c(NA,NA,"Problems with Flap",
NA,NA,"Sarcoma primary",NA,NA,NA,NA,
"advanced cancer",NA,NA,NA,"Admitted due to longstanding epilepsy",
NA,NA,NA,NA)

As you can see I'm very new, only a few days since i've started using Rstudio. ..these are outcomes for cancer patients, so some columns have incomplete information..but I'm not sure this matters, as the Swimmer plot, I presume, only needs the 'Time' and 'Patient Number' variables for computation. The patient number variable is simply their assigned number (as I anonymised their hospital number) and Time = survival in days. I'm really not sure where I'm going wrong here.

Here are a couple of things that may help.

  1. I would recommend avoiding column names that contain spaces. You can use these, but it makes things a bit more complicated, requiring references using [[ ]] or `notation, e.g. df$`Patient number` or df[["Patient number"]]. So, let's replace that name with Patient_ID:
HN_outcomes_no_columns <- data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  Patient_ID = c(1,2,3,4,5,6,7,8,9,10,
                     11,12,13,14,15,16,17,18,19),
  Time = c(1596,1514,356,2085,1921,
           318,83,328,815,1854,159,240,821,720,2315,1800,99,
           205,177),
  Status = c("2","2","2","2","2","2",
             "2","2","2","2","2","2","2","2","2","2","2","2",
             "2"),
  Notes = c(NA,NA,"Problems with Flap",
            NA,NA,"Sarcoma primary",NA,NA,NA,NA,
            "advanced cancer",NA,NA,NA,"Admitted due to longstanding epilepsy",
            NA,NA,NA,NA)
)
  1. To change the class of your columns, you need to wrap the column in the functions as.character() or as.numeric(), like so:
HN_outcomes_no_columns$Patient_ID <- as.character(HN_outcomes_no_columns$Patient_ID)

Alternately, you can use the class() function to change class:

class(HN_outcomes_no_columns$Patient_ID) <- "character"

As you've written it, you're assigning the HN_outcomes_no_columns$Time vector to an object called as.numeric, which isn't what you really want to do.

With these corrections, you should now get your Swimmer plot:

library(ggplot2)
library(swimplot)

HN_outcomes_no_columns <- data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  Patient_ID = c(1,2,3,4,5,6,7,8,9,10,
                     11,12,13,14,15,16,17,18,19),
  Time = c(1596,1514,356,2085,1921,
           318,83,328,815,1854,159,240,821,720,2315,1800,99,
           205,177),
  Status = c("2","2","2","2","2","2",
             "2","2","2","2","2","2","2","2","2","2","2","2",
             "2"),
  Notes = c(NA,NA,"Problems with Flap",
            NA,NA,"Sarcoma primary",NA,NA,NA,NA,
            "advanced cancer",NA,NA,NA,"Admitted due to longstanding epilepsy",
            NA,NA,NA,NA)
)

as.character <- HN_outcomes_no_columns$Patient number
as.numeric <- HN_outcomes_no_columns$Time

HN_outcomes_no_columns$Patient_ID <- as.character(HN_outcomes_no_columns$Patient_ID)
HN_outcomes_no_columns$Time <- as.numeric(HN_outcomes_no_columns$Time)

swimmer_plot(df = HN_outcomes_no_columns, 
             id = "Patient_ID", 
             end = "Time", 
             fill = 'lightblue',
             width = .85)

Many thanks for your reply.

How do I rename the columns though? when I enter the following code (dplyr enabled):
H_N_outcomes_first20 <- rename(H_N_outcomes_first20, patientID=H_N_outcomes_first20$Patient number)

I get the following error message:
Error: Can't rename columns that don't exist.
x Locations 3, 4, 5, 6, 7, etc. don't exist.
i There are only 2 columns.

So still a bit stuck. Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.