Steelgray Bar Chart Over Gray Background and White Grid Lines
# ==============================================================================
# Bar Chart with Grid
#
# Here we'll create a chart of my citations from Google Scholar using a gray
# background and white horizontal grid lines.
#
# Kurt Taylor Gaubatz
# January, 2014
#
# ==============================================================================
# The Data
year = c(1993:2013) # Create a vector of years
# The citations data is copied from my Google Scholar page. It looks like it
# is set up to first set the y range for the chart and then the actual
# citations numbers are a percentage of the max y range, which in this
# case is 63.
#
citations1 = c(6.3,11.1,7.9,17.5,38.1,31.7,20.6,34.9,63.5,57.1,61.9,
61.9,54.0,100.0,84.1,95.2,74.6,84.1,68.3,100.0,60.3)
citations2 = citations1*.63 # Change from % to actual counts
#-------------------------------------------------------------------------------
# We'll plot this in four steps.
# 1. First we'll create an empty plot with the appropriate x and y ranges.
# 2. Next we'll use the coordinates of the plot area to create a dark
# gray rectangle the size of the plot area.
# 3. Then we'll create the white grid lines.
# 4. Finally, we'll add the bar chart.
#
# A few idiosyncracies to note:
#
# > The yaxis="i" option is to prevent R from adding its default overage to
# the range of the y axis. You'll note that in most R charts the origin
# starts a little below (0,0). This forces the y axis to start exactly
# at 0.
#
# > When the first plot is set up, the usr parameter identifies the x and y
# coordinates for the central plot area. I then use those in the second
# step to create the background rectangle in dark gray. We have to do it
# this way rather than using the bg="gray" parameter setting, because bg=
# colors in the entire plot window and I just want the internal plot area
# to be gray.
#
# > We use the add=T option in the barplot to tell R to add the barplot to
# an existing plot. We do the barchart last because we want it to be on
# top of the rectangle and the grid lines.
#
#-------------------------------------------------------------------------------
par(mai=c(1,.75,.25,.25)) # Set margins in inches (B,L,T,R)
# Create an empty plot
plot(x=0, y=0, type="n", # Plot nothingness
xlab=NA, ylab=NA, # No axis labels
yaxs="i", # No overage on the Y axis
yaxt="n", xaxt="n", # Turn off x and y axes
xlim=c(1,max(year)-min(year)), # Set range of x axis
ylim=c(0,80)) # Set range of y axis
# Create a medium gray rectange over the plot area
rect( # Use plot area coordinates
xleft = par()$usr[1], # Left side x coordinate
ybottom = par()$usr[3], # Bottom y coordinate
xright = par()$usr[2], # Right side x coordinate
ytop = par()$usr[4], # Top y coordinate
col=gray(.7)) # Set the color to a medium gray
# Overlay grid lines
grid(nx=NA, # No vertical (x) lines
ny=8, # 8 lines for y
lty=1, # Use solid lines
col="white") # Set color to white
# Overlay the barplot
barplot(citations2, # Use citation counts for bar heights
names=year, # Use years as names for bars
width = .65, # Set width of bars
add=T, # Add this as an overlay to open plot
las=2, # Rotate bar labels by 90 degrees
space= .5, # Set space between bars
col="darkslategray") # Set color of bars to darkish blue gray