0

I've been trying to make a graph that looks like this (but nicer)

Rudimentary Flow Chart

based on what I found in this discussion using the transitionPlot() function from the Gmiscpackage.

However, I can't get my transition_matrix right and I also can't seem to plot the different state classes in separate third column.

My data is based on the symptomatic improvement of patients following surgery. The numbers in the boxes are the number of patients in each "state" pre vs. post surgery. Please note the (LVAD) is not a necessity.

The data for this plot is this called df and is as follows

dput(df)
structure(list(StudyID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("P1", "P2", "P3", 
"P4", "P5", "P6", "P7"), class = "factor"), MeasureTime = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Postoperative", 
"Preoperative"), class = "factor"), NYHA = c(3L, 3L, 3L, 3L, 
3L, 2L, 3L, 1L, 3L, 1L, 3L, 3L, 1L, 1L)), .Names = c("StudyID", 
"MeasureTime", "NYHA"), row.names = c(NA, -14L), class = "data.frame")

I've made a plot in ggplot2 that looked like this

ggplot2 image

but my supervisor didn't like it, because I had to jitterthe lines so that they didn't overlap and so one could see what was happening with each patient and thus the points/lines aren't exactly lined up with the y-axis.

So I was wondering if anyone had an idea, how I'd be able to do this using the Gmisc package making what seems to me to be a transitionPlot.

Your help and time is much appreciated.

Thanks.

Community
  • 1
  • 1
OFish
  • 474
  • 1
  • 9
  • 19
  • 1
    I dont think `transitionPlot` will work as you want due to the zero cells in your transition matrix - they wont show as the boxes are scaled to the counts – user20650 Oct 16 '14 at 01:35
  • That kept on coming up as an error when I entered the zeros into the matrix...good point! – OFish Oct 16 '14 at 01:37
  • Interesting idea, adding a starter/end text-column should be rather easy although you need to know a little about the grid package in order to get it to work and I guess it would require the boxes to have fixed sizes. It is a good suggestion, not sure if I'll find the time to implement it. – Max Gordon Jun 28 '15 at 18:56

1 Answers1

2

Using your sample df data, here are some pretty low-level plotting function that can re-create your sample image. It should be straigtforward to customize however you like

First, make sure pre comes before post

df$MeasureTime<-factor(df$MeasureTime, levels=c("Preoperative","Postoperative"))

then define some plot helper functions

textrect<-function(x,y,text,width=.2) {
    rect(x-width, y-width, x+width, y+width)
    text(x,y,text)
}
connect<-function(x1,y1,x2,y2, width=.2) {
    segments(x1+width,y1,x2-width,y2)   
}

now draw the plot

plot.new()
par(mar=c(0,0,0,0))
plot.window(c(0,4), c(0,4))

with(unique(reshape(df, idvar="StudyID", timevar="MeasureTime", v.names="NYHA", direction="wide")[,-1]), 
    connect(2,NYHA.Preoperative,3,NYHA.Postoperative)
)
with(as.data.frame(with(df, table(NYHA, MeasureTime))), 
    textrect(as.numeric(MeasureTime)+1,as.numeric(as.character(NYHA)), Freq)
)

text(1, 1:3, c("I","II","III"))
text(1:3, 3.75, c("NYHA","Pre-Op","Post-Op"))
text(3.75, 2, "(LVAD)")

which results in

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks MrFlick - once again a great answer from you! I'll rate this as an answer, as it effectively does what I want, but I'd still be intrigued to find out how one can do it with `Gmisc` too! I'm trying to break down your code in my head though, so that I can modify it. `textrect` draws the rectangles+text, `connect` draws the connections. But why did you have to `reshape` the `df` into a wide format? And how do you actually command the right connections of the boxes, i.e. that the zero boxes don't get connected? – OFish Oct 16 '14 at 01:34
  • 1
    I reshape into wide format to match up the pre and post op values for an particular study ID. This tells me which nodes need to be connected. – MrFlick Oct 16 '14 at 02:10