Figure 1 – Syntax

Make sure you downloaded Figure 1 – Dataset
Follow the steps below to create Figure 1.

1. Open RStudio
2. Push Ctrl + Shift + N (creates a new R script)
3. Copy the syntax below in the R script
4. Insert the right directory of your dataset into the 8th line
5. Insert the right directory of “Ghostscript” into the 89th line
5. Select all (Ctrl + A) and push the “Run” button
6. You should find the high quality picture on your Desktop

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

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

#### LOADING AND PREPARATION OF YOUR DATA ####

setwd("H:/desktop")					                          	#Sets directory file (NB: "/" and not"\")
data <- read.table("Figure 1 - 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, aes(x=D, y=mean, group=Patients)) + 					                      #Points x and y, group
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2) +		          	#Errorbars
  geom_line(linetype = 2, size = 1)+						                            #Line between points
  geom_point(aes(shape=factor(Patients)), size=6,fill="white") + 	          #Groups have different shapes
  scale_shape_manual(values=c(16,21), labels=c("Controls", "Patients")) + 	#Shapes manually set and labeled
  
  #### AXIS ####

scale_y_continuous(limits = c(-0.5, 1.6), breaks=seq(-1,2,0.5)) +		        #Y-axis, limits, breaks/labels
  scale_x_log10(   							  						  						  							#X-axis (log)		
    breaks = scales::trans_breaks("log10", function(x) 10^x,n=5),			      #Breaks x-axis
    labels = scales::trans_format("log10", scales::math_format(10^.x))		  #Labels x-axis
  ) +    	
  
#### R TITLE / UNIT and QUANTITY ####

ggtitle("") +									#No title above graph
  xlab(expression("Luminance "(cd/m^2) )) + 					#x-axis label
  ylab("log CS") 									#y-axis label

#### LAY-OUT ####

fig1=
  
  p + 
  annotate("text", x = 0.01, y = 1.5, label = "Figure 1", size = 8, hjust=0, fontface=2) + 			             #Title within graph (bold)
  annotate("text", x = 0.01, y = 1.5, label = "                 Psychophysical laws", size = 8, hjust=0) +  	#Title within graph
  theme_bw() +									
  theme(										
    panel.border = element_rect(colour = "black", size = 1), 		      	#Black border surrounding graph		
    panel.grid.major = element_line(colour = "gray95", size=1),		      #Major grid lines
    panel.grid.minor = element_blank(), 					              	      #Minor grid lines
    axis.title.x=element_text(size=25, colour="black", vjust=-0.5), 		#Lay-out x-axis title
    axis.title.y=element_text(size=27, colour="black"),				          #Lay-out y-axis title
    axis.text.x=element_text(size=23, colour="black"),				          #Lay-out x-axis text
    axis.text.y=element_text(size=23, colour="black"),			          	#Lay-out y-axis text
    axis.ticks.x = element_line(colour="black", size=0.5),			      	#Lay-out x-axis ticks
    axis.ticks.y = element_line(colour="black", size=0.5),			      	#Lay-out y-axis ticks
    legend.position = c(0.8,0.135 ),						                        #Legend position (x,y)
    legend.text = element_text(size = 20),					                  	#Legend background 
    panel.background = element_rect (fill = "white"), 						  		#Background color of graph
    legend.background = element_blank(), 												    		#No background color legend
    legend.key=element_blank(),																					#No background color legend
    legend.key.size = unit(2, 'lines'),																	#Some space between legend keys
    legend.title = element_blank (),																		#No legend title
    plot.margin = unit(c(0,0.2,0.1,0.1), "in")						    				  #Margins (top, right, bottom, left)
  ) + 	
   annotation_logticks(sides = "bt", base=10, size =1)  								#Log ticks on sides

fig1 #show the figure you just created. 

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

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


Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.21/bin/gswin32c.exe")				                    #Install Ghostscript and put the right path here
bitmap("Figure 1.tiff", width = 7.5,  height = 7.5, units = 'in', type="tifflzw", res=600)	#Set width and change height to make graphs squared
grid.arrange(fig1, 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 on your Desktop and find Figure 1.tiff ####

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