Make sure you have necessary packages in your library. If you have not installed yet, do it first by using the code: install.packages(“ggplot2”)

library(ggplot2)

Bar graph and Pie chart

Load the online resource preference data. *I have changed the names of datasets. You should be sure to load the correct name of your data in your working directory.

load(file="online.RData")

See what your data look like

summary(online)
##        Source      Count       
##  Google   :1   Min.   : 19.00  
##  Library  :1   1st Qu.: 43.75  
##  Other    :1   Median : 63.50  
##  Wikipedia:1   Mean   :138.00  
##                3rd Qu.:157.75  
##                Max.   :406.00
names(online)
## [1] "Source" "Count"

Make a bar graph (Textbook, p10)

ggplot(online, aes(x=Source, y=Count)) +
  geom_bar(stat="identity")

Change the order of the bars in the graph

Source_order <- c("Google","Library","Wikipedia","Other")
ggplot(online, aes(x=Source, y=Count)) +
  scale_x_discrete(limits=Source_order) +
  geom_bar(stat="identity")

You may want to make a pie chart for this data

ggplot(online, aes(x="", y=Count, fill=Source)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) 

Making histogram

Load the data of IQ scores of 60 fith-grade students (tb, p15)

load(file="iq.RData")
attach(iq)

You can simply make a histogram by using the following code

hist(IQ, col="green", breaks=5)

Or you can also use ‘ggplot’ to make a histogram

ggplot(iq) + geom_histogram(mapping = aes(x = IQ), col="white", binwidth=5)

Now make a histogram for the customer service center call data (tb, p17)

load(file="call.RData")
attach(call)
ggplot(call) + geom_histogram(mapping = aes(x = length), col="blue", binwidth=15) +
  coord_cartesian(xlim = c(0, 1200), ylim = c(0, 2500))

Making Stemplots

Make stempots for the college students data (tb, p20)

load(file="clg.RData")
attach(clg)
stem(UGradPerThou, scale=1)
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   3 | 8
##   4 | 1111133
##   4 | 55556667777788889
##   5 | 001112224444
##   5 | 566
##   6 | 00001
##   6 | 79
##   7 | 12
##   7 | 7