library(gcookbook)  数据准备

 

绘图函数里的stat参数表示对样本点做统计的方式,默认为identity,表示一个x对应一个y,同时还可以是bin,表示一个x对应落到该x的样本数。”说白了就是,identity提取横坐标x对应的y值,bin提取横坐标x的频数。

 

 

 

 

 簇状图:

> ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")

> ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position="dodge",stat="identity")
>

 

> ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position="dodge",stat="identity",colour="black")

 

> ce <- cabbage_exp[1:5,]

1-5行的数据
>

 

> ?geom_bar
>

geom_bar(mapping = NULL, data = NULL, stat = "count",
position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)

geom_bar understands the following aesthetics (required aesthetics are in bold):

x     X轴

y     Y轴

alpha    透明度

colour    边框颜色

fill         填充

group

linetype  线型

size    大小?

 

频数:(不设置Y)

> ggplot(diamonds,aes(cut))+geom_bar()

 

 

直方图:连续变量geom_histogram()

条形图:离散型变量 geom_bar()

 

3)簇型图 
制图要点是,对数据作图后,添加geom_bar时,position=”dodge”(分开的)如果去掉这部分,默认是生成堆积图.

 

 

> head(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(BOD,aes(x=factor(Time),y=demand))+geom_bar(stat="identity")
> ggplot(pg_mean,aes(group,weight))+geom_bar(stat="identity",fill="lightblue",colour="black")
> library(MASS)#数据
> head(birthwt)
low age lwt race smoke ptl ht ui ftv bwt
85 0 19 182 2 0 0 0 1 0 2523
86 0 33 155 3 0 0 0 0 3 2551
87 0 20 105 1 1 0 0 0 1 2557
88 0 21 108 1 1 0 0 1 2 2594
89 0 18 107 1 1 0 0 1 0 2600
91 0 21 124 3 0 0 0 0 0 2622

> ggplot(mtcars,aes(x=wt,y=mpg))+geom_point(colour="red")
> ggplot(mtcars,aes(x=wt,y=mpg))+geom_point(colour="red",size=4,shape=22)

> ggplot(mtcars,aes(x=wt,y=mpg))+geom_point(colour="darkred",fill="pink",size=4,shape=22)
> #shape=22方形
> ggplot(mtcars,aes(x=wt,y=mpg))+geom_point(colour="darkred",fill="white",size=4,shape=22)
> #空心圆

 

> df <- data.frame(
+ trt = factor(c(1, 1, 2, 2)),
+ resp = c(1, 5, 3, 4),
+ group = factor(c(1, 2, 1, 2)),
+ upper = c(1.1, 5.3, 3.3, 4.2),
+ lower = c(0.8, 4.6, 2.4, 3.6)
+ )
> df
trt resp group upper lower
1 1 1 1 1.1 0.8
2 1 5 2 5.3 4.6
3 2 3 1 3.3 2.4
4 2 4 2 4.2 3.6
> p <- ggplot(df, aes(trt, resp, colour = group))
> p
> p + geom_linerange(aes(ymin = lower, ymax = upper))

 

posted on 2018-10-08 11:11  cathy庆  阅读(117)  评论(0编辑  收藏  举报