R programming contains a multitude of library that can be used to display a visual representation of our computed data.
ggplot2 is a very famous graphs package and is viewed as the most powerful graphics device R has to offer.
Initial Environment Setup
But before we get into creating a program let’s just first set up a proper environment for R programming.
To install R programming tool go to the following link and download the software and install it.
For Windows Users:
And Type and Run the following code RGui
install.packages(“ggplot2”)
For Linux Users:
If you are on Linux platform then you can use this fast and easy command used in Linux which can be used to install R. The yum command is used for installing like this:
$ yum install R
For Ubuntu Linux or other Debian-related OSs, a more direct method is:
$ apt-get install r-base
Now Let’s Start using the RGui (64 -bit) which will be installed in the Installation folder which we had selected while installing the software.
Let us now take the following example and execute in the RGui
df <- data.frame (
group = c(“Male”, “Female”, “Child”),
value = c(25, 25, 50)
)
head(df)
Output:
group value
1 Male 25
2 Female 25
3 Child 50
A DataFrame is a collection of multiple Series where Series is an immutable single dimensional array.
group and value are two series inside the df dataframe.
The above code declares this dataframe in the memory.
Now to create a Bar plot we need to import ggplot2 library and write the following code
library(ggplot2)
# Barplot
bp<- ggplot(df, aes(x="", y=value, fill=group))+
geom_bar(width = 1, stat = “identity”)
bp
X co-ordinate in the above code will remain empty as this a flat graph with only Y co-ordinate receiving the input value
Output:
Next let’s look at a simple Pie chart
pie <- bp + coord_polar("y", start=0)
pie
Output:
Syllabus of Data Science training in Mumbai