Skip to main content

R script editor to create an advanced pie chart in Power BI


Case- I want to create a pie chart using R visual in Power BI while being able to use all available features for a creation of pie chart in the programming language R.

Creating an R visual


In the above image you can notice the symbol R at the bottom of the line, this represents the R visual and I selected it to create one, then I dragged and dropped ProductName and ProductPrice as values for R script. Once you drag and drop a column then the values will be saved as a data frame in the R script of Power BI as shown in the below image.



Define the values as ProductPrice and labels as ProductName

We can define the values for the pie chart as ProductPrice from the dataset, dataset is the data frame that is predefined when we did the drag and drop in R visual fields. In order to call all the values from a specific column we need to use “$” symbol as shown below

“dataset$ProductPrice”

Now we need to assign the column values from the data set to the values and labels for the pie chart. In the language R letter “x” is used to define values. Labels are named as label itself. Check the following image to see how the values and labels are defined in R.


Creating a pie chart

Once you have defined the values and labels then we can create a simple pi chart using the following code.

pie(x, labels)

In this code we are telling to create a pie chart using “x” which represents the values from product price and “labels” which represents the values from the product name, then the resulting pie chart is given below.


Note- Following are going to be some modifications for the pie chart which will affect the visual in various ways and these has to be included within the expression of the pie chart while separated by commas.

Naming the pie chart, “main”

Now it is time for us to give a name to this pie chart, this can be done by providing a string value to the field main like shown below.

main = “Price Comparison”

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison")

Then the result will be as shown below.


Giving custom radius for pie chart, “radius”

Now it is time for us to set radius for this pie chart, this can be done by assigning a decimal value in-between (-1,1) to the attribute radius.

radius = 0.5

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5)

Then the result will be as shown below.



As you can see the radius is decreased when it is set to 0.5 because the default value for radius is 1 and decreasing it to 0.5 has resulted the pie chart to shrink. What will happen if the value is minus? check that for your self.

Setting custom color to the pie chart, “col”

Now it is time for us to set colors to this pie chart, this can be done by assigning a color as String, also you can add colors based on rainbow colors by assigning rainbow(length(x)) to the attribute col.

col = “red”

col = rainbow(length(x))

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)))

Then the result will be as shown below.


Setting the direction for the slices to be drawn, “clockwise”

Now it is time for us to set the direction for the slices, in this pie chart this can be done by assigning Boolean value TRUE or FALSE to the attribute clockwise.

clockwise = TRUE

clockwise = FALSE

Once this is included in the R coding of the pie chart it will be as following. Normally the default option for the slicer direction is anti clock wise, If we want we can change that in to clockwise like I have shown below by setting clockwise "TRUE".

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE)

Then the result will be as shown below.




Setting the maximum number of edges for the pie chart, “edges”

Now it is time for us to set the max allowable edges for the creation of this pie chart, this can be done by assigning a positive integer to the attribute edges.

edges = 20

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 20)

Then the result will be as shown below.


If you can count all the edges the total will be 20 but in order to create a pie chart for a specific requirement you need a minimum amount of edges so giving a value less than that will result in considering the minimum value as default. If you assign a higher value for edges it will result in more steeper and circular pie chart.

Setting the starting point for the first slice in the pie chart, “init.angle”

Now it is time for us to set the starting point for the first slice in the pie chart this can be done by assigning an angle value to the attribute init.angle.

init.angle = 45

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 200, init.angle = 45)

Then the result will be as shown below.

Can you notice that the slicer for the “Book” has moved 45 degrees from its initial point from the previous one?

Setting the density for all the slices in the pie chart, “density”

Now it is time for us to set the density for the pie chart this can be done by assigning an integer value to the attribute density.

density = 20

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 200, init.angle = 45, density = 20)

Then the result will be as shown below.

Density will allow you to change the color intensity applied to the chart. If the values are small (like below 30) then it will end up in lines and if the values are high (like more than 100) then the color saturation will increase along with the value increment.

