Compositional Geometric Mean Bar Plot

These plots are made for the folllowing manuscript:

Talarico R, Janssen I. Unravelling the Compositional Effects of Time Spent in Sleep, Sedentary Behaviour and Physical Activity on Obesity Measures in Children. (2016-2017).

1. The data

The brunt of this analysis was done in SAS so the summarized data is imported from a SAS library into R.

##load necessary packages
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 3.3.3
## Warning: package 'survival' was built under R version 3.3.3
## Warning: package 'Formula' was built under R version 3.3.2
## Warning: package 'ggplot2' was built under R version 3.3.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.3
library(ggplot2)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.3.3
library(grid)

##Import the summarized data from a exported SAS xpt file
mydata <- sasxport.get("C:/Users/rober/Documents/Rob/mydata.xpt") ## BMI data
## Processing SAS dataset MYDATA     ..
mydata1 <- sasxport.get("C:/Users/rober/Documents/Rob/mydata1.xpt") ##Fat mass index data
## Processing SAS dataset MYDATA1    ..
mydata2 <- sasxport.get("C:/Users/rober/Documents/Rob/mydata2.xpt") ##Waist circumference data
## Processing SAS dataset MYDATA2    ..
mydata
##           bmi        legend       cen.lr
## 1      Normal      Cen_MVPA  0.030662780
## 2      Normal      Cen_LIPA -0.001726911
## 3      Normal        Cen_SB -0.003213556
## 4      Normal Cen_Sleep_Dur  0.001583340
## 5       Obese      Cen_MVPA -0.220917156
## 6       Obese      Cen_LIPA -0.031820187
## 7       Obese        Cen_SB  0.029116920
## 8       Obese Cen_Sleep_Dur -0.004846122
## 9  Overweight      Cen_MVPA -0.008230719
## 10 Overweight      Cen_LIPA  0.024044397
## 11 Overweight        Cen_SB -0.002315495
## 12 Overweight Cen_Sleep_Dur -0.003922564

Just some mild cleaning.

mydata$legend<-factor(mydata$legend, levels=c("Cen_MVPA","Cen_LIPA","Cen_SB","Cen_Sleep_Dur"))


mydata<-
mydata%>%
          mutate(bmi2=ifelse(bmi=="Normal","Non-Overweight",as.character(mydata$bmi))) %>%
          select(-bmi) %>%
          rename(bmi=bmi2)

mydata1$legend<-factor(mydata1$legend, levels=c("Cen_MVPA","Cen_LIPA","Cen_SB","Cen_Sleep_Dur"))

mydata2$legend<-factor(mydata1$legend, levels=c("Cen_MVPA","Cen_LIPA","Cen_SB","Cen_Sleep_Dur"))

2. The plot

Part A:

First I will construct the BMI plot with a legend on the top.

g<-ggplot(data=mydata,aes(x=bmi,y=cen.lr,fill=legend))

dodge <- position_dodge(width = 0.7)

legend_plot<-g+geom_bar(stat="identity",position=dodge, width=0.7)+
  scale_fill_discrete(
    labels=c("MVPA","LIPA","SB","Sleep"))+
  theme(
        legend.title=element_blank(),
        legend.text=element_text(size=18),
        legend.position = "top")

Second I will extract the legend from this plot

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(legend_plot)

grid.draw(mylegend)

dev.copy(png,file="Legend.png")
## png 
##   3
dev.off()
## png 
##   2

Part B:

Now I will create the three plots (with no legends) for body mass index, waist circumference and fat mass index.

  1. BMI plot
plot1<-g+geom_bar(stat="identity",position=dodge, width=0.7)+
  labs(y="Log Ratio",x="BMI Categories")+ 
  scale_fill_discrete(
    labels=c("MVPA","LIPA","SB","Sleep"))+
  scale_x_discrete(
    limits=c("Non-Overweight","Overweight","Obese"),label=c("Non-\nOverweight","Overweight","Obese"))+
  theme_bw()+
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        axis.title= element_text(size=30,face="bold"), 
        axis.text = element_text(size=24),
        legend.title=element_blank(),
        legend.text=element_text(size=18),
        legend.position = "none")+
  geom_hline(yintercept=0.00)+
  coord_cartesian(ylim=c(-0.25,0.15))

plot1

dev.copy(png,file="BMIplot.png")
## png 
##   3
dev.off()
## png 
##   2
  1. Fat Mass Index Plot
g<-ggplot(data=mydata1,aes(x=fmi,y=cen.lr,fill=legend))

dodge <- position_dodge(width = 0.5)

plot2<-g+geom_bar(stat="identity",position=dodge, width=0.5)+
  labs(y="",x="FMI Quartiles")+ 
  scale_fill_discrete(
    name="Movement\nBehaviours",
    labels=c("MVPA","LIPA","SB","Sleep")
  )+
  scale_x_discrete(
    limits=c("Q1","Q2","Q3","Q4"))+
  theme_bw()+
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        axis.title= element_text(size=30,face="bold"), 
        axis.text = element_text(size=24),
        legend.title=element_text(face='bold'),
        legend.text=element_text(size=12),
        legend.position = "none")+
  geom_hline(yintercept=0.00)+
  coord_cartesian(ylim=c(-0.25,0.15))

plot2

dev.copy(png,file="FMIplot.png")
## png 
##   3
dev.off()
## png 
##   2
  1. Waist Circumference Plot
g<-ggplot(data=mydata2,aes(x=wc,y=cen.lr,fill=legend))

dodge <- position_dodge(width = 0.5)

plot3<- 
  g+geom_bar(stat="identity",position=dodge, width=0.5)+
  labs(y="",x="WC Quartiles")+ 
  scale_fill_discrete(
    name="Movement\nBehaviours",
    labels=c("MVPA","LIPA","SB","Sleep")
  )+
  scale_x_discrete(
    limits=c("Q1","Q2","Q3","Q4"))+
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        axis.title= element_text(size=30,face="bold"), 
        axis.text = element_text(size=24),
        legend.title=element_text(face='bold'),
        legend.text=element_text(size=12),
        legend.position = "none")+
  geom_hline(yintercept=0.00)+
  coord_cartesian(ylim=c(-0.25,0.15))

plot3

dev.copy(png,file="WCplot.png")
## png 
##   3
dev.off()
## png 
##   2

Now bring the legend and 3 images into powerpoint to group together.