13.12 Creating a Vector Field

13.12.1 Problem

You want to make a vector field.

13.12.2 Solution

Use geom_segment(). For this example, we’ll use the isabel data set:

x and y are the longitude and latitude, respectively, and z is the height in kilometers. The vx, vy, and vz values are the wind speed components in each of these directions, in meters per second, and speed is the wind speed.

The height (z) ranges from 0.035 km to 18.035 km. For this example, we’ll just use the lowest slice of data.

To draw the vectors (Figure 13.21), we’ll use geom_segment(). Each segment has a starting point and an ending point. We’ll use the x and y values as the starting points for each segment, then add a fraction of the vx and vy values to get the end points for each segment. If we didn’t scale down these values, the lines would be much too long:

First attempt at a vector field. The resolution of the data is too high, but it does hint at some interesting patterns not visible in graphs with a lower data resolution

Figure 13.21: First attempt at a vector field. The resolution of the data is too high, but it does hint at some interesting patterns not visible in graphs with a lower data resolution

This vector field has two problems: the data is at too high a resolution to read, and the segments do not have arrows indicating the direction of the flow. To reduce the resolution of the data, we’ll define a function every_n() that keeps one out of every n values in the data and drops the rest:

Now that we’ve taken a subset of the data, we can plot it, with arrowheads, as shown in Figure 13.22:

Vector field with arrowheads

Figure 13.22: Vector field with arrowheads

13.12.3 Discussion

One effect of arrowheads is that short vectors appear with more ink than is proportional to their length. This could somewhat distort the interpretation of the data. To mitigate this effect, it may also be useful to map the speed to other properties, like size (line thickness), alpha, or colour. Here, we’ll map speed to alpha (Figure 13.23, left):

Next, we’ll map speed to colour. We’ll also add a map of the United States and zoom in on the area of interest, as shown in the graph on the right in Figure 13.23,using coord_cartesian() (without this, the entire USA will be displayed):

Vector field with speed mapped to alpha (left); With speed mapped to colour (right)Vector field with speed mapped to alpha (left); With speed mapped to colour (right)

Figure 13.23: Vector field with speed mapped to alpha (left); With speed mapped to colour (right)

The isabel data set has three-dimensional data, so we can also make a faceted graph of the data, as shown in Figure 13.24. Because each facet is small, we will use a sparser subset than before:

Vector field of wind speeds, faceted on z

Figure 13.24: Vector field of wind speeds, faceted on z

13.12.4 See Also

If you want to use a different color palette, see Recipe 12.6.

See Recipe 8.2 for more information about zooming in on part of a graph.