# Circular Barplots and Customisation in R

Last Updated : 07 Mar, 2022

In this article, we are going to see how to create Circular Barplots and Customisation in R Programming Language.

A circular barplot is similar to a barplot, but instead of cartesian coordinates, it uses polar coordinates. A circular barplot is one in which the bars are presented in a circle rather than a line. This article will show you how to create such graphs using R and ggplot2. It contains reproducible code and explains how to use the coord_polar() method.

### Define the data

To use dataset in barplot we need to create dataset so here we will create it.

## R

 `# Libraries` `library``(tidyverse) ``# help you to prepare the data` `library``(ggplot2) ``# help you to prepare the plots`   `# prepare dataset` `data = ``data.frame``(` `  ``# add a parameter with a range list 1-100` `  ``index = ``seq``(1,100),` `  ``# create labelled parameter` `  ``label = ``paste``( data =``"Data "``, ` `                    ``seq``(1,100), ` `                    ``sep=``"= "``),` `  ``# random values in the range 1 - 100` `  ``values = ``sample``( ``seq``(10,100), 100, replace = T)` `)`   `# top five values of the dataframe` `head``(data)`

Output:

```  index   label  values
1     1 Data -1     28
2     2 Data -2     46
3     3 Data -3     54
4     4 Data -4     25
5     5 Data -5     43
6     6 Data -6     26```

### Example 1: Basic Circular BarPlot

coord_polar() methods used to create plot in specific coordinated.

Syntax:  coord_polar(theta = “x”, start = 0, direction = 1, clip = “on”)

Parameters:

• theta: Variable to map angle to (x or y)
• start : Offset of starting point from 12 o’clock in radians. Offset is applied clockwise or anticlockwise depending on value of direction.
• direction : 1, clockwise; -1, anticlockwise
• clip : Should drawing be clipped to the extent of the plot panel? A setting of “on” (the default) means yes, and a setting of “off” means no. For details, please see coord_cartesian().

## R

 `# Make the plot` `p <- ``ggplot``(data, ``aes``(x = ``as.factor``(index), ``# x-axis factor label ` `                      `  `                      ``# y-axis numerical parameter` `                      ``y = values)) +      ` `  `  `  ``# the bar height will represent` `  ``# the actual value of the data` `  ``geom_bar``(stat = ``"identity"``, ` `           ``fill=``alpha``(``"green"``, 0.5)) + ``# define bar color` `  `  `  ``# define size of inner circle` `  ``# and the size of the bar` `  ``ylim``(-100,120) +` `  `  `  ``# define the polar coordinate` `  ``coord_polar``(start = 0)`   `# plot` `p`

Output:

### Example 2: Adding labels to the data

To add labels and data into it will use geom_text() methods.

## R

 `# Adding labels to the plot`   `data_with_labels = data`   `# number of labels required` `number_of_label <- ``nrow``(data_with_labels)` `# find the angle of rotation of the label` `angle <-  90 - 360 * (data_with_labels\$index - 0.5) /number_of_label     `   `# check the label alignment - right or left` `data_with_labels\$hjust<-``ifelse``( angle < -90, 1, 0)` `# check the label angle ` `data_with_labels\$angle<-``ifelse``(angle < -90,` `                               ``angle + 180, angle)`       `# Make the plot` `# x-axis factor label ` `p <- ``ggplot``(data, ``aes``(x = ``as.factor``(index),` `                      `  `                      ``# y-axis numerical parameter` `                      ``y = values)) +      ` `  `  `  ``# the bar height will represent` `  ``# the actual value of the data` `  ``geom_bar``(stat = ``"identity"``, ` `           `  `           ``# define bar color` `           ``fill=``alpha``(``"green"``, 0.5)) + ` `  `  `  ``# define size of inner circle` `  ``# and the size of the bar` `  ``ylim``(-100,120) +` `  `  `  ``# define the polar coordinate` `  ``coord_polar``(start = 0) +`   `  ``# add labels` `  ``geom_text``(data = data_with_labels,` `            ``aes``(x = index, y = values+10,` `                `  `                ``# label alignment` `                ``label = label, hjust=hjust), ` `            ``color = ``"black"``, fontface=``"bold"``,` `            ``alpha = 0.6, size = 2.5, ` `            ``angle = data_with_labels\$angle,` `            ``inherit.aes = ``FALSE` `)`   `p`

Output:

Previous
Next