Using Qplot and R Tutorial 1 Abhik Seal This tutorial will guide you how to use ggplot2 (an R package for visualizing data). This tutorial will not cover every function of ggplot2 but will cover basic and some important functions how you will use the data for visualization. 1. Getting R http://www.r-project.org/ Link for downloading R for windows ,linux and Mac. 2. Install ggplot2 Type install.packages(“ggplot2”) in the R command window and select any of the mirror sites for downloading ggplot2. 3. After downloading the ggplot2 package use the command library(ggplot2) to load the package The Figure 1 gives you the screenshot of the previous steps
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Using Qplot and R Tutorial 1
Abhik Seal
This tutorial will guide you how to use ggplot2 (an R package for visualizing data). This
tutorial will not cover every function of ggplot2 but will cover basic and some important
functions how you will use the data for visualization.
1. Getting R
http://www.r-project.org/ Link for downloading R for windows ,linux and Mac.
2. Install ggplot2
Type install.packages(“ggplot2”) in the R command window and select any
of the mirror sites for downloading ggplot2.
3. After downloading the ggplot2 package use the command
library(ggplot2) to load the package
The Figure 1 gives you the screenshot of the previous steps
>qplot(carat, ..density.., data = diamonds, facets = color ~ .,geom =
"histogram", binwidth = 0.1, xlim = c(0, 3))
#xlim is the limit of x axis and binwidth is the width of the histograms facets which are choosen by the form row variable ~ column variable. Use of more than one variable like 2 and three will make the graph very long time to compute and also making the graph much complex. Color facet used as row variable. > qplot(carat, ..density.., data = diamonds, facets = . ~ color,
geom="histogram", binwidth=0.1,xlim=c(0,3))
# when color facet is used as column variable.
From the two figure 8a and 8b it is observed that 8a is much more informative than 8b because we can see in 8b the bars are much more congested and difficult to interpret than bars in 8a. High-quality diamonds (colour D) are skewed towards small sizes, and as quality declines the distribution becomes more flat.
Fig 8a Fig 8b
Now to use the maps package( the maps package contains maps of USA,World,Italy,New Zealand,France
To install maps
>install.packages(“maps”)
Mapss pacakage has various datasets among them one is us.cities to see the dataset type to use the data
us.cities
> data(us.cities)
Now the cities have populations as a variable. I want to make a sample of data of population >500000