Plots with polar coordinates

In this section, we will have a look at a few plots that can be created using polar coordinates. We have already introduced this coordinate system in the previous chapter. Just remember that you should always use these coordinates with caution since the representation of data in circular form can generate very pronounced perceptual problems concerning the relative areas in the plot.

The most important plots that you can realize with these coordinates are the pie chart, the bullseye chart, and the coxcomb diagram. For our examples, we will use the movie dataset, which we created in previous chapters and the myMovieData dataset, and we will represent in these different plots the proportions of movies within each category.

A pie chart

A pie chart in ggplot2 corresponds to a stacked bar chart in polar coordinates. This means that to produce a pie chart, we will first create a bar chart with bars stacked one on top of the other, and then we will change the coordinate system. The following code shows this:

ggplot(data=myMovieData, 
aes(x=factor(1),fill=factor(Type))) + geom_bar(width = 1) + coord_polar(theta = "y")

This code can turn out to be quite tricky to follow, but with more details, it should become much clearer. The first two lines simply define a bar with the count for each movie type stacked on top of each other along the y axis, with the type variable defining the filling color. This variable is finally the one that we will end up representing in the pie chart. The definition of x=factor(1) doesn't do anything, and it can contain any value; its scope is simply to have just one factor that will be the x axis of our hypothetical bar chart. The width=1 parameter basically defines the radius of our pie chart. Any value greater than 1 will not produce any change since 1 means that the bars of our plot will cover the whole plot area. On the other hand, a value lower than 1 would produce a tighter bar. Finally, with the last line, we just make polar the coordinate y, which is the axis along which we have built our bar chart. In Figure 4.19 are represented the bar plots and the resulting pie charts obtained when changing the coordinates to polar for width=1 and for width=0.5. As you can see, even using a smaller width value can create a nice pie chart effect with an empty area in the middle.

A pie chart

Figure 4.19: This shows bar plots and the resulting pie charts obtained when changing the coordinates to polar for width=1 and for width=0.5

A bullseye chart

A bullseye chart is a chart in which the variables are represented in a circular way with an area proportional to the variable value. The difference compared with the pie chart is that the variables are represented in a concentric way, as in the center of a target ("bull's-eye"). In ggplot2, these plots are realized by producing a bar chart as shown in Figure 4.19, but instead of stretching the y axis along the polar coordinates, in this case, we simply stretch them along the x axis. The following code shows how this is done:

ggplot(data=myMovieData, aes(x=factor(1),fill=factor(Type))) + geom_bar(width = 1) + coord_polar()

The resulting plot is represented in Figure 4.20:

A bullseye chart

Figure 4.20: This is an example of a bullseye chart realized with the myMovieData dataset

A coxcomb diagram

The coxcomb diagram is similar to a pie chart, but the areas representing the data in the pie are not normalized to cover the whole area of the circle. They can be realized in a similar way to the pie chart by producing a normal bar chart and converting its coordinates into polar coordinates. In this case, the bar chart is not realized with the stacked position adjustment, but the data is placed such that one piece of data is next to another along the x axis of our bar chart. The following code shows this:

ggplot(data=myMovieData, 
aes(x=Type,fill=factor(Type))) + geom_bar(width = 1) + coord_polar(theta = "x")

You can then see the resulting chart in Figure 4.21:

A coxcomb diagram

Figure 4.21: Here's an example of a coxcomb diagram realized with the myMovieData dataset

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset