Advertisement
Guest User

Untitled

a guest
May 31st, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.77 KB | None | 0 0
  1. library(data.table)
  2. library(ggplot2)
  3.  
  4. # raw data
  5. data <- data.table(x = c(2,8,5,10,3,10,4,9,1,7),
  6.                    y = c(5,5,4,4,3,3,2,2,1,1),
  7.                    line_type = c("dashed","dashed","dashed","dashed","dashed","dashed","solid","solid","solid","solid"))
  8.  
  9.  
  10. # ggplot2
  11. ggplot(data = data) +
  12.     geom_line(aes(x = x, y = y, group = y, linetype = line_type)) +
  13.     geom_vline(xintercept = 10) +
  14.     labs(x = "month", y = "patient No.", title = "Follow-up from treatment") +
  15.     scale_linetype_discrete(name  ="Line",
  16.                             breaks = c("dashed", "solid"),
  17.                             labels = c("censored", "uncensored")) +
  18.     scale_x_continuous(breaks = seq(0, 10),
  19.                        labels = c("", "Jan.", "Feb.", "Mar.", "Apr.", "May",
  20.                                   "June", "July", "Aug.", "Sep.", "Oct.")) +
  21.     annotate("text", x = 9.5, y = 5, label= "End of study", size = 3) +
  22.     theme_classic() +
  23.     theme(plot.title = element_text(hjust = 0.5))
  24.  
  25.  
  26. # basic
  27. par(mar=c(5.1, 4.1, 4.1, 8.1))
  28. with(data,
  29.      plot(x, y, xlab = "month", ylab = "patient No.",
  30.           main = "Follow-up from treatment",
  31.           xlim = c(0,11), ylim = c(0,5),
  32.           type = "n",
  33.           axes = FALSE))
  34.  
  35. for(each_y in unique(data$y)) {
  36.     with(data[y == each_y],
  37.          lines(x, y, lty = line_type))
  38. }
  39. abline(v=10)
  40. text(x = 9.5, y = 5, labels = "End of study", cex = 0.8)
  41. axis(side = 1,
  42.      at = seq(1, 10),
  43.      labels = c("Jan.", "Feb.", "Mar.", "Apr.", "May",
  44.                 "June", "July", "Aug.", "Sep.", "Oct."))
  45. axis(side = 2)
  46. box()
  47.  
  48. # legend box
  49. legend("topright", inset = c(-0.35, 0), xpd = TRUE,
  50.        cex = 0.8,
  51.        legend = c("censored","uncensored"),
  52.        lty = c("dashed", "solid"),
  53.        title = "Line")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement