Figure 2

1. Create a new text file and name it “Figure 2 dataset.txt”
2. Copy the data below in the text file; save and close

Patient	Difficulty	Frequency
0	1	85
0	2	10
0	3	5
0	4	0
0	5	0
1	1	45
1	2	30
1	3	15
1	4	5
1	5	0

3. Open RStudio and create a new R script (Ctrl + Shift + N)
4. Copy the syntax below in the R script
5. Insert the right directory of “Ghostscript” into the 7th line (see R Graphs – Preparation) – use “/” instead of “\” to seperate directory levels
6. Insert the right directory of your dataset (see step 1) into the 8th line – use “/” instead of “\” to seperate directory levels
7. Select all (Ctrl + A) and push the “Run” button
8. You should find your high resolution figure in the same directory as your dataset

Now you are ready to play around – use your own dataset or change the syntax and see what happens. 

#### 	This syntax is written by Ronald Bierings - PhD student Ophthalmology UMCG 	####
#### 	Questions and/or remarks are welcome (ronaldbierings@gmail.com)			####

#=======================================================================================================#
#PREPARATION FIGURE 2

Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.21/bin/gswin64c.exe")		 #Install Ghostscript and put the right path here (NB: "/" and not"\")
setwd("D:/Desktop")					                                         #Sets directory file (NB: "/" and not"\")
data <- read.table("Figure 2 dataset.txt",header=T)		               #Loads dataset 
attach(data)							                                           #Makes dataset active

names(data)							#Shows names of variables 
summary(data)						#Gives summary of dataset
data								    #Gives whole dataset

Patients <- factor(Patients)					#Categorical variable (standard: quantitative)

library("ggplot2")                    #Loads package
library("Rmisc")                      #Loads package 
library("plyr")                       #Loads package
library("scales")                     #Loads package
library("gridExtra")                  #Loads package
library("cowplot")                    #Loads package



######################################    SYNTAX TO PLOT GRAPH    #######################################

#### POINTS / ERRORBARS / LINES ####

p=
  ggplot(data=cdata, aes(x=Difficulty, y=Frequency, fill=factor(Patient))) + 				   #Points x and y, group
  geom_bar(colour="black", width=.8, stat="identity",position="dodge") +		           #Bars next to eachother
  scale_fill_manual(values=c("white", "black"), labels=c("Controls", "Patients")) +    #Colors of bars manually set and labeled
  
  #### AXIS ####

scale_y_continuous(limits = c(0, 100), breaks=seq(0,100,10), expand = c(0,0)) +		  #Axis minimum, maximum, breaks
  scale_x_continuous(   																            		            #Log axis minimum, maximum, breaks
    breaks=seq(1,5,1),                                                              #Breaks x-axis (5 groups)
    labels = c("No \n difficulty  \n","A little \n difficulty \n","A lot of \n difficulty \n","Extreme \n difficulty \n", "Stopped \n doing \n") 
  ) +    	
  
  #### TITLE / UNIT and QUANTITY ####

ggtitle("") +									                                  #No title above graph
  xlab(expression("Seeing outside on a cloudy day")) + 		   		#x-axis label
  ylab("Frequency (%)") 							                        	#y-axis label

#### LAY-OUT ####

fig2 = 
  p + 
  annotate("text", x = 0.6, y = 94, label = "Figure 2", size = 8, hjust=0, fontface=2) + 			             #Title within graph (bold)
  theme_bw() + 
  theme(
    axis.ticks.x = element_blank(),                         #No x-axis ticks
    axis.ticks.y = element_line(colour="black"),            #No y-axis ticks
    panel.border = element_rect(colour = "black"), 					#Black border surrounding graph		
    panel.grid.major.x = element_blank() ,						      #No vertical gridlines
    panel.grid.major.y = element_line(colour = "gray95"),		#Major grid lines
    panel.grid.minor = element_blank(), 						        #No minor grid lines
    axis.line = element_line(colour = "black"),					    #Lay-out axis lines
    axis.title.x=element_text(size=25, colour="black"), 		#Lay-out x-axis title
    axis.title.y=element_text(size=25, colour="black"),			#Lay-out y-axis title
    axis.text.x=element_text(size=20, colour="black"),			#Lay-out x-axis text
    axis.text.y=element_text(size=20, colour="black"),			#Lay-out y-axis text
    legend.text = element_text(size = 20),                  #Legend size 
    legend.position = c(0.85,0.907 ),						            #Legend position (x,y)	
    panel.background = element_rect (fill = "white"), 			#Background color
    legend.background = element_blank(), 						        #Legend text background color
    legend.key=element_blank(),				  			              #Legend shapes background color
    legend.key.size = unit(2, 'lines'),	                    #Space between legend categories
    legend.title = element_blank (),						            #Legend title
    plot.margin = unit(c(0,0.2,0.1,0.1), "in")              #Margins
) 						


fig2      #Show the figure you just created                                   

#########################
#=======================================================================================================#

################################    SYNTAX TO SAVE GRAPH AS HQ TIFF    ##################################

bitmap("Figure 2.tiff", width = 7.5,  height = 7.5, units = 'in', type="tifflzw", res=600)
grid.arrange(fig2, nrow =1, ncol=1, widths=c(1))																						#Creats a grid (more applicable if you want more graphs)
dev.off()
par(mfrow = c(1,1))

####Look in the directory of your database and find Figure 2.tiff ####

#=======================================================================================================#