Skip to main content

Data Visualization in R

R programming was developed in 1993 for making graphs and producing statistical results. There are many libraries in R language that can be used for making graphs and producing statistical data.

There are many steps that have to be taken into consideration for doing data analysis through this language. These steps are:

  • Programming
  • Transforming
  • Discovering
  • Modeling
  • Communicating

R programming is being used in many industries like academics, healthcare, government, insurance, retail, media, manufacturing, etc.

This data analysis can be done through programming in R language which comes with a number of packages having many inbuilt functions and this is the reason that developers do not have to program much. They just need to use those functions and carry out the analysis.

Why R Programming?

R Programming can be used to create statistics and graphs. The language has become very popular and people who want to make their career in this language can undergo R certification online through various institute who provide in-depth knowledge of R.

R certification when seen today can prove to be very useful for students and they can plan a good career after the certification which certainly gives an individual an upper edge over others.

Data Visualization in R

Many types of data visualizations can be created through the language and these are:

  • Histogram
  • Bar / Line Chart
  • Box plot
  • Scatter plot
  • Heat Map
  • Mosaic Map
  • Map Visualization
  • 3D Graphs
  • Correlogram

These will be discussed one by one.

Histogram

A histogram can be created by using histdata package that has many small data sets to create the histogram. A histogram can be used to break data into bins and show their frequency. Here is the code in which histogram is created. Here is the code of creating a simple histogram.

hist(Temperature,
main="Maximum daily temperature ",
xlab="Temperature in degrees Fahrenheit",
xlim=c(50,100),
col="darkmagenta",
freq=FALSE
)

Histogram with R

Image Source

While creating a histogram a developer can see that number of colors specified is more than the number of breaks. The colors are repeated if the number of colors is more.

Line Chart

A line chart shows an increase in the data for a given time period. Line charts are created to compare the changes between two organizations or between weather of two places or other comparisons. Line charts are also created to analyze the trends over a particular period.

x <- c(1:5); y <- x
par(pch=22, col="red")
par(mfrow=c(2,4))
opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(opts)){
  heading = paste("type=",opts[i])
  plot(x, y, type="n", main=heading)
  lines(x, y, type=opts[i])
}

Line Chart in R

Image Source

Bar Chart

Bar chart is also created for comparing profits or weather report or other comparisons. The chart is displayed in the form of bars. Here is the code of creating a vertical bar chart.

counts <- table(mtbikes$gear)
barplot(counts, main="Bike Distribution",
   xlab="Number of Gears")

Bar Chart in R

Image Source

Here is an example of creating a horizontal bar chart.

counts <- table(mtbikes$gear)
barplot(counts, main="Bike Distribution", horiz=TRUE,
  names.arg=c("3 Gears", "4 Gears", "5 Gears"))

Box Plot

It is created either for a single variable or a group of variables. The syntax of creating a box plot is as follows

boxplot(x,data=)

Here x is the formula and data= is the frame which provides the data. Here is an example of creating a box plot.

boxplot(mpg~cyl,data=mtbikes, Bike Milage Data",
   xlab="Number of Cylinders", ylab="Miles Per Gallon")

Box Plot in R

Image Source

Scatter Plot

Scatter plot can be created in many ways. The basic formula for creating the scatter plot is

plot(x, y)

Here is an example of creating a scatter plot.

attach(mtbikes)
plot(wt, mpg, main="Scatterplot Example",
   xlab="Bike Weight ", ylab="Miles Per Gallon ", pch=19)

Heat Map

A heat map is displayed in the form of a table in which colors are displayed in place of numbers. All the columns can have either same or different colors. The dark color denotes highs while the light colors denote lows. Here is an example of creating a heat map.

> heatmap(as.matrix(mtcars))

Mosaic Map

Mosaic Map can be created by using VCD library which has an ample amount of functions to create the map. The syntax for creating a mosaic map is as follows:

mosaic(x, condvar=, data=)

Here x is the formula and codevar is an optional variable in which conditions can be defined. The example below shows the making of a mosaic map

library(vcd)
mosaic(HairEyeColor, shade=TRUE, legend=TRUE)

3D Graphs

R programming can be used to create 3D graphs which are very impressive. The R commander package is used to create these graphs. In order to create the 3D graph R commander package has to be installed and then 3D plot option should be used in the graph.

Here is the code for creating the graph:

>data(iris, package="datasets")
>scatter3d(Petal.Width~Petal.Length+Sepal.Length|Species, data=iris, fit="linear"
>residuals=TRUE, parallel=FALSE, bg="black", axis.scales=TRUE, grid=TRUE, ellipsoid=FALSE)

Lattice package can also be used to create 3D graphs.

Here is an example

>attach(iris)
>cloud(Sepal.Length~Sepal.Width*Petal.Length|Species, main="3D Scatterplot by Species")
>xyplot(Sepal.Width ~ Sepal.Length, iris, groups = iris$Species, pch= 20)

Correlogram

Correlogram helps the users to view the data in the form of matrices. The syntax for creating a correlogram is given below.

corrgram(x, order = , panel=, lower.panel=, upper.panel=, text.panel=, diag.panel=)

Here

Order=TRUE will set the variables in proper order in relation to the correlation matrix.

Panel= refers to diagonal panels in which developers can use lower= and upper=. These options can be chosen below and above the diagonal. Text.panel and diag.panel are the references to main diagonal.

Here is an example of creating a correlogram.

library(corrgram)
corrgram(mtbikes, order=TRUE, lower.panel=panel.shade,
  upper.panel=panel.pie, text.panel=panel.txt,
  main="Bike Milage Data in PC2/PC1 Order")

Map Visualization

This is the latest thing that has been put into the R programming. R can provide the map visualization through JavaScript libraries. The leaflet is open source through which JavaScript libraries can be used for creating interactive maps. In order to use the library, it should be installed.

library(magrittr)
library(leaflet)
m <- leaflet() %>%
addTiles() %>%
addMarkers(lng=77.2310, lat=28.6560, popup="The delicious food of India")
m

Hexbin

Hexbin is a package, which helps to create multiple points This package can be used to create a bivariate histogram. Here is the code for the same.

>library(hexbin)
>a=hexbin(diamonds$price,diamonds$carat,xbins=40)
>library(RColorBrewer)
>plot(a)

Final Verdict

It can be said that various kinds of graphs can be made from R language by writing few lines of code and embedding packages and functions, which have already been coded.

These charts can be used on the web as well as desktop applications and can help you for data visualization in R in a specific way.

The post Data Visualization in R appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/12/data-visualization-in-r.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do