ggplot2 - is there any simple way to write code in R to get swimmer plot? -
i tried below code gives error.
library(ggplot2) library(reshape2) library(dplyr) library(grid) set.seed(33) dat = data.frame(subject = 1:10, months = sample(4:20, 10, replace=true), treated=sample(0:1, 10, replace=true), stage = sample(1:4, 10, replace=true), continued=sample(0:1, 10, replace=true)) dat = dat %>% group_by(subject) %>% mutate(complete=sample(c(4:(max(months)-1),na), 1, prob=c(rep(1, length(4:(max(months)-1))),5), replace=true), partial=sample(c(4:(max(months)-1),na), 1, prob=c(rep(1, length(4:(max(months)-1))),5), replace=true), durable=sample(c(-0.5,na), 1, replace=true)) # order subjects months dat$subject = factor(dat$subject, levels=dat$subject[order(dat$months)]) # melt part of data frame adding points bars dat.m = melt(dat %>% select(subject, months, complete, partial, durable), id.var=c("subject","months")) ggplot(dat, aes(subject, months)) + geom_bar(stat="identity", aes(fill=factor(stage)), width=0.7) + geom_point(data=dat.m, aes(subject, value, colour=variable, shape=variable), size=4) + geom_segment(data=dat %>% filter(continued==1), aes(x=subject, xend=subject, y=months + 0.1, yend=months + 1), pch=15, size=0.8, arrow=arrow(type="closed", length=unit(0.1,"in"))) + coord_flip() + scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) + scale_colour_manual(values=c(hcl(seq(15,375,length.out=3)[1:2],100,40),"black")) + scale_y_continuous(limits=c(-1,20), breaks=0:20) + labs(fill="disease stage", colour="", shape="", x="subject recevied study drug") + theme_bw() + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())
it gives error unknown parameter "shape" when run program in r studio. there error in geom_segment.
please me correct code.
not quite sure, if remove pch=15, size=0.8,
looks great!
Comments
Post a Comment