knitr Options

knitr::opts_chunk$set(fig.align = "center")
knitr::opts_chunk$set(results = "hold")

R Packages

library(readr, warn.conflicts = FALSE)
library(tidyr, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)

Data Import

table_1 <- read_csv("GoodwinTable1.csv", col_types = "cdddddddddddddd")
table_2 <- read_csv("GoodwinTable2.csv", col_types = "cdddddddddddddd")
table_3 <- read_csv("GoodwinTable3.csv", col_types = "cdddddddddddddd")

Colors / Shapes

colors <- c("#999999", "#56B4E9", "#E69F00", "#0072B2", "#000000")
shapes <- c(21:25)

Figure 1

table_1 %>%
  gather("Year", "Prevalence", 2:15) %>%
  ggplot(aes(Year, Prevalence, color = `Smoking status`)) +
  geom_point(aes(shape = `Smoking status`)) +
  geom_line(aes(group = `Smoking status`, linetype = `Smoking status`)) +
  guides(color = guide_legend(ncol = 1)) +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  theme_minimal() +
  theme(legend.position = "bottom", legend.title = element_blank()) +
  ggtitle("Prevalence of smoking status from 2002 to 2015",
          subtitle = "(The National Survey on Drug Use and Health, aged 12+)")

Figure 2

table_2 %>%
  gather("Year", "Prevalence", 2:15) %>%
  mutate(CPD = factor(CPD, levels = unique(CPD))) %>%
  ggplot(aes(Year, Prevalence, color = CPD)) +
  geom_point(aes(shape = CPD)) +
  geom_line(aes(group = CPD, linetype = CPD)) +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  theme_minimal() +
  theme(legend.position = "bottom", legend.title = element_blank()) +
  ggtitle("Prevalence of CPD among current smokers from 2002 to 2015",
          subtitle = "(The National Survey on Drug Use and Health, aged 12+)")

Figure 3

subtitle <- "(The National Survey on Drug Use and Health, 2002–15, aged 12+)"

table_3 %>%
  gather("Year", "Prevalence", 2:15) %>%
  filter(`Current smokers` == "TTFC (%)" |
           `Current smokers` == "No TTFC (%)") %>%
  mutate(`Current smokers` = factor(`Current smokers`,
                                    levels = unique(`Current smokers`))) %>%
  ggplot(aes(Year, Prevalence, color = `Current smokers`)) +
  geom_point(aes(shape = `Current smokers`)) +
  geom_line(aes(group = `Current smokers`, linetype = `Current smokers`)) +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  theme_minimal() +
  theme(legend.position = "bottom", legend.title = element_blank()) +
  ggtitle("Prevalence of TTFC nicotine dependence among current smokers",
          subtitle = subtitle)

Figure 4

table_3 %>%
  gather("Year", "Prevalence", 2:15) %>%
  filter(`Current smokers` != "TTFC (%)" &
           `Current smokers` != "No TTFC (%)") %>%
  mutate(`Current smokers` = factor(`Current smokers`,
                                    levels = unique(`Current smokers`))) %>%
  ggplot(aes(Year, Prevalence, color = `Current smokers`)) +
  geom_point(aes(shape = `Current smokers`)) +
  geom_line(aes(group = `Current smokers`, linetype = `Current smokers`)) +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  theme_minimal() +
  theme(legend.position = "bottom", legend.title = element_blank()) +
  ggtitle("Prevalence of TTFC nicotine dependence stratified by number of CPD",
          subtitle = subtitle)

Session Info

sessionInfo()
## R version 3.4.2 (2017-09-28)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] bindrcpp_0.2  dplyr_0.7.4   ggplot2_2.2.1 tidyr_0.7.2   readr_1.1.1  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.13       bindr_0.1          knitr_1.17        
##  [4] magrittr_1.5       hms_0.3            tidyselect_0.2.2  
##  [7] munsell_0.4.3      colorspace_1.3-2   R6_2.2.2          
## [10] rlang_0.1.2        plyr_1.8.4         stringr_1.2.0     
## [13] tools_3.4.2        grid_3.4.2         gtable_0.2.0      
## [16] htmltools_0.3.6    assertthat_0.2.0   lazyeval_0.2.1    
## [19] yaml_2.1.14        rprojroot_1.2      digest_0.6.12     
## [22] tibble_1.3.4       purrr_0.2.4        glue_1.2.0        
## [25] evaluate_0.10.1    rmarkdown_1.5.9000 labeling_0.3      
## [28] stringi_1.1.5      compiler_3.4.2     scales_0.5.0      
## [31] backports_1.1.1    pkgconfig_2.0.1