新浪博客

R语言中ggplot2中的geom参数的选项以及实例图形

2014-07-03 20:39阅读:
> require(ggplot2)
Loading required package: ggplot2
Attaching package: ‘ggplot2’
The following object is masked _by_ ‘.GlobalEnv’:
diamonds
> attach(diamonds)
The following objects are masked _by_ .GlobalEnv:
x, y
> #geom几种不同参数的不同图形样式效果以及示例图形
> #如果数据是单维的连续性数据,则geom的默认值就是'histogram'
> #如下例:
> qplot(carat,colour='red')
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
>
R语言中ggplot2中的geom参数的选项以及实例图形

> #如果qplot命令中包含x,y数据,且都
是连续型数据,则geom的默认值即使'points',即绘制散点图,如下例:
> qplot(carat,price)
R语言中ggplot2中的geom参数的选项以及实例图形

>
> #geom=“smooth”则会在图形中绘制一条拟合平滑曲线,并将曲线和标准误差表现出来,如下图:
>
> qplot(carat,price,geom='smooth')
geom_smooth: method='auto' and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = 'cs'). Use 'method = x' to change the smoothing method.
R语言中ggplot2中的geom参数的选项以及实例图形
>
> #如果需要同时体现散点图以及拟合曲线,则可以用以下方法:
> qplot(carat,price,geom=c('point','smooth'))
geom_smooth: method='auto' and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = 'cs'). Use 'method = x' to change the smoothing method.
R语言中ggplot2中的geom参数的选项以及实例图形
>
> #以上的警告信息是涉及平滑拟合方法的差异,数据量小于1000的默认方法是loess,即局部回归方法,span参数规定平滑程度,0表示很不平滑,1表示非常平滑
> #对于样本量大于1000的,需要添加以下参数:method='gam',formula=y~s(x,bs='cs')
> #如下例:
> qplot(carat,price,geom=c('point','smooth'),method='gam',formula=y~s(x,bs='cs'))
R语言中ggplot2中的geom参数的选项以及实例图形
> #geom='boxplot'则是用来绘制箱线图,见下例:
> qplot(cut,carat,geom='boxplot')
R语言中ggplot2中的geom参数的选项以及实例图形
>
>
> #geom='density'可以用来绘制概率密度图,见下例:
>
> qplot(carat,geom='density')
R语言中ggplot2中的geom参数的选项以及实例图形
>
>
>
> #geom='jitter'可以用来绘制干扰点图,用于分组区分,见下例
> qplot(cut,carat,geom='jitter',colour=I('red'))
R语言中ggplot2中的geom参数的选项以及实例图形
>
> #geom='bar'用来绘制柱状图,见下例:
> qplot(cut,geom='bar',fill=I('green'))
R语言中ggplot2中的geom参数的选项以及实例图形

> #geom='line'用来绘制折线图,多用于时间序列分析,见下例:
> qplot(date,unemploy/pop,geom='line',data=economics)
R语言中ggplot2中的geom参数的选项以及实例图形

我的更多文章

下载客户端阅读体验更佳

APP专享