Setting the angle for the lines in the pie chart when density is small, “angle”

Now it is time for us to change the angle of the lines produced by a low density pie chart like shown above, this can be done by assigning an angle value to the attribute angle.

angle = 90

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 200, init.angle = 45, density = 20, angle = 90)

Then the result will be as shown below.


Can you notice the angle of the lines in the pie chart is changed from the previous one and now it is 90 degrees from x axis?

Setting border to the slicers in the pie chart, “border”

Now it is time for us to set color to the border of the slicers, this can be done by assigning a colour as text to the attribute border.

border = “black”

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 200, init.angle = 45, density = 20, angle = 90, border = “black”)

Then the result will be as shown below.

Changing the edges and lines to be dotted lines in the pie chart, “lty”

Now it is time for us to make the edges and lines of the pie chart as dotted lines, this can be done by assigning an integer to the attribute lty.

lty = 2

Once this is included in the R coding of the pie chart it will be as following.

pie(x, labels, main = "Price Comparison", radius = 0.5, col = rainbow(length(x)), clockwise = TRUE, edges = 200, init.angle = 45, density = 20, angle = 90, border = “black”, lty=2)

Then the result will be as shown below.


Final R script including all attributes for a pie chart in Power BI 




Creation of 3D pie chart in Power BI using R

We can import libraries in R and use them in Power BI in order to create more advanced and customized visuals, There is a library named plotrix and it will allow the creation of 3D pie chart, check the coding given below.




Depending on user requirement it is possible to provide a perfect customized pie chart as R visual. Sometimes the R visuals are low in quality comparing to the default pie chart in Power BI. So simply it is a trade off between quality and customization where R visual provides more customization. 


















Comments

Popular posts from this blog

Choosing color scheme for Power BI reports

Choosing beautiful colors for an attention seeking Power BI report is always challenging. Sometimes we may have an important message but it might go unseen if proper color scheme is not used, so it is important to create reports which are informational and visually appealing. In this blog our main objective is to get an idea about selecting appropriate color scheme and tools that can be beneficial to do this. What is a color scheme? As you all know not all colors look great together and you can tell stories by just having only two colors, have a look at the following image, there in the middle you can see a circle with same color and same dimension, only thing that changes is the back ground, but depending on the back ground the way you see those circles are totally different. Some small circles appear softer, some appear sharper, and some are brighter also you might notice movements or depth effect too. It is important to choose colors wisely to serve the purp

Ragged hierarchy in Power BI

Case- I have a data set which has to be visualized in a ragged hierarchy. What is ragged hierarchy A ragged hierarchy is a user defined hierarchy with an uneven number of levels.   You can see the above table which has 5 levels of hierarchy. In the first level we only have “World” as a category then moving forward to the next level we have two categories  namely “UK” and “USA” likewise the level of hierarchy is drilling down up to five levels with an uneven amount of categories. Creating a hierarchy in Power BI In the above image  you can notice that I have right clicked the “level 1” column which has to be the first level of our hierarchy, Once it is right clicked there you will notice “New hierarchy” as the second option then you will have to click that in order to create a new one. As you can notice in the above image once I have created a new hierarchy then a new column is created named “Level 1 Hierarchy” in that I have dragged and dro

Five tips to enhance a tableau model with usability features.

In the previous blogs I was more focused in Power BI since i work with it day to day, but this time we are going to dig into increasing the usability features of tableau. This blog revolves around the end-user simplicity, if a model is hard to comprehend then it will be a disaster, so we should deploy some means to stay simple. So without further ado lets head on to the first topic of the day. Configuring table and  column  properties. A model can consist of many tables but the important fact is they have to be unique and it is better if we can have a table description to elaborate on whats it about. This can be also considered as an internal documentation, when a project member is changed it is easy to pick up and continue. We use connections to load data for the tables and we have a connection name for it but once if we set the source it cannot be changed in the latter part, so be mindful because once you are dropping a connection you are losing all the tables assoc