Hi, I have recently started learning R and I intend to go pro in it. I strongly believe that the best way to learn something is to teach it. So, I am restarting by blog series and this time it will be around R.
Yeah buddy!
I will start with something which I in particular have a strong interest in.
*Drums roll*
Visualization in R
*Drums roll*
Visualization in R
The general approach to creating a good visualization is as follows -
- Decide upon the most suitable visualization for the available data set. Something that provides quickest and maximum insights
- Decide upon the type of output file required (pdf, jpeg,png,bmp.....)
- Create a basic structure
- Keep on adding the details till you get what you want
Lets look at an example. One of the things I really appreciate about R is that it comes with many popular data sets to practice upon.I will take the "mtcars" dataset.
Lets have a look at the first few lines of the dataset by using the head function
This data set provides specs around some of the popular cars of USA. Now, suppose I want to show the relationship between weight of the car (wt) and mileage per gallon (mpg). Lets start by simply plotting the relationship between the two variables by using the plot command.
And yes I want the output in a png file. The name of the output will be "uncleSamCars.png". Here is how the first few lines of our code will look -
png("UncleSamCars.png")
attach(mtcars)
plot(wt,mpg)
The mission is not yet accomplished, but caressing "enter" after writing in the above code will bloom the following on your R GUI -
Hmmm, so ladies and gentlemen we can see over here that bulky cars are having lesser mileage. So, for the sake of my unborn child(ren), start buying lighter cars and reduce the load of your existence on the planet.
Now, suppose I want to add a plot for linear regression between the two variables in this graph. For that I need to add to my code another line
abline(lm(mpg~wt))
And I want to give it a title - "Regression of mpg on wt", yep another line -
title("Regression of mpg on wt")
And here is how our final code will look when it grows up -
png("UncleSamCars.png")
Attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("Regression of mpg on wt")
detach(mtcars)
dev.off()
And here will be the final output -
Of course, we will discuss the code now :)
So the first and the last line - png("name of file. extension of file") and dev.off() are used to save the graphical output in a file. Instead of png, if you want pdf or jpeg or anything else, simply replace the png with the desired file type. The dev.off() will remain same and always needs to be present at the end of your graphics code. Alright bruh?
Next i have used attach(mtcars) and detach(mtcars), this has got nothing to do with the graph, its just a convenient way to access the columns of a dataframe. the attach comes before we start accessing the columns of the data frame and detach comes once our code gets orgasm.
Next, plot function. As the name suggests, it plots! Since it has two axis, we can give only two variables as input. The first argument plots the x axis and the second ? yep, y-axis
Now, the abline. This one is too scared to go out alone. So, if you try to simply plot an abline without using its momma, plot() function. It will start crying! So, this must follow the plot function.
As the name suggests abline is used to plot a line. But, you might have noticed the input of abline is another function - lm
lm() is used to plot linear regression model. In our case we have plotted predictions of mpg based on wt. The line represents the least square of sum of differences between the predicted value and the actual value. To know more search for "linear regression"
So, abline(lm(mpg~wt)) plots a line for linear regression of mpg on wt
In the end we have title(), no extra points for guessing, it puts the title of the graph.
So, here is again the code and the final output -
png("UncleSamCars.png")
Attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("Regression of mpg on wt")
detach(mtcars)
dev.off()
Time to try it yourself. Now, of course there are no free lunches, so share this post. And do leave a comment, it creates a perception that my content was useful :)
On a serious note, will really appreciate a feedback.
Till then,
Happy learning.




Nice one champ! Fun+Learning..cool.
ReplyDeleteThanks Bruh!
ReplyDelete