Figure 1

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

Patients	D	mean	se
0.00	0.013	-0.10	0.15
0.00	0.130	0.40	0.15
0.00	1.300	0.9	0.15
0.00	13.000	1.1	0.15
0.00	130.000	1.1	0.15
1.00	0.130	0.00	0.15
1.00	1.300	0.50	0.15
1.00	13.000	0.70	0.15
1.00	130.000	0.7	0.15

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)			####

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

Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.21/bin/gswin32c.exe") #Install Ghostscript and put the right path here (NB: "/" and not"\")
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    ##################################

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 in the directory of your database and find Figure 1.tiff ####

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