#Scatterplot created by Chad C. Williams from the University of Victoria's Neuroeconomics Laboratory, 2016. ################################################################################################################### ###MODIFY THIS SECTION TO YOUR LIKING### ################################################################################################################### #This section is an easy way to set many of the variables that we will use later when plotting. This is a practice you should #get familiar with. The reason is, if you need to change something quickly (such as regression line colour), you don't have to go #search through your script to find it. You can come right here and change it quickly. It is also helpful if you use the #same variable multiple times. That way, you change it once up here and it all changes below. #First, change your working directory to the folder with your data #Filenames filename = "Scatterplot Data.txt" OutputName = "Scatterplot.tiff" #Condition Names Condition1 = "Height" Condition2 = "Weight" #Axis Value Ranges groupylim = c(110,180) groupxlim = c(140,225) #Axis labels groupylab = "Height" groupxlab = "Weight" #Regression Line Colour regcolour = '#999999' ################################################################################################################### ###DO NOT CHANGE ANYTHING BEYOND THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING### ################################################################################################################### #Load Packages library(ggplot2) #Load Data data = read.table(filename) #Change column names colnames(data) = c("variable","value") ################################################################################################################### #Plot Data Plot = ggplot(data, aes(x=variable, y=value)) #Creates your plot, designating the x and y column from your data frame print( #Because ggplot is meant to be done in the console tab, we must print any ggplot in a script. #Print essentially puts it into the console tab Plot # Here, we recall plot so we can add properties to the variable, anything below with a + in front of it is us adding a property +geom_point(shape = 1) #Determines type of plot, in this case scatterplot. Shape is the shape of each data point +geom_smooth(method=lm, se = FALSE, color = regcolour, size = 0.5) #This is a linear regression line. se shows confidence intervals around the line if it is designated as TRUE. +scale_y_continuous(expand = c(0,0)) #This makes it so that your y axis touches the x axis (otherwise there will be a gap) +scale_x_continuous(expand = c(0,0)) #This makes it so that your x axis touches the y axis (otherwise there will be a gap) +coord_cartesian(ylim = groupylim, xlim = groupxlim) #This determines the axis limits. This was determined at the top of the script +xlab(groupxlab) #Determines the x axis label +ylab(groupylab) #Determines the y axis label #Background and border +theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) #Adds white space around your plot +theme(axis.line.x = element_line(color="black", size = 0.5), #This adds a x axis line axis.line.y = element_line(color="black", size = 0.5), #This adds a y axis line panel.grid.major = element_blank(), #Removes grid panel.grid.minor = element_blank(), #Removes more grid panel.background = element_blank(), #Removes grey background panel.border = element_blank(), #Removes lines around the plot legend.title=element_blank())) #Removes any legend ################################################################################################################### #Save Plot - Here, we can save the plot as an image. This will save the current plot in your R plot tab. #Output name was determined at the top of the script ggsave(filename = OutputName, width = 6.54, height = 4.36, dpi = 600)