BOD,ggplot,

?BOD
BOD
##   Time demand
## 1    1    8.3
## 2    2   10.3
## 3    3   19.0
## 4    4   16.0
## 5    5   15.6
## 6    7   19.8
str(BOD)
## 'data.frame':    6 obs. of  2 variables:
##  $ Time  : num  1 2 3 4 5 7
##  $ demand: num  8.3 10.3 19 16 15.6 19.8
##  - attr(*, "reference")= chr "A1.4, p. 270"

ggplot(dataframe,…) ggplot(BOD,aes(x=Time,y=demand))+ geom_line()

library(ggplot2)
ggplot(BOD,aes(x=Time,y=demand))+
  geom_line()

The line doesn’t start from zero. in order to solve this problem, increase the the range of y

ggplot(BOD,aes(x=Time,y=demand))+
  geom_line()+
  expand_limits(y=0)

OR

ggplot(BOD,aes(x=Time,y=demand))+geom_line()+expand_limits(x=0,Y=0)

When the x variable is a factor, you must also use aes(group=1) to ensure that ggplot() knows that the data points belong together and should be connected with a line.

ggplot(BOD,aes(x=as.factor(Time),y=demand,group=1))+
  geom_line()+
  expand_limits(x=0,y=0)

*Otherwise

ggplot(BOD,aes(x=as.factor(Time),y=demand))+ + geom_line()+ + expand_limits(y=0)

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

dplyr

the data set is not organized in a nice way, the first step is to clean data

mainly we have 2 catagories: suppliment and dose. there are 6(2*3)dataset ###dplyr is more for data session.

library(plyr)
?ToothGrowth

funcion going to use is “ddply”. ddply(dataframe,used catagories, what going to do)

x<-ddply(ToothGrowth,
         c("supp","dose"),
         summarize,
         MeanLength=mean(len))

install.packages(“ggplot2”) library(ggplot2)

ggplot(x,aes(x=dose,y=MeanLength))+
  geom_line()

Group lines by supp. Since str(x) supp is factor, no need to group by supp ggplot(x,aes(x=dose,y=MeanLength,color=supp))+geom_line()

Otherwise:

ggplot(x,aes(x=dose,y=MeanLength,group=supp,color=supp))+
  geom_line()

If black/white printing, change linetype to make it more obvious. add points in the transition even add shape demand in the ggplot(),then add shape in the geon_point which is the upper layor.

ggplot(x,aes(x=dose,y=MeanLength,group=supp,color=supp,linetype=supp,shape=supp))+
  geom_line()+
  geom_point(size=3,shape=3)

(Trick to know the shape of R)

pts<-data.frame(x=1:25,y=1:25)
pts
##     x  y
## 1   1  1
## 2   2  2
## 3   3  3
## 4   4  4
## 5   5  5
## 6   6  6
## 7   7  7
## 8   8  8
## 9   9  9
## 10 10 10
## 11 11 11
## 12 12 12
## 13 13 13
## 14 14 14
## 15 15 15
## 16 16 16
## 17 17 17
## 18 18 18
## 19 19 19
## 20 20 20
## 21 21 21
## 22 22 22
## 23 23 23
## 24 24 24
## 25 25 25
ggplot(pts,aes(x=x,y=y))+geom_point(shape=1:25)