A simple ggplot2 scatterplot revisited

Rick Wicklin contacted me with a helpful suggestion for improving the data presentation method outlined in my  previous post on using ggplot2 to visualize some data. In the previous post I had plotted up a highly correlated set of points, showing the correspondence between maximum daily body temperatures of model snails sitting with the foot touching the rock surface, or withdrawn into the shell.

The original figure.

Rick points out that with a bit of data manipulation, you can replot these data to give a better picture of how the body temperatures differ across the range of temperatures encountered in the environment. You can use the average of the two maximum body temperatures on each day (extended + withdrawn) to create a new x-axis variable, and then plot the difference between the two body temperatures on the y-axis to emphasize how they differ across that range of temperatures.

So following Rick’s suggestion, let’s calculate those new values (adding 1 whole line to the R script) and plot the new data using a slightly modified version of the ggplot2 code used previously. For reference, the original data are available here: dailymax_runs139_169.csv. The script below can be downloaded here.

library(ggplot2) #load ggplot2 package
df = read.csv(file.choose()) #Load the data into a data frame
colnames(df) = c("Foot.in", "Foot.out","Matlab.Day") #rename columns
#Make a new data frame with two columns. 1st calculate the average of the two 
#temperatures on each day and call that result 'avg'. 2nd, calculate the 
#the difference between the Foot-out temperature and the Foot-in temperature on 
#each day, and call that result 'differ'. 
df2 = data.frame(avg = (df[,1] + df[,2])/2, differ = (df[,2] - df[,1]))
previous.theme = theme_set(theme_bw()) #set black and white ggplot theme
#Define data to be plotted
dfplot = ggplot(df2, aes(x = avg, y = differ))
#Assemble a x-axis title. The 'atop' function allows you to have two lines of 
#text in a pasted-together expression
my.xlab = expression(atop(paste("Average Maximum Daily Body Temperature, ", degree,"C"),
				" (Extended + Withdrawn)/2"))
#Assemble a y-axis title using the same method. Two spaces are needed after 
#"Temperature" to make proper spacing on the y-axis title for some reason
my.ylab = expression(atop(paste("Difference Between Temperatures, ", degree,"C"),
				" (Extended - Withdrawn)"))
#Open a new png device to print the figure out to (or use tiff, pdf, etc).
png(filename = "figure_x2.png", width = 600, height = 600, units = 'px')
print(dfplot +
				#Define point shape and set alpha transparency
				geom_point(shape = 20, color = alpha("black", 1/5)) +
				xlab(my.xlab) + #insert the x-axis title
				ylab(my.ylab) + #insert the y-axis title
				xlim(5,40) +  #set the x-axis limits explicitly
				#Adjust the plot margins slightly
				opts(plot.margin = unit(c(1,1,2,2), "lines")) +
				#Adjust the font size and margin location of the x-axis title
				opts(axis.title.x = theme_text(size = 16, vjust = -1))+
				#Adjust the font, angle, and margin location of the y-axis title
				opts(axis.title.y = theme_text(size = 16, angle = 90, vjust = 0.25)) +
				#Adjust the font size of the tick labels on the x-axis
				opts(axis.text.x = theme_text(size = 14)) +
				#Adjust the font size of the tick labels on the y-axis
				opts(axis.text.y = theme_text(size = 14)) +
				#Draw only the major grid lines
				opts(panel.grid.minor = theme_blank())
dev.off() #close the png device to save the figure.

Created by Pretty R at inside-R.org


The new figure, produced following Rick Wicklin's suggestion.

The astute viewer will note the handful of negative values in the data set. The goal of the model in the original paper was to explore the effects of different Littorina snail postures and morphological variation on body temperature. In this figure we have shown the difference in maximum body temperature on each day in a 10-year dataset, and three of those days show the snail with its foot withdrawn having a higher body temperature than the same snail with the foot on the rock (producing the negative values on the y-axis). The simplest physical explanation for this sort of occurrence is that these were likely days with minimal sunshine to warm the rock surface, and an air temperature that was slightly warmer than the rock surface temperature. As a result, a snail that pulled its foot into the shell might be warmed primarily by the air, while a snail with the foot on the rock is very much tied to whatever the (cold) rock surface temperature is. Since we are dealing with daily maximum temperatures here, this sort of situation is very rare, since on most days a bit of sun will tend to warm the rock surface enough to make the extended-foot snail warmer than the withdrawn-foot snail. In examining the full time series of temperatures rather than just the daily maxima, you will find more instances where a snail with the foot withdrawn is kept relatively warmer by the air temperature for brief periods on cold, dark days.