Posts tagged with ggplot2

Two interesting ideas here:

• price impact of a trade proportional to `exp( √size )`

Code follows:

(Source: finmath.stanford.edu)

nice ggplot intro tutorial. Just run the commands, about 6 pages = flexible 1-3 hours of learning, depending how much reading you want to pair it with

by Ramon Saccilotto

## Earthquakes

`> data(quakes)``> head(quakes)`

```     lat   long depth mag stations

1 -20.42 181.62   562 4.8       41

2 -20.62 181.03   650 4.2       15

3 -26.00 184.10    42 5.4       43

4 -17.97 181.66   626 4.1       19

5 -20.42 181.96   649 4.0       11

6 -19.68 184.31   195 4.0       12```

`> summary(quakes)`

```      lat              long           depth            mag

Min.   :-38.59   Min.   :165.7   Min.   : 40.0   Min.   :4.00

1st Qu.:-23.47   1st Qu.:179.6   1st Qu.: 99.0   1st Qu.:4.30

Median :-20.30   Median :181.4   Median :247.0   Median :4.60

Mean   :-20.64   Mean   :179.5   Mean   :311.4   Mean   :4.62

3rd Qu.:-17.64   3rd Qu.:183.2   3rd Qu.:543.0   3rd Qu.:4.90

Max.   :-10.72   Max.   :188.1   Max.   :680.0   Max.   :6.40

stations

Min.   : 10.00

1st Qu.: 18.00

Median : 27.00

Mean   : 33.42

3rd Qu.: 42.00

Max.   :132.00  ```

` > plot(quakes,     pch=20, col=rgb(0,0,0,.1) , lwd=.6) `

`> require(ggplot2)> qplot(data = quakes, x = lat, y = long, size = exp(mag), color = mag, alpha = I(.8))`

UPDATE: In the comments, Sean Mulcahy shared his much better post on earthquakes: http://seanmulcahy.blogspot.com/2011/11/global-earthquakes-desktop.html. He shows how to grab up-to-date earthquake data from the U.S. Geological Survey and display it with `R`'s `maps` package. Hooray!

My interpretation of [Leland Wilkinson’s] grammar [of statistical graphics]:

Data is the most important thing, and the thing that you bring to the table.

—Geometric objects … what you actually see on the plot: points, lines, polygons, etc.

Statistics transform the data in many useful ways. For example, binning and counting to create a histogram….

—Scales map values in the data space to values in an aesthetic space, whether it be colour, or size, or shape. Scales also provide an inverse mapping: a legend.

—A coordinate system describes how data coordinates are mapped to the plane of the graphic. It also provides axes and gridlines to make it possible to read the graph.

— A facetting, or conditioning, specification…. to reproduce the same plot for different subsets of the data. The facetting specification describes those subsets and how the facets should be arranged in to a plot.