Have more Charts by writing R codes inside Power BI: Part 2



In the previous post (Part 1) I have explained how to write a simple scatter chart in the Power BI. Now in this post I am going to show how to present 5 different values in just one chart via writing R scripts.

I will continue the codes that I wrote in the previous post  as below :

In previous post we just shown 3 variables : speed in city, speed in highway, and number of cylinder.

In this post,  I am going to show variable “year” and type of “Drive” of each car plus what we have.

first, I have to change the above code as below:

Before that, I want to do some changes in the chart first. Hence, I changed the “aes” function argument. I replaced the “Size” argument with “Colour”. that means, I want to differentiate car’s cylinder values not just by Cycle size, but I am going to show them by allocating them different  colours. so I changes the “aes” function as above.

so by changing the codes as below

we will have below chart:


now I want to add other layer to this chart. by adding year and car drive option to the chart. To do that first choose year and drv  from data field in power BI. As I have mentioned before, now the dataset variable will  hold data about speed in city, speed in highway, number of cylinder, years of cars and type of drive.

I am going to use another function in the ggplot packages name “facet_grid” that helps me to show the different facet in my scatter chart. in this function, year and drv (driver) will be shown against each other.

To do that, I am going to use above code to add another layer to my previous chart.

so I add another layer to variable “t” as above.

now the visualization will be look like as below: as you can see the car’s speed in the highway and city in y and x axis.  Also, we have cylinder as colour and drive and year as facet.



Now imagine, I am going to add a slicer to filter “car brands”, and also to see these five variables against each other in one chart.

So, we will have below chart:


I am going to have some more fun with chart, I need to show the drive type in all region not just the three above (see below image)



In this case I am able to use the another facet function instead of facet_grid(year ~ drv)  I am going to use other function name facet_wrap(year~ drv) which help me to do that.

Hence, I change the codes as below:

Moreover, I want to show the car’s cylinder type not just by different colour, but also with same colour just different shading. so I will replace the argument inside the aes function as below

instead of  aes(x=cty, y=hwy,colour = factor(cyl))

so finally the code will be look like as below


Finally,I will have below picture, as you can see in the below image, we have title for each group as for the top left 1999 and 4drive, for top and middle we have 1999, and r drive and so forth.


In future posts, I will show some other visuals that we have in ggplot2 package, which help us to have more fun in power BI.

Download Demo Files

Enter Your Email to download the file (required)


Leila Etaati
Dr. Leila Etaati is Principal Data Scientist, BI Consultant, and Speaker. She has over 10 years’ experience working with databases and software systems. She was involved in many large-scale projects for big sized companies. Leila has PhD of Information System department, University of Auckland, MS and BS in computer science. Leila is Microsoft Data Platform MVP.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">