Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Plotting With Ggplot: Install - Packages ("Ggplot2") Library (Ggplot2)

The document discusses using the ggplot2 package in R for creating publication-ready graphics. It introduces some basic functions in ggplot2 like qplot() and ggplot() for generating different types of plots like scatter plots, line graphs, density plots, and bubble charts from sample datasets. Specific geoms like geom_point(), geom_line(), and aesthetics like color and size mappings are demonstrated. Producing interactive and informative visualizations with ggplot2's comprehensive theming capabilities is emphasized.

Uploaded by

sunifeb128075
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Plotting With Ggplot: Install - Packages ("Ggplot2") Library (Ggplot2)

The document discusses using the ggplot2 package in R for creating publication-ready graphics. It introduces some basic functions in ggplot2 like qplot() and ggplot() for generating different types of plots like scatter plots, line graphs, density plots, and bubble charts from sample datasets. Specific geoms like geom_point(), geom_line(), and aesthetics like color and size mappings are demonstrated. Producing interactive and informative visualizations with ggplot2's comprehensive theming capabilities is emphasized.

Uploaded by

sunifeb128075
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Plotting with ggplot

Umesh P., SilpaBhaskaran


Dept. of Computational Biology and Bioinformatics, University of Kerala
R package is nowadays used widely for generating dynamic and interactive graphics because of
its capability to produce wide variety of graphs out of the input data. Several packages are
available with R to create such high resolution, publication ready images. ggplot2 is one of the
most popular packages in R that helps to generate beautiful graphics. Apart from the R's plot()
function, ggplot2 allows you to create more complex graphs and plots. ggplot2 allows
composing a set of independent components- such as scales and layers, in different ways to get
variety of graphs. ggplot2 helps us to create hassle-free, quality plots in seconds without
compromising the accuracy. It also takes care of the formatting requirements of the plot and
provides a comprehensive theming system that complements the plots in appearance. In this
issue we are discussing some basic commands used in ggplot2 for producing interactive
graphics.
To make ggplot2 available for our application, first install the package and load it into R
environment:
install.packages("ggplot2")
library(ggplot2)
Now, let us look into qplot( ) which is one of the basic plotting function in the ggplot2 package.
Consider the following data of accident cases occurred in a particular place during five
years(from 2010- 2014):
Now try the given R statement:
>qplot(Accident, data = new, geom="density", fill=YearRange,
xlab="Accidents", ylab="Density", main="Density of Accidents per 5
years", alpha=I(.4))
This statement will plot the density of accidents of each year, from its beginning to end, as each
layer as given in Fig. 1.

Fig. 1: Data and correspoding density plot
The attribute alpha allows transparency of overlapping items, as indicated in the figure, with a
value indicating the transparency strength. geom specifies the geometric objects that define the
graph type. It can be either "point", "smooth", "boxplot", "line", "histogram", "density", "bar", or "
histogram ".
Now let us have a look at the ggplot() function in the ggplot2 library which is more advanced
than qplot(). A typical ggplot() function takes two primary arguments data and aes. data
indicates the data frame contacting the data that is to be plotted and aes itself is like a function
to pass arguments to the plot.
Now let us try ggplot ( ) with an example.
myplot<- ggplot(heightweight, aes(Height, Weight))

This statement will give us an error and no graph is plotted, as we have to specify the
geometrical object we would like to use too. If we wish to get scatter plot, add geom_point( ).
i.e. myplot + geom_point()<- ggplot(heightweight, aes(Height, Weight))

For getting lines that join data points, we can use
myplot + geom_point()+ geom_line()<- ggplot(heightweight,
aes(Height, Weight))
We can colour either the point or the line by adding aes(color = factor( )) into the
argument of geom. That is if we want to add colour to the points use the below statement:
myplot+geom_point(aes(color = factor(Height)))+ geom_line()
See the resultant plot (Fig. 2).

Fig. 2: Sample data and the obtained plot


Now let us see how bubble plot is used to represent the literacy of people in different states of
India. We used the sample data from http://en.wikipedia.org/wiki/Demographics_of_India.
Let us try the following code:
ggplot(data, aes(x = no, y = Population)) +
geom_point(aes(size = Literacy), alpha = 0.5, position = "jitter",
color = "red") +
geom_text(data=data, mapping=aes(x=no, y=Population, label=State),
size=4, vjust=3, hjust=0.5) +
scale_size(range = c(10, 50)) +
theme_classic()


Fig. 3: Bubble plot

In this article, we have indicated some of the usages of ggplot2 package. You can explore a lot
more capabilities of ggplot2 and generate wonderful graphs to represent your data.

You might also like