Here are a couple of things that may help.
- 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)
)
- 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)