Richard Lent, College of the Holy Cross
Some R code chunks that produce graphics:
par(pin=c(3,3))
boxplot(iris$Sepal.Length)
plot(iris$Sepal.Length,iris$Sepal.Width)
A Python code chunk that produces a map of the world and stores it in a graphics file.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# set up orthographic map projection with
# perspective of satellite looking down at 50N, 100W.
# use low resolution coastlines.
map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
# draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='aqua')
# draw the edge of the map projection region (the projection limb)
map.drawmapboundary(fill_color='aqua')
# draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0,360,30))
map.drawparallels(np.arange(-90,90,30))
# make up some data on a regular lat/lon grid.
nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
# compute native map projection coordinates of lat/lon grid.
x, y = map(lons*180./np.pi, lats*180./np.pi)
# contour data over the map.
cs = map.contour(x,y,wave+mean,15,linewidths=1.5)
plt.title('Contour lines over filled continent background\n')
plt.savefig("map.png")
The Python code creates a map using the Python Matplotlib Basemap Toolkit, and saves the map to a local graphics file. We have to do this because currently RStudio does not support display of Python graphics in an R Notebook.
We are using our locally installed Anaconda Python 3.5.2 distribution. This is why the beginning of the Python code chunk looks like this:
{python engine.path='/Users/rlent/anaconda/bin/python'}
The path to the Anaconda Python distribution will vary locally.
This file is part of an RStudio project, so that everything is saved to the project folder, and thus we do not need to specify file paths.
We can then insert the image using a Markdown image tag:
Here’s a citation1 inserted from a BibTeX file using the citr tool.
References
1. Lent, R. A. Variation in Gray Catbird nest morphology. J. Field Ornithol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