8.16 Making a Circular Plot

8.16.1 Problem

You want to make a circular plot.

8.16.2 Solution

Use coord_polar(). For this example we’ll use the wind data set from gcookbook. It contains samples of wind speed and direction for every 5 minutes throughout a day. The direction of the wind is categorized into 15-degree bins, and the speed is categorized into 5 m/s increments:

We’ll plot a count of the number of samples at each SpeedCat and DirCat using geom_histogram() (Figure 8.33). We’ll set binwidth to 15 and make the origin of the histogram start at –7.5, so that each bin is centered around 0, 15, 30, etc.:

Polar plot

Figure 8.33: Polar plot

8.16.3 Discussion

Be cautious when using polar plots, since they can perceptually distort the data. In the example here, at 210 degrees there are 15 observations with a speed of 15–20 and 13 observations with a speed of >20, but a quick glance at the picture makes it appear that there are more observations at >20. There are also three observations with a speed of 10–15, but they’re barely visible.

In this example we can make the plot a little prettier by reversing the legend, using a different palette, adding an outline, and setting the breaks to some more familiar numbers (Figure 8.34):

Polar plot with different colors and breaks

Figure 8.34: Polar plot with different colors and breaks

It may also be useful to set the starting angle with the start argument, especially when using a discrete variable for theta. The starting angle is specified in radians, so if you know the adjustment in degrees, you’ll have to convert it to radians:

Polar coordinates can be used with other geoms, including lines and points. There are a few important things to keep in mind when using these geoms. First, by default, for the variable that is mapped to y (or r), the smallest actual value gets mapped to the center; in other words, the smallest data value gets mapped to a visual radius value of 0. You may be expecting a data value of 0 to be mapped to a radius of 0, but to make sure this happens, you’ll need to set the limits.

Next, when using a continuous x (or theta), the smallest and largest data values are merged. Sometimes this is desirable, sometimes not. To change this behavior, you’ll need to set the limits.

Finally, the theta values of the polar coordinates do not wrap around-it is presently not possible to have a geom that crosses over the starting angle (usually vertical).

I’ll illustrate these issues with an example. The following code creates a data frame from the mdeaths time series data set and produces the graph shown on the left in Figure 8.35:

The first problem is that the data values (ranging from about 1000 to 2100) are mapped to the radius such that the smallest data value is at radius 0. We’ll fix this by setting the y (or r) limits from 0 to the maximum data value, as shown in the graph on the right in Figure 8.35:

Polar plot with line (notice the data range of the radius) (left); With the radius representing a data range starting from zero (right)Polar plot with line (notice the data range of the radius) (left); With the radius representing a data range starting from zero (right)

Figure 8.35: Polar plot with line (notice the data range of the radius) (left); With the radius representing a data range starting from zero (right)

The next problem is that the lowest and highest month values, 1 and 12, are shown at the same angle. We’ll fix this by setting the x limits from 0 to 12, creating the graph on the left in Figure 8.36 (notice that using xlim() overrides the scale_x_continuous() in p, so it no longer displays breaks for each month; see Recipe 8.2 for more information):

There’s one last issue, which is that the beginning and end aren’t connected. To fix that, we need to modify our data frame by adding one row with a month of 0 that has the same value as the row with month 12. This will make the starting and ending points the same, as in the graph on the right in Figure 8.36 (alternatively, we could add a row with month 13, instead of month 0):

Polar plot with theta representing x values from 0 to 12 (left); The gap is filled in by adding a dummy data point for month 0 (right)Polar plot with theta representing x values from 0 to 12 (left); The gap is filled in by adding a dummy data point for month 0 (right)

Figure 8.36: Polar plot with theta representing x values from 0 to 12 (left); The gap is filled in by adding a dummy data point for month 0 (right)

Note

Notice the use of the %+% operator. When you add a data frame to a ggplot object with %+%, it replaces the default data frame in the ggplot object. In this case, it changed the default data frame for p from md to mdnew.

8.16.4 See Also

See Recipe 10.4 for more about reversing the direction of a legend.

See Recipe 8.6 for more about specifying which values will have tick marks (breaks) and labels.