'Error in swimmer plot x-axis (representing ages)
I've been around swimmer plots, and still can't seem to get the hang of it.
I've asked a question before, also involving swimmer_plot and dates, if that interests anyone.
Now I'm trying to follow pacients medication through their age at the time in the beginning of a treatment (A.Beg), complementing with the age at the end of the treatment (A.End).
But something doesn't add up in the x-axis.
The x-axis shows that there are negative values of age (false) and also makes that the end values of age are wrong, according to the x-axis (at least that's what I think is the reason).
Can anyone please help?
I'll provide the sample I took from my data:
ID A.Beg A.End Treatm
12041 1 3.252567 3.526352 A
12042 1 3.655031 3.931554 A
12043 1 4.156057 4.427105 A
12044 1 4.662560 5.212868 A
12055 1 8.717317 9.264887 A
12116 1 12.821355 12.944559 E
12127 1 13.486653 13.568789 E
13683 2 12.062971 12.145106 A
13684 2 12.062971 12.128679 C
13685 2 14.162902 14.245038 A
13686 2 14.162902 14.203970 C
library(swimplot)
swimmer_plot(df = testDF, id = 'ID', name_fill = "Treatm", col = 1,
start = 'A.Beg', end = 'A.End',
id_order = unique(testDF$PACMANID)) +
#coord_flip(ylim = c(min(swim_table_age$AgeAtBeg), max(swim_table_age$AgeAtEnd))) +
scale_y_continuous(breaks=c(0:15))+
theme_bw()
Solution 1:[1]
You have overlapping times for some of the treatments within IDs, which cannot be displayed on this type of plot (what would it look like?). If you fix the overlaps you stop confusing the internals of swimmerplot (which to be fair should probably throw an error in this case rather than miscalculating end times)
library(ggplot2)
library(swimplot)
testDF$A.Beg[8] <- testDF$A.End[9]
testDF$A.Beg[10] <- testDF$A.End[11]
swimmer_plot(df = testDF, id = 'ID', name_fill = "Treatm", col = 1,
start = 'A.Beg', end = 'A.End',
id_order = unique(testDF$ID)) +
scale_y_continuous(breaks = 0:15) +
theme_bw()

Created on 2022-04-21 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |

