├── GW_base.r ├── GW_color.r ├── GW_comparisons.r ├── GW_dose.r ├── GW_dynamite.r ├── GW_implement.r ├── GW_legibility.r ├── LICENSE ├── NVSCheatSheet.pdf └── README.md /GW_base.r: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Selecting the right base graph 3 | ############################################################################### 4 | 5 | 6 | rm(list=ls()) 7 | library(ggplot2) 8 | library(dplyr) 9 | library(gridExtra) 10 | library(ggfortify) 11 | library(survival) 12 | library(reshape2) 13 | library(treemap) 14 | 15 | 16 | ####################################################### 17 | ## Novartis look and feel functions 18 | ####################################################### 19 | Novartis.Color.Palette <- structure( 20 | c( "#0460A9", "#CDDFEE", "#9BBFDD", "#68A0CB", "#03487F", "#023054", 21 | "#E74A21", "#FADBD3", "#F5B7A6", "#F1927A", "#AD3819", "#742510", 22 | "#EC9A1E", "#FBEBD2", "#F7D7A5", "#F4C278", "#B17416", "#764D0F", 23 | "#8D1F1B", "#E8D2D1", "#D1A5A4", "#BB7976", "#6A1714", "#46100E", 24 | "#7F7F7F", "#E5E5E5", "#CCCCCC", "#B2B2B2", "#5F5F5F", "#404040", 25 | "#CCCCCC", "#F5F5F5", "#EBEBEB", "#E0E0E0", "#999999", "#666666", 26 | "#404040", "#D9D9D9", "#B3B3B3", "#8C8C8C", "#303030", "#202020" 27 | ),.Dim = c(6L, 7L), 28 | .Dimnames = list(c("Hue", "Tint3", "Tint2", "Tint1", "Shade1", "Shade2"), 29 | c("Novartis Blue", "Sienna", "Apricot", "Carmine", 30 | "Gray", "Light Gray", "Dark Gray"))) 31 | 32 | ggColor <- function(n.colors = 50, color.values = c("Hue", "Tint1", "Shade1"), 33 | color.hues = c(1:4)){ 34 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 35 | 36 | rep.colors <- 1 37 | if(n.colors > length(color.vec)){ 38 | rep.colors <- ceiling(n.colors/length(color.vec)) 39 | } 40 | 41 | ggcolor <- scale_color_manual(values = rep(color.vec,rep.colors)) 42 | return(ggcolor) 43 | } 44 | 45 | ggFill <- function(n.colors = 42, color.values = c("Hue", "Tint1", "Shade1"), 46 | color.hues = c(1:4)){ 47 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 48 | 49 | rep.colors <- 1 50 | if(n.colors > length(color.vec)){ 51 | rep.colors <- ceiling(n.colors/length(color.vec)) 52 | } 53 | 54 | ggcolor <- scale_fill_manual(values = rep(color.vec,rep.colors)) 55 | return(ggcolor) 56 | } 57 | 58 | 59 | 60 | ############################################################################### 61 | ## Deviation (i.e. change from baseline) 62 | ## Example graphs for displaying deviations from a common reference point or 63 | ## baseline include line plots displaying change from baseline or a diverging 64 | ## barchart such as a waterfall plot. 65 | ############################################################################### 66 | 67 | 68 | #make data 69 | df <- data.frame(trt=rep(c("Dose 1", "Dose 2", "Dose 3"), each=4), 70 | visit=rep(c(1, 2, 3, 4),3), 71 | response=c(0, 0.7, 0.8, 0.6, 72 | 0, 0.5, 0.6, 0.2, 73 | 0, -0.2, -0.6, -0.9 )) 74 | #head(df) 75 | 76 | # define a minimal theme 77 | th_con <- theme_minimal(base_size = 12 ) + 78 | theme(legend.position="none", 79 | axis.title.y=element_blank(), 80 | axis.text.y=element_blank(), 81 | axis.title.x=element_blank(), 82 | axis.text.x=element_blank(), 83 | panel.grid.minor=element_blank(), 84 | panel.grid.major=element_blank() 85 | ) 86 | 87 | ggplot(df, aes(x=visit, y=response, group=trt)) + 88 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.6)+ 89 | geom_point(size=3) + 90 | geom_line(size=1) + 91 | scale_y_continuous(limits = c(-1, 1), breaks = c(-1,-0.5,0,0.5,1)) + 92 | th_con 93 | 94 | ggsave(file="GWBase1.png", width = 80, height = 80, units = "mm", dpi = 300) 95 | 96 | 97 | ## generate data 98 | set.seed(123) 99 | dat <- data.frame(x=1:10, ratio=sort(runif(10,0,2))) 100 | 101 | #create flag 102 | dat$col_flag <- dat$ratio > 1 103 | 104 | ggplot(dat, aes(color=col_flag)) + 105 | geom_segment(aes(x=x,xend=x,y=1, yend=ratio), size=8) + 106 | geom_hline(yintercept = 1, colour = "wheat4", linetype=1, size=1)+ 107 | theme_minimal(base_size=9) + ggColor(color.hues=c(2,1)) + 108 | theme(panel.grid.minor=element_blank(), 109 | panel.grid.major=element_blank(), 110 | #panel.border=element_blank(), 111 | axis.ticks = element_blank(), 112 | axis.text = element_blank(), 113 | legend.position="none", 114 | axis.title.x=element_blank(), 115 | axis.title.y=element_blank()) 116 | 117 | ggsave(file="GWBase2.png", width = 80, height = 80, units = "mm", dpi = 300) 118 | 119 | 120 | ############################################################################### 121 | ## Correlation 122 | ## For displaying the relationship between them two or more variables, 123 | ## x-y displays such as scatter plots and heatmaps are useful starting points. 124 | ############################################################################### 125 | 126 | 127 | set.seed(1984) 128 | 129 | 130 | # Create some data 131 | df <- data.frame(cause=c(runif(40,1,10)) ) 132 | df$effect <- df$cause*(rnorm(40,2,0.5)) + rnorm(40,0,1) 133 | 134 | # define a minimal theme 135 | th_sim <- theme_minimal(base_size = 20 ) + 136 | theme(panel.grid=element_blank(), 137 | #panel.border=element_blank(), 138 | legend.position="none", 139 | axis.title.x=element_blank(), 140 | axis.text.x=element_blank(), 141 | #axis.ticks.x=element_blank(), 142 | #axis.ticks.y=element_blank(), 143 | axis.title.y=element_blank(), 144 | axis.text.y=element_blank() 145 | 146 | ) 147 | 148 | 149 | ggplot(df, aes(x=cause, y=effect)) + 150 | geom_point(size=4.5, shape = 16,position="jitter") + 151 | geom_smooth(method = "lm") + 152 | scale_y_continuous(expand=c(0,0.5)) + 153 | scale_x_continuous(expand=c(0,0.5)) + 154 | th_sim 155 | 156 | ggsave(file="GWBase3.png", width = 80, height = 80, units = "mm", dpi = 300) 157 | 158 | 159 | mydata <- mtcars[, c(1,3,4,5,6,7)] 160 | cormat <- round(cor(mydata),2) 161 | melted_cormat <- melt(cormat) 162 | 163 | # Get lower triangle of the correlation matrix 164 | get_lower_tri<-function(cormat){ 165 | cormat[upper.tri(cormat)] <- NA 166 | return(cormat) 167 | } 168 | # Get upper triangle of the correlation matrix 169 | get_upper_tri <- function(cormat){ 170 | cormat[lower.tri(cormat)]<- NA 171 | return(cormat) 172 | } 173 | 174 | upper_tri <- get_upper_tri(cormat) 175 | melted_cormat <- melt(upper_tri, na.rm = TRUE) 176 | 177 | reorder_cormat <- function(cormat){ 178 | # Use correlation between variables as distance 179 | dd <- as.dist((1-cormat)/2) 180 | hc <- hclust(dd) 181 | cormat <-cormat[hc$order, hc$order] 182 | } 183 | 184 | # Reorder the correlation matrix 185 | cormat <- reorder_cormat(cormat) 186 | upper_tri <- get_upper_tri(cormat) 187 | 188 | # Melt the correlation matrix 189 | melted_cormat <- melt(upper_tri, na.rm = TRUE) 190 | 191 | # Create a ggheatmap 192 | ggplot(melted_cormat, aes(Var2, Var1, fill = value)) + 193 | geom_tile(color = "white") + 194 | scale_fill_gradient2(low = Novartis.Color.Palette[1,1], high = Novartis.Color.Palette[1,2], 195 | mid = "white", 196 | midpoint = 0, limit = c(-1,1), space = "Lab") + 197 | coord_fixed() + 198 | guides(fill = guide_colorbar(barwidth = 7, barheight = 1)) + 199 | theme_minimal() + 200 | theme( 201 | axis.text = element_blank(), 202 | axis.title.x = element_blank(), 203 | axis.title.y = element_blank(), 204 | legend.title = element_blank(), 205 | panel.grid = element_blank(), 206 | panel.border = element_blank(), 207 | panel.background = element_blank(), 208 | axis.ticks = element_blank(), 209 | legend.justification = c(1, 0), 210 | legend.position = c(0.6, 0.7), 211 | legend.direction = "horizontal" 212 | ) 213 | 214 | 215 | ggsave(file="GWBase4.png", width = 80, height = 80, units = "mm", dpi = 300) 216 | 217 | 218 | ############################################################################### 219 | ## Ranking 220 | ## Bar charts and dot plots are effective for disaplying quantities ordered 221 | ## highest to lowest (or vice versa). 222 | ############################################################################### 223 | 224 | my_data <- data.frame( 225 | grp = c("A", "B", "C", "D"), 226 | perc = c(1, 0.8, 0.6, 0.2) 227 | ) 228 | 229 | ggplot(my_data, aes(x = perc, y = reorder(grp, perc))) + 230 | geom_point(size = 6) + 231 | scale_x_continuous(breaks = seq(0, 1, 0.2),limits = c(0, 1)) + 232 | theme_minimal() + 233 | theme(panel.grid.major.y = element_line(colour = "grey60", size = 0.8), 234 | panel.grid.major.x = element_blank(), 235 | panel.grid.minor = element_blank(), 236 | axis.title.x=element_blank(), 237 | axis.text.x=element_blank(), 238 | axis.title.y=element_blank(), 239 | axis.text.y=element_blank() 240 | ) 241 | 242 | 243 | ggsave(file="GWBase5.png", width = 80, height = 80, units = "mm", dpi = 300) 244 | 245 | df <- data.frame(trt=c("A", "B", "C","D","E"), 246 | cause=c(1,2,4,6,10), 247 | highlight = c(2,1,2,2,2)) 248 | 249 | 250 | ggplot(df, aes(x=trt, y=cause, group=trt)) + theme_minimal(base_size=18) + 251 | geom_bar(width=0.7,fill=Novartis.Color.Palette[1,1], stat = "identity") + 252 | scale_y_continuous(breaks=c(0, 5, 10)) + 253 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=1)+ 254 | coord_flip() + 255 | theme(legend.position="none", 256 | panel.grid.minor.x = element_blank(), 257 | panel.grid.minor.y = element_blank(), 258 | panel.grid.major.y = element_blank(), 259 | axis.ticks = element_blank(), 260 | axis.title=element_blank(), 261 | axis.text=element_blank() 262 | ) 263 | 264 | ggsave(file="GWBase6.png", width = 80, height = 80, units = "mm", dpi = 300) 265 | 266 | 267 | ############################################################################### 268 | ## Distribution 269 | ## Histograms, density plots, boxplots and violin plots are common displays 270 | ## for visualising the distribution of a variable. 271 | ############################################################################### 272 | 273 | 274 | ### dummy data generation 275 | set.seed(131) 276 | 277 | mu1 <- 135.5 # population mean; 278 | sd1 <- 5.5 # sd of population mean across trials 279 | NVS301 <- rnorm(200, mu1, sd1) 280 | 281 | mu2 <- 140.1 # population mean; 282 | sd2 <- 5.9 # sd of population mean across trials 283 | NVS201 <- rnorm(200, mu2, sd2) 284 | 285 | mu3 <- 145.5 # population mean; 286 | sd3 <- 7 # sd of population mean across trials 287 | SOC101 <- rnorm(200, mu3, sd3) 288 | 289 | bp <- c(NVS301,NVS201,SOC101) 290 | 291 | trt <- 1:600 292 | trt[1:200] = "NVS301" 293 | trt[201:400] = "NVS201" 294 | trt[401:600] = "SOC101" 295 | 296 | trtn <- 1:600 297 | trtn[1:200] = 1 298 | trtn[201:400] = 2 299 | trtn[401:600] = 3 300 | 301 | 302 | # Put in to simple data frame 303 | dat1 <- data.frame( 304 | treatment = factor(trt, levels = c("NVS301", "NVS201", "SOC101")), 305 | bpm = bp 306 | ) 307 | 308 | ggplot(dat1, aes(x=as.factor(treatment), y=bpm, fill=treatment)) + 309 | geom_violin(trim=TRUE)+ggFill(color.values=c("Tint1"))+ 310 | geom_boxplot(width=0.2, fill="white")+ 311 | #scale_fill_brewer(palette="RdBu") + 312 | theme_minimal() + 313 | theme(legend.position='None', 314 | panel.grid.minor = element_blank(), 315 | panel.grid.major = element_blank(), 316 | axis.ticks.x = element_blank(), 317 | axis.title=element_blank(), 318 | axis.text=element_blank(), 319 | panel.background = element_blank() 320 | ) 321 | 322 | 323 | ggsave(file="GWBase7.png", width = 80, height = 80, units = "mm", dpi = 300) 324 | 325 | 326 | set.seed(1234) 327 | dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), 328 | rating = c(rnorm(200),rnorm(200, mean=.8))) 329 | 330 | 331 | # Histogram overlaid with kernel density curve 332 | ggplot(dat, aes(x=rating)) + 333 | geom_histogram(aes(y=..density..), binwidth=.4,colour="black", fill=Novartis.Color.Palette[1,1]) + 334 | geom_density(alpha=.1, color = "gray", fill=Novartis.Color.Palette[1,1])+ 335 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=1)+ 336 | theme_minimal() + 337 | theme(legend.position='None', 338 | panel.grid.minor = element_blank(), 339 | panel.grid.major = element_blank(), 340 | axis.ticks = element_blank(), 341 | axis.title=element_blank(), 342 | axis.text=element_blank(), 343 | panel.background = element_blank(), 344 | panel.border = element_blank() 345 | ) 346 | 347 | 348 | ggsave(file="GWBase8.png", width = 80, height = 80, units = "mm", dpi = 300) 349 | 350 | 351 | ############################################################################### 352 | ## Change over time (i.e. evolution) 353 | ## Line plots can be used for displaying how quantities evolve over time. 354 | ############################################################################### 355 | 356 | 357 | fit <- survfit(Surv(time, status) ~ sex, data = lung) 358 | gg<-autoplot(fit, conf.int = FALSE, censor = FALSE, surv.size = 2) 359 | gg + theme_minimal() + ggColor() + 360 | theme(legend.position='None', 361 | axis.ticks = element_blank(), 362 | axis.title=element_blank(), 363 | axis.text=element_blank(), 364 | panel.grid.minor = element_blank(), 365 | panel.grid.major = element_blank(), 366 | panel.background = element_blank() 367 | ) 368 | 369 | 370 | 371 | ggsave(file="GWBase9.png", width = 80, height = 80, units = "mm", dpi = 300) 372 | 373 | 374 | #make data 375 | df <- data.frame(trt=rep(c("Dose 1", "Dose 2", "Dose 3"), each=4), 376 | visit=rep(c(1, 2, 3, 10),3), 377 | response=c(0.8, 2.9, 4.2,2.5, 0.4, 2.4,3.8,1.0,0.2, 2.1,3.0,0.2)) 378 | #head(df) 379 | 380 | # define a minimal theme 381 | th_con <- theme_minimal(base_size = 12 ) + 382 | theme(legend.position="none", 383 | axis.title.y=element_blank(), 384 | axis.text.y=element_blank(), 385 | axis.title.x=element_blank(), 386 | axis.text.x=element_blank(), 387 | panel.grid.minor=element_blank(), 388 | panel.grid.major=element_blank() 389 | ) 390 | 391 | 392 | 393 | ggplot(df, aes(x=visit, y=response, group=trt, color = trt)) + 394 | geom_line(size=1.5) + ggColor() + 395 | scale_y_continuous(limits = c(0, 5), breaks = c(0,2.5, 5)) + 396 | th_con 397 | 398 | 399 | ggsave(file="GWBase10.png", width = 80, height = 80, units = "mm", dpi = 300) 400 | 401 | 402 | ############################################################################### 403 | ## Part-to-whole 404 | ## For displaying sub‐divisions of a whole (e.g. the percentage of patients 405 | ## in a subgroup), pie charts, bar charts and stacked barcharts can be used. 406 | ############################################################################### 407 | 408 | 409 | # Generate data 410 | df <- data.frame(trt=c("A","A","B", "B","C", "C"), 411 | subgroup = c("1","2","1","2","1","2"), 412 | percent=c(0.5,0.5,0.4,0.6,0.2,0.8) 413 | ) 414 | 415 | ggplot(df, aes(x = factor(trt), y = percent, fill = subgroup)) + 416 | geom_bar(width=0.6, stat="identity") + 417 | scale_y_continuous(breaks=c(0, 0.5, 1)) + ggFill() + 418 | # scale_fill_brewer(palette="Blues") + 419 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=1)+ 420 | theme_minimal(base_size=9) + 421 | theme(panel.grid.minor=element_blank(), 422 | panel.grid.major=element_blank(), 423 | panel.border=element_blank(), 424 | axis.ticks = element_blank(), 425 | axis.text = element_blank(), 426 | legend.position="none", 427 | axis.title.x=element_blank(), 428 | axis.title.y=element_blank()) 429 | 430 | 431 | ggsave(file="GWBase11.png", width = 80, height = 80, units = "mm", dpi = 300) 432 | 433 | 434 | data(business) 435 | treemap(business[business$NACE1=="C - Manufacturing",], 436 | index = c("NACE2","NACE3"), vSize = c("employees"), vColor = c("employees"), 437 | type = "index", palette = Novartis.Color.Palette[,1], fontsize.labels = 0, fontsize.title = 0) 438 | 439 | 440 | ggsave(file="GWBase12.png", width = 80, height = 80, units = "mm", dpi = 300) 441 | 442 | 443 | ############################################################################### 444 | ## Magnitude 445 | ## Dotplots, forest plots and barcharts are useful for displaying comparisons 446 | ## of size or magnitude (i.e. treatment differences) 447 | ############################################################################### 448 | 449 | df <- data.frame(trt=c("A", "B", "C","D","E"), 450 | cause=c(4,6,10,12,15), 451 | highlight = c(2,2,2,1,2)) 452 | 453 | ggplot(df, aes(x=c(4,1,5,2,3), y=cause, group=trt, fill=trt)) + 454 | geom_bar(width=0.5, stat = "identity") + 455 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 456 | ggFill(color.values = "Hue", color.hues = 1) + 457 | scale_y_continuous(breaks=c(0, 5, 10)) + 458 | theme(panel.grid.minor=element_blank(), 459 | panel.grid.major=element_blank(), 460 | axis.ticks = element_blank(), 461 | axis.text.y = element_blank(), 462 | axis.text.x = element_blank(), 463 | legend.position="none", 464 | axis.title.x=element_blank(), 465 | axis.title.y=element_blank()) 466 | 467 | 468 | ggsave(file="GWBase13.png", width = 80, height = 80, units = "mm", dpi = 300) 469 | 470 | 471 | 472 | theme_set(theme_minimal(base_size=26)) 473 | th <- theme(axis.title.x=element_blank(), 474 | axis.title.y=element_blank(), 475 | axis.text.x=element_blank(), 476 | axis.text.y=element_blank(), 477 | panel.grid.minor=element_blank(), 478 | panel.grid.major=element_blank() 479 | ) 480 | 481 | 482 | align <- data.frame(x=factor(c(1,2,3)),y=c(2,1,3),low=c(1,0,2),hi=c(3,2,4)) 483 | ggplot(align, aes(x=x, y=y, ymin=low, ymax=hi)) + 484 | geom_point(size=7) + 485 | geom_linerange(size=1) + 486 | th + 487 | coord_flip() 488 | 489 | ggsave(file="GWBase14.png", width = 80, height = 80, units = "mm", dpi = 300) 490 | -------------------------------------------------------------------------------- /GW_color.r: -------------------------------------------------------------------------------- 1 | # Color for emphasis or distinction 2 | 3 | rm(list=ls()) 4 | library(ggplot2) 5 | library(dplyr) 6 | library(reshape2) 7 | library(gridExtra) 8 | 9 | ####################################################### 10 | ## Novartis look and feel functions 11 | ####################################################### 12 | Novartis.Color.Palette <- structure( 13 | c( "#0460A9", "#CDDFEE", "#9BBFDD", "#68A0CB", "#03487F", "#023054", 14 | "#E74A21", "#FADBD3", "#F5B7A6", "#F1927A", "#AD3819", "#742510", 15 | "#EC9A1E", "#FBEBD2", "#F7D7A5", "#F4C278", "#B17416", "#764D0F", 16 | "#8D1F1B", "#E8D2D1", "#D1A5A4", "#BB7976", "#6A1714", "#46100E", 17 | "#7F7F7F", "#E5E5E5", "#CCCCCC", "#B2B2B2", "#5F5F5F", "#404040", 18 | "#CCCCCC", "#F5F5F5", "#EBEBEB", "#E0E0E0", "#999999", "#666666", 19 | "#404040", "#D9D9D9", "#B3B3B3", "#8C8C8C", "#303030", "#202020" 20 | ),.Dim = c(6L, 7L), 21 | .Dimnames = list(c("Hue", "Tint3", "Tint2", "Tint1", "Shade1", "Shade2"), 22 | c("Novartis Blue", "Sienna", "Apricot", "Carmine", 23 | "Gray", "Light Gray", "Dark Gray"))) 24 | 25 | ggColor <- function(n.colors = 50, color.values = c("Hue", "Tint1", "Shade1"), 26 | color.hues = c(1:4)){ 27 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 28 | 29 | rep.colors <- 1 30 | if(n.colors > length(color.vec)){ 31 | rep.colors <- ceiling(n.colors/length(color.vec)) 32 | } 33 | 34 | ggcolor <- scale_color_manual(values = rep(color.vec,rep.colors)) 35 | return(ggcolor) 36 | } 37 | 38 | ggFill <- function(n.colors = 42, color.values = c("Hue", "Tint1", "Shade1"), 39 | color.hues = c(1:4)){ 40 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 41 | 42 | rep.colors <- 1 43 | if(n.colors > length(color.vec)){ 44 | rep.colors <- ceiling(n.colors/length(color.vec)) 45 | } 46 | 47 | ggcolor <- scale_fill_manual(values = rep(color.vec,rep.colors)) 48 | return(ggcolor) 49 | } 50 | 51 | 52 | ## Set theme 53 | theme_set(theme_minimal(base_size=18)) 54 | th <- theme(panel.grid.minor=element_blank(), 55 | panel.grid.major=element_blank(), 56 | axis.title.y=element_blank(), 57 | axis.text.y=element_blank(), 58 | axis.title.x=element_blank(), 59 | axis.text.x=element_blank(), 60 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 61 | panel.background = element_rect(fill = "white", colour = "grey50") 62 | ) 63 | 64 | 65 | ############################################################################## 66 | ## Simple bar chart 67 | ############################################################################## 68 | 69 | # Do not use color to differentiate between categories of the same variable. 70 | # The second version of the graph below is more effective. 71 | # The reader is much more likely to compare the bars when they look alike than 72 | # when they look different. 73 | 74 | ## Make data 75 | df <- data.frame(trt=c("A", "B", "C","D","E"), 76 | cause=c(4,6,10,12,15), 77 | highlight = c(2,2,2,1,2)) 78 | 79 | a1 <- ggplot(df, aes(x=trt, y=cause, group=trt, fill=trt)) + 80 | geom_bar(width=0.5, stat = "identity") + 81 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 82 | ggFill() + 83 | scale_y_continuous(breaks=c(0, 5, 10)) + 84 | coord_flip() + 85 | theme(panel.grid.minor=element_blank(), 86 | panel.grid.major.y=element_blank(), 87 | axis.ticks = element_blank(), 88 | axis.text.y = element_blank(), 89 | axis.text.x = element_blank(), 90 | legend.position="none", 91 | axis.title.x=element_blank(), 92 | axis.title.y=element_blank()) 93 | 94 | ggsave(a1, file="GWCOL_a1.png", width = 80, height = 80, units = "mm", dpi = 300) 95 | 96 | a2 <- ggplot(df, aes(x=trt, y=cause, group=trt, fill=trt)) + 97 | geom_bar(width=0.5, stat = "identity") + 98 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 99 | ggFill(color.values = "Hue", color.hues = 1) + 100 | scale_y_continuous(breaks=c(0, 5, 10)) + 101 | coord_flip() + 102 | theme(panel.grid.minor=element_blank(), 103 | panel.grid.major.y=element_blank(), 104 | axis.ticks = element_blank(), 105 | axis.text.y = element_blank(), 106 | axis.text.x = element_blank(), 107 | legend.position="none", 108 | axis.title.x=element_blank(), 109 | axis.title.y=element_blank()) 110 | 111 | ggsave(a2, file="GWCOL_a2.png", width = 80, height = 80, units = "mm", dpi = 300) 112 | 113 | 114 | ######################################################################### 115 | ## Simple waterfall plot 116 | ######################################################################### 117 | 118 | ## Distinction 119 | ## Use colors or shades to represent meaningful differences. 120 | 121 | ## generate data 122 | set.seed(123) 123 | dat <- data.frame(x=1:10, ratio=sort(runif(10,0,2))) 124 | 125 | #create flag 126 | dat$col_flag <- dat$ratio > 1 127 | 128 | b1 <- ggplot(dat, aes(color=factor(ratio))) + 129 | geom_segment(aes(x=x,xend=x,y=1, yend=ratio), size=8) + 130 | geom_hline(yintercept = 1, colour = "wheat4", linetype=1, size=1)+ 131 | theme_minimal(base_size=9) + ggColor() + 132 | theme(panel.grid.minor=element_blank(), 133 | panel.grid.major=element_blank(), 134 | axis.ticks = element_blank(), 135 | axis.text = element_blank(), 136 | legend.position="none", 137 | axis.title.x=element_blank(), 138 | axis.title.y=element_blank() 139 | ) 140 | 141 | ggsave(b1, file="GWCOL_b1.png", width = 80, height = 80, units = "mm", dpi = 300) 142 | 143 | 144 | b2 <- ggplot(dat, aes(color=col_flag)) + 145 | geom_segment(aes(x=x,xend=x,y=1, yend=ratio), size=8) + 146 | geom_hline(yintercept = 1, colour = "wheat4", linetype=1, size=1)+ 147 | theme_minimal(base_size=9) + ggColor(color.hues=c(2,1)) + 148 | theme(panel.grid.minor=element_blank(), 149 | panel.grid.major=element_blank(), 150 | axis.ticks = element_blank(), 151 | axis.text = element_blank(), 152 | legend.position="none", 153 | axis.title.x=element_blank(), 154 | axis.title.y=element_blank()) 155 | 156 | ggsave(b2, file="GWCOL_b2.png", width = 80, height = 80, units = "mm", dpi = 300) 157 | 158 | 159 | ################################################################### 160 | ## Simple horizontal bar chart 161 | ################################################################### 162 | 163 | ## Consistency 164 | ## Be consistent. Use the same colors to mean the same things in a series of graphs. 165 | 166 | 167 | ## make data 168 | distance <- data.frame(expand.grid(study=c("Study 1","Study 2"), trt=c("A","B"))) 169 | distance$y <- c(1.2, 1.1, 1.8, 1.9) 170 | 171 | c1 <- ggplot(distance, aes(x=trt, y=y, ymin=0, ymax=y, colour=factor(paste0(study,"-",trt)))) + 172 | facet_wrap(~study, ncol=2) + geom_linerange(size=10) + 173 | th + ggColor() + 174 | theme(axis.text.y = element_blank())+ 175 | theme(legend.position = "top") + 176 | theme(axis.text.y = element_blank()) + 177 | guides(color = "none") 178 | 179 | ggsave(c1, file="GWCOL_c1.png", width = 80, height = 80, units = "mm", dpi = 300) 180 | 181 | 182 | # Panels removed, groups of interest put next to one another. 183 | 184 | c2 <- ggplot(distance, aes(x=trt, y=y, ymin=0, ymax=y, colour=factor(trt)))+ 185 | facet_wrap(~study, ncol=2) + geom_linerange(size=10) + 186 | th + ggColor() + 187 | theme(axis.text.y = element_blank())+ 188 | theme(legend.position = "top") + 189 | theme(axis.text.y = element_blank()) + 190 | guides(color = "none") 191 | 192 | ggsave(c2, file="GWCOL_c2.png", width = 80, height = 80, units = "mm", dpi = 300) 193 | 194 | 195 | ################################################################# 196 | ## Simple bar chart 197 | ################################################################# 198 | 199 | ## Using color for emphasis 200 | ## Use a darker shade or different colour to highlight/emphasize a focal point. 201 | 202 | # Make data 203 | df <- data.frame(trt=c("A", "B", "C","D","E"), 204 | cause=c(4,6,10,12,15), 205 | highlight = c(2,2,2,1,2)) 206 | 207 | d1 <- ggplot(df, aes(x=trt, y=cause, group=trt, fill=trt)) + 208 | geom_bar(width=0.5, stat = "identity") + 209 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 210 | ggFill(color.values = "Hue", color.hues = 1) + 211 | scale_y_continuous(breaks=c(0, 5, 10)) + 212 | coord_flip() + 213 | theme(panel.grid.minor=element_blank(), 214 | panel.grid.major.y=element_blank(), 215 | axis.ticks = element_blank(), 216 | axis.text.y = element_blank(), 217 | axis.text.x = element_blank(), 218 | legend.position="none", 219 | axis.title.x=element_blank(), 220 | axis.title.y=element_blank()) 221 | 222 | ggsave(d1, file="GWCOL_d1.png", width = 80, height = 80, units = "mm", dpi = 300) 223 | 224 | 225 | d2 <- ggplot(df, aes(x=trt, y=cause, group=trt, fill=factor(highlight))) + 226 | geom_bar(width=0.5, stat = "identity") + 227 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 228 | ggFill(color.values = c("Shade2","Tint1"), color.hues = c(1)) + 229 | scale_y_continuous(breaks=c(0, 5, 10)) + 230 | coord_flip() + 231 | theme(panel.grid.minor=element_blank(), 232 | panel.grid.major.y=element_blank(), 233 | axis.ticks = element_blank(), 234 | axis.text.y = element_blank(), 235 | axis.text.x = element_blank(), 236 | legend.position="none", 237 | axis.title.x=element_blank(), 238 | axis.title.y=element_blank()) 239 | 240 | 241 | ggsave(d2, file="GWCOL_d2.png", width = 80, height = 80, units = "mm", dpi = 300) 242 | 243 | 244 | 245 | ######################################################################### 246 | ## Simple line plot 247 | ######################################################################### 248 | 249 | ## Using color for emphasis 250 | ## Use a darker shade or different colour to highlight/emphasize a focal point. 251 | ## Same example but with a line plot as an illustration. 252 | 253 | # make data 254 | df <- data.frame(trt=rep(c("Dose 1", "Dose 2", "Dose 3"), each=4), 255 | highlight=rep(c("Y", "N", "Y"), each=4), 256 | visit=rep(c(1, 2, 3, 10),3), 257 | response=c(0.8, 2.9, 4.2,2.5, 0.4, 2.4,3.8,1.0,0.2, 2.1,3.0,0.2)) 258 | #head(df) 259 | 260 | # define a minimal theme 261 | th_con <- theme_minimal(base_size = 12 ) + 262 | theme(legend.position="none", 263 | axis.title.y=element_blank(), 264 | axis.text.y=element_blank(), 265 | axis.title.x=element_blank(), 266 | axis.text.x=element_blank() 267 | ) 268 | 269 | ## Dummy color value 270 | e1 <- ggplot(df, aes(x=visit, y=response, group=trt, color = 'Y')) + 271 | geom_line(size=1.5) + ggColor(color.values = "Tint1", color.hues = c(1)) + 272 | scale_y_continuous(limits = c(0, 5), breaks = c(0,2.5, 5)) + 273 | th_con 274 | 275 | ggsave(e1, file="GWCOL_e1.png", width = 80, height = 80, units = "mm", dpi = 300) 276 | 277 | 278 | ## Use highlight dummy value for highlighting the dose 279 | e2 <- ggplot(df, aes(x=visit, y=response, group=trt, color = highlight)) + 280 | geom_line(size=1.5) + ggColor(color.values = c("Shade2","Tint1"), color.hues = c(1)) + 281 | scale_y_continuous(limits = c(0, 5), breaks = c(0,2.5, 5)) + 282 | th_con 283 | 284 | ggsave(e2, file="GWCOL_e2.png", width = 80, height = 80, units = "mm", dpi = 300) 285 | 286 | ######################################################################### 287 | ## Simple scatter plot 288 | ######################################################################### 289 | 290 | ## Thinking about gridlines 291 | ## Soften gridlines using a light colour such as gray and reduce the size to be less 292 | ## prominent than the data. 293 | 294 | 295 | set.seed(1) 296 | 297 | # Create some data 298 | df <- data.frame(cause=c(rnorm(12,2,2)), 299 | effect=c(rnorm(12,2,2)) 300 | ) 301 | 302 | f1 <- ggplot(df, aes(x=cause, y=effect)) + 303 | geom_point(size=5) + 304 | theme_linedraw(base_size=12) + 305 | theme(legend.position="none", 306 | axis.title.y=element_blank(), 307 | axis.text.y=element_blank(), 308 | axis.title.x=element_blank(), 309 | axis.text.x=element_blank(), 310 | panel.grid.major=element_line(size = 1) 311 | ) 312 | 313 | ggsave(f1, file="GWCOL_f1.png", width = 80, height = 80, units = "mm", dpi = 300) 314 | 315 | f2 <- ggplot(df, aes(x=cause, y=effect)) + 316 | geom_point(size=5) + 317 | theme_minimal(base_size = 12 ) + 318 | theme(panel.grid.minor=element_blank(), 319 | legend.position="none", 320 | axis.title.y=element_blank(), 321 | axis.text.y=element_blank(), 322 | axis.title.x=element_blank(), 323 | axis.text.x=element_blank(), 324 | panel.grid.major=element_line(size = 1, color = rgb(0.75,0.75,0.75)) 325 | ) 326 | 327 | 328 | ggsave(f2, file="GWCOL_f2.png", width = 80, height = 80, units = "mm", dpi = 300) 329 | 330 | 331 | 332 | ################################################################ 333 | ## Plot all examples in to a single file 334 | ################################################################ 335 | all <- grid.arrange(a1, a2, b1, b2, c1, c2, d1, d2, e1, e2, f1, f2, ncol = 4) 336 | ggsave(all, file="GWCOL_all.png", width = 280, height = 220, units = "mm", dpi = 300) 337 | -------------------------------------------------------------------------------- /GW_comparisons.r: -------------------------------------------------------------------------------- 1 | rm(list=ls()) 2 | library(dplyr) 3 | library(ggplot2) 4 | 5 | ####################################################### 6 | ## Novartis look and feel functions 7 | ####################################################### 8 | Novartis.Color.Palette <- structure( 9 | c( "#0460A9", "#CDDFEE", "#9BBFDD", "#68A0CB", "#03487F", "#023054", 10 | "#E74A21", "#FADBD3", "#F5B7A6", "#F1927A", "#AD3819", "#742510", 11 | "#EC9A1E", "#FBEBD2", "#F7D7A5", "#F4C278", "#B17416", "#764D0F", 12 | "#8D1F1B", "#E8D2D1", "#D1A5A4", "#BB7976", "#6A1714", "#46100E", 13 | "#7F7F7F", "#E5E5E5", "#CCCCCC", "#B2B2B2", "#5F5F5F", "#404040", 14 | "#CCCCCC", "#F5F5F5", "#EBEBEB", "#E0E0E0", "#999999", "#666666", 15 | "#404040", "#D9D9D9", "#B3B3B3", "#8C8C8C", "#303030", "#202020" 16 | ),.Dim = c(6L, 7L), 17 | .Dimnames = list(c("Hue", "Tint3", "Tint2", "Tint1", "Shade1", "Shade2"), 18 | c("Novartis Blue", "Sienna", "Apricot", "Carmine", 19 | "Gray", "Light Gray", "Dark Gray"))) 20 | 21 | ggColor <- function(n.colors = 50, color.values = c("Hue", "Tint1", "Shade1"), 22 | color.hues = c(1:4)){ 23 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 24 | 25 | rep.colors <- 1 26 | if(n.colors > length(color.vec)){ 27 | rep.colors <- ceiling(n.colors/length(color.vec)) 28 | } 29 | 30 | ggcolor <- scale_color_manual(values = rep(color.vec,rep.colors)) 31 | return(ggcolor) 32 | } 33 | 34 | ggFill <- function(n.colors = 42, color.values = c("Hue", "Tint1", "Shade1"), 35 | color.hues = c(1:4)){ 36 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 37 | 38 | rep.colors <- 1 39 | if(n.colors > length(color.vec)){ 40 | rep.colors <- ceiling(n.colors/length(color.vec)) 41 | } 42 | 43 | ggcolor <- scale_fill_manual(values = rep(color.vec,rep.colors)) 44 | return(ggcolor) 45 | } 46 | 47 | 48 | 49 | ################################################################################## 50 | ## Facilitating Comparisons - Proximity & Alignment 51 | ## Demostration of direct labels on a simple line graph 52 | ## Direct labels reduce burden to decode 53 | ################################################################################## 54 | 55 | 56 | 57 | ## make data 58 | df <- data.frame(trt=rep(c("A", "B", "C"), each=4), 59 | visit=rep(c(1, 2, 3, 4),3), 60 | response=c(4.9,3.2,2.1,2.5, 61 | 3.4,3.8,1.3,0.8, 62 | 4.1,3.6,2.1,1.5) 63 | ) 64 | 65 | ## plot with legend look up 66 | ggplot(df, aes(x=visit, y=response, group=trt, colour=trt)) + 67 | geom_line(size=1.35, aes(colour=trt)) + 68 | scale_x_continuous(limits = c(0.5, 4.5), breaks = c(1,2,3,4)) + 69 | ggColor() + 70 | theme_minimal(base_size = 10 ) + 71 | theme(panel.grid.minor=element_blank(), 72 | panel.grid.major=element_blank(), 73 | axis.title.y=element_blank(), 74 | axis.text.y=element_blank(), 75 | axis.title.x=element_blank(), 76 | axis.text.x=element_blank(), 77 | legend.title=element_blank(), 78 | panel.background = element_rect(fill = "white", colour = "grey50"), 79 | legend.text=element_text(size = 20), 80 | legend.justification=c(0,0), legend.position=c(0,0) 81 | ) 82 | 83 | ggsave(file="Plot15a.png", width = 80, height = 80, units = "mm", dpi = 300) 84 | 85 | 86 | ## plot with direct label 87 | ggplot(df, aes(x=visit, y=response, group=trt)) + 88 | geom_line(size=1.35, aes(colour=trt)) + 89 | scale_x_continuous(limits = c(0.5, 4.5), breaks = c(1,2,3,4)) + 90 | ggColor()+ 91 | geom_text(data = df[df$visit == "4",], 92 | aes(label = trt), size = 8, hjust = -0.5, vjust = 0.25) + 93 | theme_minimal(base_size = 10 ) + 94 | theme(panel.grid.minor=element_blank(), 95 | panel.grid.major=element_blank(), 96 | axis.title.y=element_blank(), 97 | axis.text.y=element_blank(), 98 | axis.title.x=element_blank(), 99 | axis.text.x=element_blank(), 100 | legend.title=element_blank(), 101 | panel.background = element_rect(fill = "white", colour = "grey50"), 102 | legend.position="none" 103 | ) 104 | 105 | ggsave(file="Plot15b.png", width = 80, height = 80, units = "mm", dpi = 300) 106 | 107 | 108 | 109 | ################################################################################### 110 | ## Demostration of a panelled and overlayed density plot 111 | ## Proximity 112 | ## Keep quantities to be compared close together. 113 | ################################################################################### 114 | 115 | ## Make data 116 | set.seed(12345666) 117 | n <- 100 118 | overlay <- data.frame(v=c("1","2"), mu=c(1,1.2)) 119 | overlay <- overlay[rep(c(1,2),n),] 120 | overlay$s <- rnorm(n=2*n,mean=overlay$mu, sd=1) 121 | 122 | 123 | ## Set theme 124 | theme_set(theme_minimal(base_size=18)) 125 | th <- theme(panel.grid.minor=element_blank(), 126 | panel.grid.major=element_blank(), 127 | axis.title.y=element_blank(), 128 | axis.text.y=element_blank(), 129 | axis.title.x=element_blank(), 130 | axis.text.x=element_blank(), 131 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 132 | panel.background = element_rect(fill = "white", colour = "grey50") 133 | ) 134 | 135 | 136 | ## Plot by panel 137 | ggplot(overlay, aes(x=s)) + 138 | facet_wrap(~v, ncol=1) + 139 | geom_line(stat="density",size=1) + 140 | th 141 | 142 | ggsave(file="Plot16a.png", width = 80, height = 80, units = "mm", dpi = 300) 143 | 144 | 145 | ## Plot by overlay 146 | ggplot(overlay, aes(x=s, colour=factor(v))) + 147 | geom_line(stat="density", size=1) + 148 | ggColor() + th + 149 | theme(legend.position = "none") + 150 | guides(color = guide_legend(title=NULL))+ 151 | geom_text(data=data.frame(x=c(1,2), y=c(0.4, 0.3), v = c("1", "2")), 152 | aes(x= x, y= y, label = factor(v), color = factor(v)),hjust = -0.1, vjust = 0.25, size = 8) 153 | 154 | ggsave(file="Plot16b.png", width = 80, height = 80, units = "mm", dpi = 300) 155 | 156 | 157 | 158 | #################################################################################### 159 | ## Ordering of a simple dot plot 160 | ## Ordering values is helpful for comparing across many categories. 161 | #################################################################################### 162 | 163 | set.seed(12345666) 164 | theme_set(theme_minimal(base_size=18)) 165 | ordering <- data.frame(v=LETTERS[1:7], y=runif(7)) 166 | 167 | ggplot(ordering, aes(y=y, x=v)) + 168 | geom_point(size=5) + 169 | coord_flip() + 170 | theme(panel.grid.major.y = element_line(colour = "grey60", size = 0.8), 171 | panel.grid.minor.y = element_blank(), 172 | panel.grid.major.x = element_blank(), 173 | panel.grid.minor.x = element_blank(), 174 | axis.title.x=element_blank(), 175 | axis.text.x=element_blank(), 176 | axis.title.y=element_blank() 177 | ) 178 | 179 | ggsave(file="Plot17a.png", width = 80, height = 80, units = "mm", dpi = 300) 180 | 181 | 182 | ## order plot to display a ranking 183 | ordering <- arrange(ordering, y) 184 | ggplot(ordering, aes(y=y, x=factor(v, levels=ordering$v))) + 185 | geom_point(size=5) + 186 | coord_flip() + 187 | theme(panel.grid.major.y = element_line(colour = "grey60", size = 0.8), 188 | panel.grid.minor.y = element_blank(), 189 | panel.grid.major.x = element_blank(), 190 | panel.grid.minor.x = element_blank(), 191 | axis.title.x=element_blank(), 192 | axis.text.x=element_blank(), 193 | axis.title.y=element_blank() 194 | #axis.text.y=element_blank() 195 | ) 196 | 197 | ggsave(file="Plot17b.png", width = 80, height = 80, units = "mm", dpi = 300) 198 | 199 | 200 | ######################################################################################## 201 | ## Simple forest plot 202 | ## Oriernation can simplify comparisons 203 | ######################################################################################## 204 | ## Set common theme 205 | theme_set(theme_minimal(base_size=18)) 206 | th <- theme(panel.grid.minor.y = element_blank(), 207 | panel.grid.major.y = element_blank(), 208 | panel.grid.major.x = element_blank(), 209 | panel.grid.minor.x = element_blank(), 210 | panel.background = element_rect(fill = "white", colour = "grey50"), 211 | axis.title.y=element_blank(), 212 | axis.title.x=element_blank(), 213 | axis.text.x=element_blank(), 214 | axis.text.y=element_blank()) 215 | 216 | ## Make data 217 | align <- data.frame(x=factor(c(1,2,3)),y=c(2,1,3),low=c(1,0,2),hi=c(3,2,4)) 218 | 219 | ggplot(align, aes(x=x, y=y, ymin=low, ymax=hi)) + 220 | geom_point(size=5) + 221 | geom_linerange(size=1) + 222 | th 223 | 224 | ggsave(file="Plot18a.png", width = 80, height = 80, units = "mm", dpi = 300) 225 | 226 | ## flip the orientation to support a simpler comparison 227 | last_plot() + coord_flip() 228 | 229 | ggsave(file="Plot18b.png", width = 80, height = 80, units = "mm", dpi = 300) 230 | 231 | 232 | ######################################################################################### 233 | ## Simple forest plot 234 | ## Differences - do the calculation 235 | ## Avoid the reviewer making the calculation; 236 | ## show the difference where applicable 237 | ## e.g. plot the mean difference rather individual means. 238 | ######################################################################################### 239 | 240 | ## Set theme 241 | theme_set(theme_minimal(base_size=18)) 242 | th <- theme(panel.grid.minor=element_blank(), 243 | panel.grid.major=element_blank(), 244 | axis.title.y=element_blank(), 245 | axis.text.y=element_blank(), 246 | axis.title.x=element_blank(), 247 | axis.text.x=element_blank(), 248 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 249 | panel.background = element_rect(fill = "white", colour = "grey50") 250 | ) 251 | 252 | 253 | ## Make data 254 | difference <- data.frame(grp=c("PBO", "TRT1", "TRT2"), 255 | y=c(1,2,2.2), 256 | low=c(0,1,1.1), 257 | hi=c(2,3,3.1)) 258 | 259 | ggplot(difference, aes(x=grp, y=y, ymin=low, ymax=hi)) + 260 | geom_pointrange(size=1) + 261 | scale_y_continuous(breaks=c(0,1,2,3)) + 262 | coord_flip() + 263 | theme(panel.grid.minor=element_blank(), 264 | panel.grid.major=element_blank(), 265 | axis.title.x=element_blank(), 266 | axis.text.x=element_blank(), 267 | axis.title.y=element_blank(), 268 | plot.background = element_rect(fill = "white", colour = "grey50") 269 | ) 270 | 271 | ggsave(file="Plot19a.png", width = 80, height = 80, units = "mm", dpi = 300) 272 | 273 | 274 | difference2 <- data.frame(grp=c("TRT1\n-PBO","TRT2\n-PBO"), 275 | y=c(1,1.2), 276 | low=c(-0.3,-0.1), 277 | hi=c(2.3,2.5)) 278 | 279 | ggplot(difference2, aes(x=grp, y=y, ymin=low, ymax=hi)) + 280 | geom_hline(yintercept=0, linetype=2) + 281 | geom_pointrange(size=1) + 282 | scale_y_continuous(breaks=c(0,1,2,3)) + 283 | coord_flip() + 284 | theme(panel.grid.minor=element_blank(), 285 | panel.grid.major=element_blank(), 286 | axis.title.x=element_blank(), 287 | axis.text.x=element_blank(), 288 | axis.title.y=element_blank(), 289 | plot.background = element_rect(fill = "white", colour = "grey50") 290 | ) 291 | 292 | ggsave(file="Plot19b.png", width = 80, height = 80, units = "mm", dpi = 300) 293 | 294 | 295 | #################################################################################### 296 | ## Simple scatter plot 297 | ## Layering and visual anchoring 298 | ## Draw attention to the comparison by utilising grid lines, 299 | ## reference lines and other visual anchors to add layer and context. 300 | #################################################################################### 301 | 302 | 303 | set.seed(12345666) 304 | 305 | # make data 306 | grl <- data.frame(x=c(0,1,2,4,8,12,24,48,72,84), y=rnorm(200)) 307 | 308 | ## Set theme 309 | theme_set(theme_minimal(base_size=18)) 310 | th <- theme(panel.grid.minor=element_blank(), 311 | panel.grid.major=element_blank(), 312 | axis.title.y=element_blank(), 313 | axis.text.y=element_blank(), 314 | axis.title.x=element_blank(), 315 | axis.text.x=element_blank(), 316 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 317 | panel.background = element_rect(fill = "white", colour = "grey50") 318 | ) 319 | 320 | 321 | 322 | ## plot 323 | ggplot(grl, aes(x=x, y=y)) + 324 | geom_point(size=5) + 325 | th 326 | 327 | ggsave(file="Plot20a.png", width = 80, height = 80, units = "mm", dpi = 300) 328 | 329 | 330 | last_plot() + 331 | geom_hline(yintercept=0, linetype=1, color="red", size=4) + 332 | theme(panel.grid.minor=element_line(color = "lightgrey", size = 0.5), 333 | panel.grid.major=element_line(color = "lightgrey", size = 0.5) 334 | ) 335 | ggsave(file="Plot20b.png", width = 80, height = 80, units = "mm", dpi = 300) 336 | 337 | -------------------------------------------------------------------------------- /GW_dose.r: -------------------------------------------------------------------------------- 1 | # Putting it all together 2 | library(ggplot2) 3 | 4 | set.seed(12345666) 5 | Time = rep(c(0.5,1,2,4,8,12,18,24)) 6 | N = 6 7 | DOSE = 100*exp(0.4*rnorm(N)) 8 | ID = sort(rep(seq(1,N,1),length(Time))) 9 | Time <- rep(Time, N) 10 | K1 = 0.1*exp(0.2*rnorm(N)) 11 | K2 = 0.8*exp(0.4*rnorm(N)) 12 | 13 | my.data <- data.frame(ID=seq(1,N,1), K1, K2, DOSE) 14 | my.data <- merge(my.data, data.frame(Time = Time, ID=ID)) 15 | 16 | my.data$Concentration = my.data$DOSE*(my.data$K2*my.data$K1/(my.data$K2-my.data$K1))*(exp(-my.data$K1*(my.data$Time)) - exp(-my.data$K2*(my.data$Time)) )*exp(0.3*rnorm(length(my.data$Time))) + 0.2*rnorm(length(my.data$Time)) 17 | 18 | my.data2 <- data.frame(my.data, ID2 = 1) 19 | for(id in 2:N){ 20 | my.data2 <- rbind(my.data2, data.frame(my.data, ID2 = id)) 21 | } 22 | 23 | temp <- my.data2$ID 24 | my.data2$ID = my.data2$ID2 25 | my.data2$ID2 <- temp 26 | 27 | gg <- ggplot(data = my.data, aes(x = Time, y = Concentration)) + theme_gray(base_size = 14) 28 | gg <- gg + geom_line(aes(group = ID, color = factor(ID), linetype = factor(ID)), size = 1) 29 | gg <- gg + geom_point(aes(color = factor(ID), shape = factor(ID)), size = 4) 30 | gg <- gg + scale_y_log10() + ggColor() 31 | gg <- gg + theme( legend.position="none" ) 32 | gg 33 | 34 | ggsave(file="Plot41a.png", width = 80, height = 80, units = "mm", dpi = 300) 35 | 36 | 37 | gg <- ggplot(data = my.data, aes(x = Time, y = Concentration)) + theme_minimal(base_size = 14) 38 | gg <- gg + geom_line(aes(group = ID, linetype = factor(ID)), size = 1) 39 | gg <- gg + scale_y_log10() + ggColor()+ xlab("Time (hours)") + ylab("Concentration (ng/mL)") 40 | gg <- gg + theme(panel.grid.minor=element_blank(), legend.position="none" ) 41 | gg 42 | 43 | ggsave(file="Plot41b.png", width = 80, height = 80, units = "mm", dpi = 300) 44 | 45 | 46 | gg <- ggplot(data = my.data2, aes(x = Time, y = Concentration)) + theme_minimal(base_size = 14) 47 | gg <- gg + geom_line(aes(group = ID2), size = 1, color = rgb(0.75,0.75,0.75)) + scale_y_log10() + ggColor() 48 | gg <- gg + geom_line(data = my.data, aes(x = Time, y = Concentration,group = ID), size = 1)+ 49 | scale_x_continuous(breaks = c(12,24)) + xlab("Time (hours)") + ylab("Concentration (ng/mL)") 50 | gg <- gg + theme(panel.grid.minor=element_blank(), 51 | legend.position="none", 52 | strip.text.x = element_blank() 53 | ) 54 | gg + facet_wrap(~ID) 55 | 56 | 57 | ggsave(file="Plot41c.png", width = 80, height = 80, units = "mm", dpi = 300) 58 | 59 | 60 | -------------------------------------------------------------------------------- /GW_dynamite.r: -------------------------------------------------------------------------------- 1 | require(graphics) 2 | library(ggplot2) 3 | library(ggthemes) 4 | library(MASS) 5 | library(gridExtra) 6 | 7 | mm1 <- function(...) { 8 | mean_cl_normal(...,mult=1) 9 | } 10 | mm2 <- function(...) { 11 | mean_cl_normal(...,mult=2) 12 | } 13 | 14 | 15 | g1 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease, fill = treatment))+ 16 | theme_gray(base_size = 12)+ 17 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 18 | stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=0.5) + 19 | stat_summary(fun.data=mean_cl_normal,geom="bar") + ggFill(color.value = "Hue")+ 20 | theme(legend.position="none", 21 | panel.grid.minor=element_line(color = "black"), 22 | panel.grid.major = element_line(color = "black"), 23 | panel.border=element_blank(), 24 | axis.title.y=element_blank(), 25 | axis.title.x=element_blank() 26 | ) 27 | 28 | g2 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease)) + theme_minimal(base_size = 14)+ 29 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 30 | stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=0.5) + 31 | stat_summary(fun.data=mean_cl_normal,geom="bar") + 32 | theme(legend.position="none", 33 | panel.grid.minor=element_blank(), 34 | panel.background=element_blank(), 35 | axis.title.y=element_blank(), 36 | axis.title.x=element_blank() 37 | ) 38 | 39 | g3 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease)) + theme_minimal(base_size = 14)+ 40 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 41 | stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=0.5) + 42 | stat_summary(fun.data=mean_cl_normal,geom="bar",width=0.7) + 43 | theme(legend.position="none", 44 | panel.grid.minor=element_blank(), 45 | panel.grid.major=element_line(size = 0.5, color = "gray96"), 46 | panel.background=element_blank(), 47 | axis.title.y=element_blank(), 48 | axis.title.x=element_blank() 49 | ) 50 | 51 | g4 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease)) + theme_minimal(base_size = 14)+ 52 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 53 | stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=0.5, size = 1) + 54 | stat_summary(fun.data=mean_cl_normal,geom="point",size=3) + 55 | xlab("Treatment") + ylab("Effect") + 56 | theme(legend.position="none", 57 | panel.grid.minor=element_blank(), 58 | panel.grid.major=element_line(size = 0.5, color = "gray96"), 59 | panel.background=element_blank() 60 | ) 61 | 62 | 63 | 64 | g5 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease))+ 65 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 66 | stat_summary(fun.data=mm2,geom="linerange", size = 0.7)+ 67 | stat_summary(fun.data=mean_cl_normal,geom="point") + 68 | xlab("Treatment") + ylab("Effect") + 69 | theme(legend.position="none", 70 | panel.grid.minor=element_blank(), 71 | panel.grid.major=element_line(size = 0.5, color = "gray96"), 72 | panel.background=element_blank() 73 | ) 74 | 75 | 76 | 77 | g6 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease))+ 78 | scale_y_continuous(limits = c(0,100), breaks = c(0,25,50,75,100)) + 79 | stat_summary(fun.data=mm2,geom="linerange", size = 0.7)+ 80 | stat_summary(fun.data=mm1,geom="linerange",size=1.1, color = "red", position=position_dodge(width=0))+ 81 | stat_summary(fun.data=mean_cl_normal,geom="point") + 82 | xlab("Treatment") + ylab("Effect") + 83 | theme(legend.position="none", 84 | panel.grid.minor=element_blank(), 85 | panel.grid.major=element_line(size = 0.5, color = "gray96"), 86 | panel.background=element_blank() 87 | ) 88 | 89 | 90 | 91 | g1 92 | ggsave(file="Plot42a.png", width = 80, height = 80, units = "mm", dpi = 300) 93 | 94 | g2 95 | ggsave(file="Plot42b.png", width = 80, height = 80, units = "mm", dpi = 300) 96 | 97 | g3 98 | ggsave(file="Plot42c.png", width = 80, height = 80, units = "mm", dpi = 300) 99 | 100 | g4 101 | ggsave(file="Plot42d.png", width = 80, height = 80, units = "mm", dpi = 300) 102 | 103 | g5 104 | ggsave(file="Plot42e.png", width = 80, height = 80, units = "mm", dpi = 300) 105 | 106 | g6 107 | ggsave(file="Plot42f.png", width = 80, height = 80, units = "mm", dpi = 300) 108 | 109 | -------------------------------------------------------------------------------- /GW_implement.r: -------------------------------------------------------------------------------- 1 | # Implementation Considerations 2 | 3 | library(ggplot2) 4 | library(graphics) 5 | library(ggplot2) 6 | library(MASS) 7 | library(scales) #scale_x_date() 8 | library(gridExtra) 9 | 10 | ## Set theme 11 | theme_set(theme_minimal(base_size=18)) 12 | th <- theme(panel.grid.minor=element_blank(), 13 | panel.grid.major=element_blank(), 14 | axis.title.y=element_blank(), 15 | axis.text.y=element_blank(), 16 | axis.title.x=element_blank(), 17 | axis.text.x=element_blank(), 18 | strip.background = element_rect(fill = "lightgrey", colour = "lightgrey"), 19 | panel.background = element_rect(fill = "white", colour = "lightgrey") 20 | ) 21 | 22 | 23 | ############################################################ 24 | ## Simple scatter plot - cause and effect 25 | ############################################################ 26 | ## Cause and effect 27 | 28 | # For xy plots display the cause on the x-axis and the effect on the y-axis. 29 | set.seed(12345666) 30 | my_data <- data.frame( 31 | Bodyweight = 90 + 20*c(-runif(50), runif(50)) 32 | ) 33 | my_data$AUC <- ((my_data$Bodyweight/90)^-0.75)*exp(0.1*rnorm(length(my_data$Bodyweight))) 34 | 35 | ggplot(my_data, aes(x = AUC, y = Bodyweight)) + 36 | geom_smooth(method = "lm") + 37 | geom_point() + 38 | theme_minimal(base_size = 18 ) + 39 | theme(panel.grid.minor=element_blank(), 40 | #panel.grid.major=element_blank(), 41 | legend.position="none", 42 | axis.ticks = element_blank(), 43 | axis.text.y=element_blank(), 44 | axis.text.x=element_blank(), 45 | panel.background = element_rect(fill = "white", colour = "lightgrey") 46 | ) 47 | 48 | ggsave(file="GWImp_a1.png", width = 80, height = 80, units = "mm", dpi = 300) 49 | 50 | ggplot(my_data, aes(x = Bodyweight, y = AUC)) + 51 | geom_smooth(method = "lm")+ 52 | geom_point() + theme_minimal(base_size = 18 ) + 53 | theme(panel.grid.minor=element_blank(), 54 | #panel.grid.major=element_blank(), 55 | legend.position="none", 56 | axis.ticks = element_blank(), 57 | axis.text.y=element_blank(), 58 | axis.text.x=element_blank(), 59 | panel.background = element_rect(fill = "white", colour = "lightgrey") 60 | ) 61 | 62 | ggsave(file="GWImp_a2.png", width = 80, height = 80, units = "mm", dpi = 300) 63 | 64 | 65 | ################################################################### 66 | ## Simple scatter plot - Aspect ratio 67 | #################################################################### 68 | 69 | # The selection of the aspect ratio can influence the interpretation of a line graph. 70 | # Try to display lesser height versus greater length. 71 | # Aim to scale the representation to have approximately a 45 degree angle of change. 72 | 73 | set.seed(12345666) 74 | my_data <- data.frame( 75 | x = 50*rnorm(1000) 76 | ) 77 | my_data$y <- (0.2*my_data$x+0.8*50*rnorm(length(my_data$x))) 78 | 79 | p <- ggplot(my_data, aes(x = x, y = y)) + 80 | geom_point() + theme_minimal(base_size = 18 ) + 81 | theme_bw(base_size=16) + 82 | theme(panel.grid.minor=element_blank(), 83 | panel.grid.major=element_blank(), 84 | legend.position="none", 85 | axis.title.y=element_blank(), 86 | axis.title.x = element_blank(), 87 | axis.ticks = element_blank(), 88 | axis.text.y=element_blank(), 89 | axis.text.x=element_blank() 90 | ) 91 | p + coord_fixed(0.5) 92 | ggsave(file="GWImp_b1.png", width = 80, height = 80, units = "mm", dpi = 300) 93 | 94 | p + coord_fixed(ratio=3) 95 | ggsave(file="GWImp_b2.png", width = 80, height = 80, units = "mm", dpi = 300) 96 | 97 | 98 | 99 | ################################################################ 100 | ## Simple bar chart and dot plot 101 | ################################################################ 102 | 103 | ## Plotting bars on a non-linear axis 104 | ## Avoid using comparisons based on length (i.e. a bar chart) 105 | ## when displaying data on a non-linear scale (i.e. a log scale or percentage change). 106 | ## Instead display comparisons using position (i.e.a dot chart). 107 | 108 | 109 | ggplot(OrchardSprays,aes(x=treatment,y=decrease)) + 110 | stat_summary(fun.data=mean_cl_normal,geom="bar") + 111 | theme_minimal(base_size = 18 )+ 112 | scale_y_log10(limits = c(1, 100), breaks = c(1,10,100)) + 113 | xlab("Treatment") + 114 | ylab("Effect") 115 | 116 | ggsave(file="GWImp_c1.png", width = 80, height = 80, units = "mm", dpi = 300) 117 | 118 | 119 | mm1 <- function(...) { 120 | mean_cl_normal(...,mult=1) 121 | } 122 | mm2 <- function(...) { 123 | mean_cl_normal(...,mult=2) 124 | } 125 | 126 | ggplot(OrchardSprays,aes(x=treatment,y=decrease)) + 127 | stat_summary(fun.data=mm1,geom="point", color = "black",size=4) + 128 | theme_minimal(base_size = 18)+ 129 | scale_y_log10(limits = c(1, 100), breaks = c(1,10,100))+ 130 | xlab("Treatment")+ 131 | ylab("Effect") 132 | 133 | ggsave(file="GWImp_c2.png", width = 80, height = 80, units = "mm", dpi = 300) 134 | 135 | 136 | ###################################################### 137 | ## Simple dot plot including uncertainty estimates 138 | ###################################################### 139 | ## Log Scales 140 | ## Do not plot log-normally distributed variables on a linear scale 141 | ## (e.g. hazards ratio, odds ratio, AUC, CL) 142 | 143 | DF <- data.frame(Est = c(1,2,3,4,5), 144 | Point = c(-0.7, 0.7, 0.2, 0.1, -0.3), 145 | SE = c(0.1,0.1,0.1,0.8,0.9)) 146 | 147 | gg <- ggplot(data = DF) + 148 | geom_point(aes(x=Est, y= exp(Point)), size = 3) + 149 | geom_errorbar(aes(x = Est, ymin = exp(Point-2*SE), 150 | ymax = exp(Point+2*SE)), width = 0.5) + 151 | scale_y_continuous(breaks = c(0,1,2,3,4,5,6,7,8)) + 152 | geom_hline(yintercept = 1, linetype = "dashed", color = rgb(0.5,0.5,0.5)) + 153 | geom_vline(xintercept = 0, linetype = "solid", color = "black") + 154 | ylab("Odds Ratio") + 155 | theme_minimal(base_size = 18 ) + 156 | theme(panel.grid.minor=element_blank(), 157 | panel.grid.major=element_blank(), 158 | legend.position="none", 159 | axis.ticks.x = element_blank(), 160 | axis.title.x = element_blank(), 161 | axis.text.x=element_blank() 162 | ) 163 | 164 | gg 165 | ggsave(file="GWImp_d1.png", width = 80, height = 80, units = "mm", dpi = 300) 166 | 167 | gg + scale_y_log10(breaks = c(0.125,0.25,0.5,1,2,4,8), labels = c("1/8","1/4","1/2","1","2","4","8")) + 168 | geom_hline(yintercept = 1, linetype = "dashed", color = rgb(0.5,0.5,0.5)) 169 | 170 | ggsave(file="GWImp_d2.png", width = 80, height = 80, units = "mm", dpi = 300) 171 | 172 | 173 | ############################################################################# 174 | ## Simple panelled line plot 175 | ############################################################################# 176 | ## Consistent scales 177 | 178 | set.seed(12345666) 179 | Time = c(c(0.5,1,2,4,8,12,18,23.99), 3*24 + c(0.5,1,2,4,8,12,18,23.99)) 180 | DAY = floor(Time/24) 181 | DOSE = (1+DAY)*100 182 | K1 = 0.2 183 | K2 = 0.8 184 | Concentration = DOSE*(K2*K1/(K2-K1))*(exp(-K1*(Time-DAY*24)) - exp(-K2*(Time-DAY*24)) ) 185 | my.data <- data.frame(Time = Time, Concentration = Concentration, DAY = DAY, DOSE = DOSE) 186 | 187 | gg <- ggplot(data = my.data, aes(x = Time-DAY*24, y = Concentration)) + 188 | geom_point(size = 4) + 189 | geom_line(aes(group = DAY), size = 1) + scale_y_log10() + 190 | theme_minimal(base_size = 18) + 191 | theme(panel.grid.minor=element_blank(), 192 | panel.grid.major=element_blank(), 193 | legend.position="none", 194 | axis.title.y=element_blank(), 195 | axis.title.x=element_blank(), 196 | axis.ticks = element_blank(), 197 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 198 | panel.background = element_rect(fill = "white", colour = "grey50"), 199 | axis.text.x=element_blank()) 200 | 201 | gg + facet_wrap(~DOSE, scales = "free_y") 202 | ggsave(file="GWImp_e1.png", width = 80, height = 80, units = "mm", dpi = 300) 203 | 204 | gg + facet_wrap(~DOSE) 205 | ggsave(file="GWImp_e2.png", width = 80, height = 80, units = "mm", dpi = 300) 206 | 207 | 208 | ######################################################################### 209 | ## Simple line plot with disconnected trend 210 | ######################################################################### 211 | ## Disconnected scale 212 | 213 | set.seed(12345666) 214 | Time = c(c(0,0.5,1,2,4,8,12,18,23.99), 24 + seq(1,2,1)*24, 3*24 + c(0,0.5,1,2,4,8,12,18,23.99)) 215 | DAY = floor(Time/24) 216 | K1 = 0.2 217 | K2 = 0.8 218 | Concentration = 100*(K2*K1/(K2-K1))*(exp(-K1*(Time-DAY*24)) - exp(-K2*(Time-DAY*24)) + 0.25*Time/100/(Time/100 + 1)) 219 | my.data <- data.frame(Time = Time, Concentration = Concentration, DAY = DAY) 220 | 221 | gg <- ggplot(data = my.data, aes(x = Time, y = Concentration)) + 222 | geom_point(size = 4) + 223 | geom_line(aes(group = DAY), size = 1) + 224 | scale_y_log10(lim = c(0.1,100)) + 225 | theme_minimal(base_size = 18) + 226 | theme(panel.grid.minor=element_blank(), 227 | panel.grid.major=element_blank(), 228 | panel.background = element_rect(fill = "white", colour = "lightgrey"), 229 | legend.position="none", 230 | axis.title.y=element_blank(), 231 | axis.title.x=element_blank(), 232 | axis.ticks = element_blank(), 233 | axis.text.y=element_blank(), 234 | axis.text.x=element_blank() 235 | ) 236 | 237 | gg + geom_line(size = 1) 238 | ggsave(file="GWImp_f1.png", width = 80, height = 80, units = "mm", dpi = 300) 239 | 240 | gg 241 | ggsave(file="GWImp_f2.png", width = 80, height = 80, units = "mm", dpi = 300) 242 | 243 | 244 | ############################################################## 245 | ## Simple line plot with uncertainty intervals and band 246 | ############################################################## 247 | ## Inferences 248 | 249 | set.seed(12345666) 250 | Dose = seq(0,10,0.1) 251 | DAY = floor(Dose/24) 252 | K1 = 0.2 253 | K2 = 0.8 254 | Response = 100*(Dose/1)/(Dose/1 + 1) 255 | my.data <- data.frame(Dose = Dose, Response = Response, 256 | ymin = Response - 0.1*Response - 5, 257 | ymax = Response + 0.1*Response + 5, 258 | obs = Response + 259 | 5*rnorm(length(Response)) + 260 | 0.1*rnorm(length(Response))*Response, 261 | DAY = DAY) 262 | 263 | gg <- ggplot(data = my.data, aes(x = Dose, y = Response)) + 264 | theme_minimal(base_size = 18) + 265 | geom_line(size = 1) + 266 | coord_cartesian(ylim = c(-10,120)) + 267 | theme(panel.grid.minor=element_blank(), 268 | panel.grid.major=element_blank(), 269 | legend.position="none", 270 | axis.title.y=element_blank(), 271 | axis.title.x=element_blank(), 272 | axis.ticks = element_blank(), 273 | axis.text.y=element_blank(), 274 | axis.text.x=element_blank(), 275 | panel.background = element_rect(fill = "white", colour = "lightgrey") 276 | ) 277 | gg 278 | 279 | ggsave(file="GWImp_g1.png", width = 80, height = 80, units = "mm", dpi = 300) 280 | 281 | gg + geom_ribbon(aes(ymin = ymin, ymax=ymax), fill = rgb(0.5,0.5,0.5), alpha = 0.5) + 282 | geom_point(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 283 | aes(x=Dose, y=obs),size = 4) + 284 | geom_errorbar(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 285 | aes(x=Dose, ymin=obs-5-0.1*obs, ymax = obs+5+0.1*obs),size = 1) 286 | 287 | ggsave(file="GWImp_g2.png", width = 80, height = 80, units = "mm", dpi = 300) 288 | 289 | 290 | ############################################################# 291 | ## Simple boxplots on a linear scale 292 | ############################################################# 293 | ## Continuous Time Scales 294 | 295 | grl <- data.frame(x=rep(c(0,1,2,4,8,12,24),200)) 296 | grl$y <- 100*(exp(-0.5*grl$x+0.5*rnorm(length(grl$x)))+0.5*rnorm(length(grl$x))) 297 | ggplot(grl, aes(x=factor(x),y=y)) + 298 | geom_boxplot(aes(group = factor(x)),size=1, color = "black", outlier.size = 3) + 299 | theme_minimal(base_size=26) + 300 | th 301 | 302 | ggsave(file="GWImp_h1.png", width = 80, height = 80, units = "mm", dpi = 300) 303 | 304 | 305 | ggplot(grl, aes(x=x,y=y)) + 306 | geom_boxplot(aes(group = factor(x)),size=1, color = "black", outlier.size = 3) + 307 | theme_minimal(base_size=26) + 308 | th 309 | 310 | ggsave(file="GWImp_h2.png", width = 80, height = 80, units = "mm", dpi = 300) 311 | -------------------------------------------------------------------------------- /GW_legibility.r: -------------------------------------------------------------------------------- 1 | # Legibility and Clarity 2 | 3 | rm(list=ls()) 4 | library(ggplot2) 5 | library(dplyr) 6 | library(reshape2) 7 | library(gridExtra) 8 | library(survival) 9 | library(ggfortify) 10 | 11 | 12 | 13 | ####################################################### 14 | ## Novartis look and feel functions 15 | ####################################################### 16 | Novartis.Color.Palette <- structure( 17 | c( "#0460A9", "#CDDFEE", "#9BBFDD", "#68A0CB", "#03487F", "#023054", 18 | "#E74A21", "#FADBD3", "#F5B7A6", "#F1927A", "#AD3819", "#742510", 19 | "#EC9A1E", "#FBEBD2", "#F7D7A5", "#F4C278", "#B17416", "#764D0F", 20 | "#8D1F1B", "#E8D2D1", "#D1A5A4", "#BB7976", "#6A1714", "#46100E", 21 | "#7F7F7F", "#E5E5E5", "#CCCCCC", "#B2B2B2", "#5F5F5F", "#404040", 22 | "#CCCCCC", "#F5F5F5", "#EBEBEB", "#E0E0E0", "#999999", "#666666", 23 | "#404040", "#D9D9D9", "#B3B3B3", "#8C8C8C", "#303030", "#202020" 24 | ),.Dim = c(6L, 7L), 25 | .Dimnames = list(c("Hue", "Tint3", "Tint2", "Tint1", "Shade1", "Shade2"), 26 | c("Novartis Blue", "Sienna", "Apricot", "Carmine", 27 | "Gray", "Light Gray", "Dark Gray"))) 28 | 29 | ggColor <- function(n.colors = 50, color.values = c("Hue", "Tint1", "Shade1"), 30 | color.hues = c(1:4)){ 31 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 32 | 33 | rep.colors <- 1 34 | if(n.colors > length(color.vec)){ 35 | rep.colors <- ceiling(n.colors/length(color.vec)) 36 | } 37 | 38 | ggcolor <- scale_color_manual(values = rep(color.vec,rep.colors)) 39 | return(ggcolor) 40 | } 41 | 42 | ggFill <- function(n.colors = 42, color.values = c("Hue", "Tint1", "Shade1"), 43 | color.hues = c(1:4)){ 44 | color.vec <- as.vector(t(Novartis.Color.Palette[color.values,color.hues])) 45 | 46 | rep.colors <- 1 47 | if(n.colors > length(color.vec)){ 48 | rep.colors <- ceiling(n.colors/length(color.vec)) 49 | } 50 | 51 | ggcolor <- scale_fill_manual(values = rep(color.vec,rep.colors)) 52 | return(ggcolor) 53 | } 54 | 55 | 56 | ## Set theme 57 | theme_set(theme_minimal(base_size=18)) 58 | th <- theme(panel.grid.minor=element_blank(), 59 | panel.grid.major=element_blank(), 60 | axis.title.y=element_blank(), 61 | axis.text.y=element_blank(), 62 | axis.title.x=element_blank(), 63 | axis.text.x=element_blank(), 64 | strip.background = element_rect(fill = "lightgrey", colour = "grey50"), 65 | panel.background = element_rect(fill = "white", colour = "grey50") 66 | ) 67 | 68 | 69 | 70 | ############################################################################### 71 | ## Annotated dose response curve 72 | ## Label axes with clear measurement units and 73 | ## provide annotations that support the message. 74 | ############################################################################### 75 | 76 | ## make data 77 | set.seed(12345666) 78 | Dose = seq(0,10,0.1) 79 | DAY = floor(Dose/24) 80 | K1 = 0.2 81 | K2 = 0.8 82 | Response = 100*(Dose/1)/(Dose/1 + 1) 83 | my.data <- data.frame(Dose = Dose, Response = Response, 84 | ymin = Response - 0.1*Response - 5, 85 | ymax = Response + 0.1*Response + 5, 86 | obs = Response + 87 | 5*rnorm(length(Response)) + 88 | 0.1*rnorm(length(Response))*Response, 89 | DAY = DAY) 90 | 91 | ## plot 92 | a1 <- ggplot(data = my.data, aes(x = Dose, y = Response)) + 93 | geom_point(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 94 | aes(x=Dose, y=obs),size = 4) + 95 | geom_errorbar(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 96 | aes(x=Dose, ymin=obs-5-0.1*obs, ymax = obs+5+0.1*obs),size = 1) + 97 | xlab("Dose") + 98 | ylab("Response") + 99 | coord_cartesian(ylim = c(-10,120)) + 100 | geom_line(size = 1) + 101 | geom_ribbon(aes(ymin = ymin, ymax=ymax), fill = rgb(0.5,0.5,0.5), alpha = 0.5) + 102 | geom_point(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 103 | aes(x=Dose, y=obs),size = 4) + 104 | geom_errorbar(data = my.data[Dose%in%c(0.1,1,2.5,5,10),], 105 | aes(x=Dose, ymin=obs-5-0.1*obs, ymax = obs+5+0.1*obs),size = 1) + 106 | theme_bw(base_size = 16) + 107 | theme(panel.grid.minor=element_blank(), 108 | panel.grid.major=element_blank(), 109 | legend.position="none", 110 | axis.text.x=element_text(size = 12) 111 | ) 112 | 113 | ggsave(a1, file="GWLeg_a1.png", width = 80, height = 80, units = "mm", dpi = 300) 114 | 115 | ## Add informative annotations 116 | a2 <- a1 + geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1, alpha = 0.4)+ 117 | geom_ribbon(data = data.frame(x=c(0,10), y=c(80,80), ymin = c(70,70), ymax = c(90,90)), 118 | aes(x = x, y=y, ymin = ymin, ymax = ymax), fill = "red", alpha = 0.2)+ 119 | scale_x_continuous(breaks = c(0.1,1,2.5,5,10), labels = c(0.1,1,2.5,5,10)) + 120 | geom_line(data = data.frame(x = c(2.5, 8), y = c(-7,-7)), aes(x=x, y=y), color = "red") + 121 | geom_point(data = data.frame(x = 4, y=-7), aes(x=x, y=y), color = "red", size = 2) + 122 | annotate("text", label = "Target dose", x = 8, y = 0, color = "red") + 123 | annotate("text", label = "Active control", x = 8, y = 60, color = "red") + 124 | xlab("Dose (mg)") + ylab("Response") + 125 | theme(panel.grid.major=element_line(color = "lightgrey", size = 0.4)) 126 | 127 | a2 128 | ggsave(a2, file="GWLeg_a2.png", width = 80, height = 80, units = "mm", dpi = 300) 129 | 130 | 131 | 132 | ############################################################################### 133 | ## Simple survival plot 134 | ## Use font size to create a visual hierarchy 135 | ############################################################################### 136 | 137 | ## data 138 | df <- lung 139 | df$sex <- plyr::mapvalues(df$sex, c(1,2),c("Male","Female")) 140 | df$sex <- factor(df$sex, levels = c("Male","Female")) 141 | fit <- survfit(Surv(time, status) ~ sex, data = df) 142 | 143 | b1 <-autoplot(fit, conf.int = FALSE, censor = FALSE, surv.size = 2) + 144 | theme_minimal() + 145 | ggColor() + 146 | labs(title="Survival Analysis") + 147 | xlab("Time (years)") + 148 | ylab("Survival Rate (%)")+ 149 | theme(plot.title = element_text(hjust = 0.5), 150 | axis.ticks = element_blank(), 151 | axis.line = element_blank(), 152 | panel.grid.minor.y=element_blank(), 153 | panel.grid.major.y=element_blank(), 154 | panel.background=element_blank(), 155 | axis.text.x=element_text(size=10), 156 | axis.text.y=element_text(size=12), 157 | legend.title = element_blank(), 158 | legend.position=c(0.75,0.75), 159 | legend.text = element_text(size = 12), 160 | title=element_text(size=8), 161 | axis.title.x = element_text(size = 16, face = "bold"), 162 | axis.title.y = element_text(size = 12) 163 | ) 164 | ggsave(b1, file="GWLeg_b1.png", width = 80, height = 80, units = "mm", dpi = 300) 165 | 166 | 167 | b2 <-autoplot(fit, conf.int = FALSE, censor = FALSE, surv.size = 2) + 168 | theme_minimal() + ggColor() + 169 | labs(title="Survival Analysis") + 170 | xlab("Time (years)") + 171 | ylab("Survival Rate (%)")+ 172 | theme(plot.title = element_text(hjust = 0.5), 173 | axis.ticks = element_blank(), 174 | axis.line = element_blank(), 175 | #axis.text.y=element_blank(), 176 | panel.grid.minor.y=element_blank(), 177 | panel.grid.major.y=element_blank(), 178 | panel.background=element_blank(), 179 | axis.text.x=element_text(size=10), 180 | axis.text.y=element_text(size=12), 181 | legend.title = element_blank(), 182 | legend.position=c(0.75,0.75), 183 | legend.text = element_text(size = 12), 184 | title=element_text(face="bold", size=14) 185 | ) 186 | 187 | ggsave(b2, file="GWLeg_b2.png", width = 80, height = 80, units = "mm", dpi = 300) 188 | 189 | 190 | #################################################################################### 191 | ## Simple bar chart 192 | ## Don't use too small or too condensed text, Dont capitalize everything 193 | #################################################################################### 194 | 195 | # create toy data 196 | DF <- data.frame( 197 | lab = c("C4", "C3", "C2", "C1"), 198 | value = c(0.05,0.15,0.3,0.5), 199 | Category = c(4,3,2,1) 200 | ) 201 | 202 | c1 <- ggplot(DF, aes(reorder(lab,value), value)) + 203 | geom_bar(width = 0.85, stat = "identity", color = "white") + 204 | geom_text(aes(x=lab, y=value, label=value),nudge_y=0.1, size=6.5) + 205 | scale_y_continuous(limits = c(0, 1)) + 206 | labs(title="Subgroup incidence rate") + 207 | theme(plot.title = element_text(hjust = 0.5), 208 | axis.ticks = element_blank(), 209 | axis.line = element_blank(), 210 | axis.title=element_blank(), 211 | panel.grid.minor.y=element_blank(), 212 | panel.grid.major.y=element_blank(), 213 | axis.text.x=element_text(size=18), 214 | axis.text.y=element_text(size=10), 215 | legend.position="none", 216 | title=element_text(face="bold", size=8) 217 | ) + 218 | coord_flip() 219 | 220 | ggsave(c1, file="GWLeg_c1.png", width = 80, height = 80, units = "mm", dpi = 300) 221 | 222 | 223 | 224 | c2 <- ggplot(DF, aes(reorder(lab,value), value)) + 225 | geom_bar(width = 0.65, stat = "identity",color = "white") + 226 | geom_text(aes(x=lab, y=value, label=value),nudge_y=0.15, size=4) + 227 | scale_y_continuous(limits = c(0, 1)) + 228 | labs(title="Subgroup \nincidence rate") + 229 | theme(plot.title = element_text(hjust = 0.5), 230 | axis.ticks = element_blank(), 231 | axis.line = element_blank(), 232 | axis.title=element_blank(), 233 | panel.grid.minor.y=element_blank(), 234 | panel.grid.major.y=element_blank(), 235 | panel.background=element_blank(), 236 | axis.text.x=element_text(size=10), 237 | axis.text.y=element_text(size=12), 238 | legend.position="none", 239 | title=element_text(face="bold", size=14) 240 | ) + 241 | coord_flip() 242 | 243 | ggsave(c2, file="GWLeg_c2.png", width = 80, height = 80, units = "mm", dpi = 300) 244 | 245 | 246 | ############################################################################ 247 | ## Simple forest plot 248 | ## Use sans serif fonts 249 | ############################################################################ 250 | 251 | # Make data 252 | df <- data.frame(Dose=c(100, 200, 400, 800)) 253 | df$Response <- df$Dose/(df$Dose + 200) 254 | df$ymin <- df$Response*exp(0.2) 255 | df$ymax <- df$Response*exp(-0.2) 256 | 257 | # plot 258 | d1 <- ggplot(data = df) + 259 | geom_point(aes(x = Dose, y = Response), size = 4) + 260 | geom_errorbar(aes(x= Dose, ymin = ymin, ymax = ymax)) + 261 | scale_y_continuous(limits = c(0, 1)) + 262 | ggtitle("Dose Reponse") + 263 | theme_minimal(base_size = 18) + 264 | theme(title=element_text(face="bold",size=18, family="serif"), 265 | plot.title = element_text(hjust = 0.5), 266 | axis.title = element_text( family="serif"), 267 | axis.text = element_text(family = "serif", size = 12) 268 | ) 269 | 270 | ggsave(d1, file="GWLeg_d1.png", width = 80, height = 80, units = "mm", dpi = 300) 271 | 272 | 273 | d2 <- ggplot(data = df) + 274 | geom_point(aes(x = Dose, y = Response), size = 4) + 275 | geom_errorbar(aes(x= Dose, ymin = ymin, ymax = ymax)) + 276 | scale_y_continuous(limits = c(0, 1)) + 277 | ggtitle("Dose Reponse") + 278 | theme_minimal(base_size = 18) + 279 | theme(title=element_text(size=18), 280 | axis.text = element_text(size = 12), 281 | plot.title = element_text(hjust = 0.5) 282 | ) 283 | 284 | ggsave(d2, file="GWLeg_d2.png", width = 80, height = 80, units = "mm", dpi = 300) 285 | 286 | 287 | ###################################################################################### 288 | ## Simple bar chart 289 | ## Display text with enough contrast to be visible. 290 | ## Favor dark text on light backgrounds over light on dark whenever possible. 291 | ###################################################################################### 292 | 293 | # create toy data 294 | DF <- data.frame(lab = c("Category 4", "Category 3", "Category 2", "Category 1"), 295 | value = c(0.05,0.15,0.3,0.5), 296 | Category = c(4,3,2,1) 297 | ) 298 | 299 | e1 <- ggplot(DF[DF$lab!="Category 4",], aes(reorder(Category,lab), value)) + 300 | geom_bar(width = 0.65, stat = "identity",color = "white") + 301 | geom_text(aes(x=Category, y=value, label=value*100,fontface="bold"), 302 | nudge_y=-0.05, size=7, color = "white") + 303 | scale_y_continuous(limits = c(0, 1)) + 304 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8)+ 305 | theme(plot.title = element_text(hjust = 0.5), 306 | axis.ticks = element_blank(), 307 | axis.line = element_blank(), 308 | axis.text.y=element_blank(), 309 | axis.text.x = element_blank(), 310 | axis.title=element_blank(), 311 | panel.grid.minor.x=element_blank(), 312 | panel.grid.major.x=element_blank(), 313 | panel.background=element_blank(), 314 | legend.position="none", 315 | title=element_text(face="bold", size=14) 316 | ) 317 | 318 | 319 | ggsave(e1, file="GWLeg_e1.png", width = 80, height = 80, units = "mm", dpi = 300) 320 | 321 | 322 | 323 | e2 <- ggplot(DF[DF$lab!="Category 4",], aes(reorder(Category,lab), value)) + 324 | geom_bar(width = 0.65, stat = "identity",color = "white") + 325 | geom_text(aes(x=Category, y=value, label=value*100), 326 | nudge_y=0.05, size=7, color = "black") + 327 | scale_y_continuous(limits = c(0, 1)) + 328 | geom_hline(yintercept = 0, colour = "wheat4", linetype=1, size=0.8) + 329 | theme(plot.title = element_text(hjust = 0.5), 330 | axis.ticks = element_blank(), 331 | axis.line = element_blank(), 332 | axis.text.y=element_blank(), 333 | axis.text.x = element_blank(), 334 | axis.title=element_blank(), 335 | panel.grid.minor.x=element_blank(), 336 | panel.grid.major.x=element_blank(), 337 | panel.background=element_blank(), 338 | legend.position="none", 339 | title=element_text(face="bold", size=14) 340 | ) 341 | 342 | 343 | ggsave(e2, file="GWLeg_e2.png", width = 80, height = 80, units = "mm", dpi = 300) 344 | 345 | 346 | ########################################################################################## 347 | ## Simple scatter plot 348 | ## Bold and italics should only be used for layering. 349 | ## Emphasizing everything means nothing gets emphasized. 350 | ########################################################################################## 351 | 352 | ## make data 353 | set.seed(12345666) 354 | my_data <- data.frame(Bodyweight = 90 + 20*c(-runif(50), runif(50))) 355 | my_data$Exposure <- ((my_data$Bodyweight/90)^-0.75)*exp(0.1*rnorm(length(my_data$Bodyweight))) 356 | my_data$BODYWEIGHT = my_data$Bodyweight 357 | my_data$EXPOSURE <- my_data$Exposure 358 | 359 | 360 | f1 <- ggplot(my_data, aes(x = BODYWEIGHT, y = EXPOSURE)) + 361 | geom_smooth(method = "lm")+ 362 | geom_point() + 363 | theme_minimal(base_size = 18 ) + 364 | ggtitle("EXPOSURE \nVS BODYWEIGHT") + 365 | theme(panel.grid.minor=element_blank(), 366 | legend.position="none", 367 | axis.ticks = element_blank(), 368 | axis.text.y=element_blank(), 369 | axis.text.x=element_blank(), 370 | axis.title = element_text(face = "bold.italic",size = 18), 371 | plot.title = element_text(hjust = 0.5, face = "bold.italic",size = 18) 372 | ) 373 | 374 | ggsave(f1, file="GWLeg_f1.png", width = 80, height = 80, units = "mm", dpi = 300) 375 | 376 | 377 | 378 | f2 <- ggplot(my_data, aes(x = Bodyweight, y = Exposure)) + 379 | geom_smooth(method = "lm")+ 380 | geom_point() + 381 | theme_minimal(base_size = 16 ) + 382 | ggtitle("Exposure \nvs. Bodyweight")+ 383 | theme(panel.grid.minor=element_blank(), 384 | legend.position="none", 385 | axis.ticks = element_blank(), 386 | axis.text.y=element_blank(), 387 | axis.text.x=element_blank(), 388 | plot.title = element_text(hjust = 0.5) 389 | ) 390 | 391 | ggsave(f2, file="GWLeg_f2.png", width = 80, height = 80, units = "mm", dpi = 300) 392 | 393 | 394 | 395 | ###################################################################################### 396 | ## Simple bar chart 397 | ## Try not to set types at an angle to avoid clashing as this decreases legibility. 398 | ## First think of alternative solutions such as transposing the graph, abbreviations, 399 | ## or reducing the number of labels to what is essential. 400 | ######################################################################################## 401 | 402 | # make data 403 | df <- data.frame(trt=c("Treatment 1", "Treatment 2", "Treatment 3"), 404 | cause=c(4,6,10), 405 | highlight = c(2,1,2)) 406 | 407 | g1 <- ggplot(df, aes(x=trt, y=cause)) + 408 | geom_bar(width=0.5, stat = "identity") + 409 | theme(axis.title=element_blank()) + 410 | scale_y_continuous(breaks=c(0, 5, 10)) 411 | 412 | ggsave(g1, file="GWLeg_g1.png", width = 80, height = 80, units = "mm", dpi = 300) 413 | 414 | 415 | g2 <- ggplot(df, aes(x=trt, y=cause)) + 416 | geom_bar(width=0.5, stat = "identity") + 417 | theme(axis.title=element_blank()) + 418 | scale_y_continuous(breaks=c(0, 5, 10)) + 419 | theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12), 420 | axis.text.y=element_text(size=12)) 421 | 422 | ggsave(g2, file="GWLeg_g2.png", width = 80, height = 80, units = "mm", dpi = 300) 423 | 424 | 425 | g3 <- ggplot(df, aes(x=trt, y=cause)) + 426 | geom_bar(width=0.5, stat = "identity") + 427 | theme(axis.title=element_blank(), 428 | axis.text.x=element_text(size=12), 429 | axis.text.y=element_text(size=12)) + 430 | scale_y_continuous(breaks=c(0, 5, 10)) + 431 | coord_flip() 432 | 433 | ggsave(g3, file="GWLeg_g3.png", width = 80, height = 80, units = "mm", dpi = 300) 434 | 435 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 GraphicsPrinciples 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NVSCheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GraphicsPrinciples/CheatSheet/7e23d05c6624cecac37a31606c0290a29ad18ca1/NVSCheatSheet.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CheatSheet 2 | Repository for the cheatsheet and R code for generating the examples displayed. 3 | --------------------------------------------------------------------------------