标签:
下面用diamonds的数据为例,由于数据很大,随机选取一个子集进行画图
> library(ggplot2)
> data(diamonds)
> set.seed(42)#设定生成随机数的种子,使结果具有重复性
> small<-diamonds[sample(nrow(diamonds),1000),]#抽样
> head(small)
      carat       cut color clarity depth table price    x    y    z
49345  0.71 Very Good     H     SI1  62.5    60  2096 5.68 5.75 3.57
50545  0.79   Premium     H     SI1  61.8    59  2275 5.97 5.91 3.67
15434  1.03     Ideal     F     SI1  62.4    57  6178 6.48 6.44 4.03
44792  0.50     Ideal     E     VS2  62.2    54  1624 5.08 5.11 3.17
34614  0.27     Ideal     E     VS1  61.6    56   470 4.14 4.17 2.56
27998  0.30   Premium     E     VS2  61.7    58   658 4.32 4.34 2.67
summary一下
> summary(small)
     carat               cut      color      clarity        depth      
 Min.   :0.2200   Fair     : 28   D:121   SI1    :258   Min.   :55.20  
 1st Qu.:0.4000   Good     : 88   E:186   VS2    :231   1st Qu.:61.00  
 Median :0.7100   Very Good:227   F:164   SI2    :175   Median :61.80  
 Mean   :0.8187   Premium  :257   G:216   VS1    :141   Mean   :61.71  
 3rd Qu.:1.0700   Ideal    :400   H:154   VVS2   : 91   3rd Qu.:62.50  
 Max.   :2.6600                   I:106   VVS1   : 67   Max.   :72.20  
                                  J: 53   (Other): 37                  
     table           price               x               y        
 Min.   :50.10   Min.   :  342.0   Min.   :3.850   Min.   :3.840  
 1st Qu.:56.00   1st Qu.:  989.5   1st Qu.:4.740   1st Qu.:4.758  
 Median :57.00   Median : 2595.0   Median :5.750   Median :5.775  
 Mean   :57.43   Mean   : 4110.5   Mean   :5.787   Mean   :5.791  
 3rd Qu.:59.00   3rd Qu.: 5495.2   3rd Qu.:6.600   3rd Qu.:6.610  
 Max.   :65.00   Max.   :18795.0   Max.   :8.830   Max.   :8.870  
       z        
 Min.   :2.330  
 1st Qu.:2.920  
 Median :3.550  
 Mean   :3.572  
 3rd Qu.:4.070  
 Max.   :5.580  
以克拉(carat)数为X轴变量,价格(price)为Y轴变量
> p<-ggplot(data=small,mapping=aes(x=carat,y=price))#将数据映射到XY坐标轴上
下面,画出散点图
> p+geom_point()
 
如果想把切工(cut)映射到形状属性:
> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut))
> p+geom_point()
 
如果想再将颜色(color)映射颜色属性:
> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut,colour=color))
> p+geom_point()
在上面的例子,各种属性映射都由ggplot函数执行,只需要加一个图层,使用geom_point()告诉ggplot要画散点图,于是所有的属性都映射到散点上。
再如geom_histogram用于直方图,geom_bar用于画柱状图,geom_boxplot用于画箱式图等。
上图,也可以用下面代码,来画
> p<-ggplot(small)
> p+geom_point(aes(x=carat,y=price,shape=cut,colour=color))
>ggplot(small)+geom_histogram(aes(x=price))
 
同样可以根据另外的变量给它填充颜色
>ggplot(small)+geom_histogram(aes(x=price,fill=cut))
 
同样,可以将它们分开
>ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="dodge")
 
还可以按照相对比例来画,
>ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="fill")
> ggplot(small)+geom_bar(aes(x=clarity))
 
通过stat参数,可以让geom_bar按指定高度画图,
> ggplot()+geom_bar(aes(x=c(LETTERS[1:3]),y=1:3), stat="identity")
> ggplot(small)+geom_density(aes(x=price, colour=cut))
> ggplot(small)+geom_density(aes(x=price,fill=clarity))
 
colour参数指定的是颜色,fill是往曲线下面填充颜色
> ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))
下面是各种geom_xxx函数
geom_abline    geom_area  
geom_bar       geom_bin2d
geom_blank     geom_boxplot  
geom_contour   geom_crossbar
geom_density   geom_density2d   
geom_dotplot   geom_errorbar
geom_errorbarh    geom_freqpoly 
geom_hex       geom_histogram
geom_hline     geom_jitter   
geom_line      geom_linerange
geom_map       geom_path  
geom_point     geom_pointrange
geom_polygon   geom_quantile 
geom_raster    geom_rect
geom_ribbon    geom_rug   
geom_segment   geom_smooth
geom_step      geom_text  
geom_tile      geom_violin
geom_vline
标签:
原文地址:http://www.cnblogs.com/XBlack/p/4967688.html