Given the swimmer dataframe taken from here converted into a data frame https://blogs.sas.com/content/graphicallyspeaking/files/2014/06/Swimmer_93.txt
df %>% dplyr::glimpse()
## Observations: 15
## Variables: 9
## $ subjectID "1", "2", "3", "3", "4", "4", "5", "5", "5",...
## $ stage Stage 1, Stage 2, Stage 3, Stage 3, Stage 4,...
## $ startTime 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ endTime 18.5, 17.0, 14.0, 14.0, 13.5, 13.5, 12.5, 12...
## $ isContinued TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, T...
## $ responseType "Complete response", "Complete response", "P...
## $ responseStartTime 6.5, 10.5, 2.5, 6.0, 7.0, 11.5, 3.5, 6.5, 10...
## $ responseEndTime 13.5, 17.0, 3.5, NA, 11.0, NA, 4.5, 8.5, NA,...
## $ Durable -0.25, -0.25, -0.25, -0.25, NA, NA, -0.25, -...
df.shapes <- df %>%
# Get just the subject and response time columns
dplyr::select(subjectID, responseType, responseStartTime) %>%
# Melt the data frame, so one row per response value.
reshape2::melt(id.vars=c("subjectID", "responseType"),
value.name="time") %>%
# Remove na values
dplyr::filter(!is.na(time)) %>%
# Remove response variable column
dplyr::select(-variable) %>%
# Add 'start' to the end of the response type
dplyr::mutate(responseType=paste(responseType, "start", sep=" "))
# Add the end time for each
df.shapes %<>%
dplyr::bind_rows(df %>%
dplyr::select(subjectID, endTime, responseEndTime,
isContinued) %>%
# Place endtime as response endtime if not
# continuing and responseEndTime is NA
dplyr::mutate(responseEndTime=dplyr::if_else(
!isContinued & is.na(responseEndTime),
endTime, responseEndTime)) %>%
dplyr::select(-endTime, -isContinued) %>%
# Remove other existing NA responseEndTimes
dplyr::filter(!is.na(responseEndTime)) %>%
dplyr::mutate(responseType="Response end") %>%
dplyr::rename(time=responseEndTime))
# Append on the durable column
df.shapes %<>%
dplyr::bind_rows(df %>%
dplyr::select(subjectID, Durable) %>%
dplyr::filter(!is.na(Durable)) %>%
dplyr::mutate(responseType="Durable") %>%
dplyr::rename(time=Durable))
# Add on the arrow sets
df.shapes %<>%
dplyr::bind_rows(df %>%
dplyr::select(subjectID, endTime, isContinued) %>%
dplyr::filter(isContinued) %>%
dplyr::select(-isContinued) %>%
dplyr::mutate(responseType="Continued Treatment") %>%
dplyr::mutate(endTime=endTime+0.25) %>%
dplyr::rename(time=endTime))
responseLevels = c("Complete response start",
"Partial response start",
"Response end", "Durable", "Continued Treatment")
# Convert responseType to factor and set the levels
df.shapes %<>%
dplyr::mutate(responseType = factor(responseType,
levels=responseLevels)) %>%
# Order by response type
dplyr::arrange(desc(responseType))
Set the Unicode variables.
unicode = list(triangle=sprintf('\u25B2'),
circle=sprintf('\u25CF'),
square=sprintf('\u25A0'),
arrow=sprintf('\u2794'))
The df.shapes dataframe should look something like this
df %>% dplyr::glimpse()
## Observations: 45
## Variables: 3
## $ subjectID "1", "3", "3", "4", "4", "5", "5", "5", "6", "6",...
## $ responseType Continued Treatment, Continued Treatment, Continu...
## $ time 18.75, 14.25, 14.25, 13.75, 13.75, 12.75, 12.75, ...
Now pipe the data frame into ggplot
df %>%
# Get just the variables we need for the base of the plot
dplyr::select(subjectID, endTime, stage) %>%
# Remove duplicate rows
dplyr::distinct() %>%
# Order subject ID by numeric value
dplyr::mutate(subjectID=forcats::fct_reorder(.f=subjectID,
.x=as.numeric(subjectID),
.desc = TRUE)) %>%
# Pipe into ggplot
ggplot(aes(subjectID, endTime)) + # Base axis
geom_bar(stat="identity", aes(fill=factor(stage))) + # Bar plot
geom_point(data=df.shapes, size=5, # Use df.shapes to add reponse points
aes(subjectID, time, colour=responseType,
shape=responseType)) +
coord_flip() + # Flip to horizonal bar plot.
scale_colour_manual(values=c(RColorBrewer::brewer.pal(3, "Set1")[1:2],
rep("black", 3))) + # Add colours
scale_shape_manual(values=c(rep(unicode[["triangle"]], 2), # Add shapes
unicode[["circle"]], unicode[["square"]],
unicode[["arrow"]])) +
scale_y_continuous(limits=c(-0.5, 20), breaks=0:20) + # Set time limits
labs(fill="Disease Stage", colour="Symbol Key", shape="Symbol Key",
x="Subject ID ", y="Months since diagnosis",
title="Swimmer Plot",
caption=paste(c("Durable defined as subject with six months",
"or more of confirmed response", sep=" ") +
theme(plot.title = element_text(hjust = 0.5), # Put title in middle
plot.caption = element_text(size=7, hjust=0)) # Make caption small
Full description can be found here: http://rpubs.com/alexiswl/swimmer