ggtern不工作,新ggvis ggplot22破碎问题,怎么解决

ggplot2入门与进阶(上) | 塞迩斯可视化系列(14)
转载自:http://blog.csdn.net/u/article/details/
作图前的数据准备工作不仅仅指原始数据的收集,还包括数据外观的整理,这些工作对后续的作图无疑十分重要。和其他作图方法相比,ggplot2的优点之一就是把数据整理融合到了作图过程中,替用户分担了数据整型的部分工作。ggplot2数据层面的操作包括映射和分面。先说映射。
1&映射的类型
前面我们已经了解到ggplot对象的data项存储了整个数据框的内容,而“映射”则确定如何使用这些数据。
ggplot2按照图形属性提供了以下可用映射类型:
颜色类型映射:包括 color(颜色或边框颜色)、fill(填充颜色)和 alpha(透明度)形状类型映射:包括 linetype(线型)、size(点的大小或线的宽度)和 shape(形状)位置类型映射:包括 x, y, xmin, xmax, ymin, ymax, xend, yend特殊类型:包括两类,一类是指定数据分组和顺序的映射group和order,另一类是字符串映射。
前两种类型是经典的美学属性映射,第三类的x和y映射也常规,第四类很特别,尤其是字符串映射,很另类,其他类型的映射都可以用aes函数指定,但H.W.为字符串映射专门造了个函数:aes_string。不知道他对字符串特别关照还是实在想不出其他解决方案。
2&颜色和形状类型映射
规律性的东西简单轻松,大家都喜欢;而到处暗藏潜规则、充斥着特例的东西(比如plotrix、perl和中国社会)很杂碎很累人,当然也很让人烦躁和讨厌。ggplot2中颜色和形状这两类映射最符合台面规则的,即:把数据框的变量和图形的美学属性对应起来。
2.1&映射的过程
先看下面ggplot2的数据diamonds:
&span style=&color: rgb(0, 42, 84);&&library&/span&(ggplot2)
&span style=&color: rgb(0, 42, 84);&&set.seed&/span&(&span style=&color: rgb(184, 93, 0);&&100&/span&)
d.sub &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& diamonds[&span style=&color: rgb(0, 42, 84);&&sample&/span&(&span style=&color: rgb(0, 42, 84);&&nrow&/span&(diamonds), &span style=&color: rgb(184, 93, 0);&&500&/span&), ]
&span style=&color: rgb(0, 42, 84);&&head&/span&(d.sub, &span style=&color: rgb(184, 93, 0);&&4&/span&)
cut color clarity depth table price
## 1 Very Good
709 4.34 4.30 2.66
0.30 Very Good
565 4.27 4.31 2.66
作图首先要指定x和y数据,即建立数据框变量和x/y之间的映射:
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&ggplot&/span&(&span style=&color: rgb(0, 84, 0);&&data&/span&=d.sub, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=carat, &span style=&color: rgb(0, 84, 0);&&y&/span&=price))
作出散点图:
&span style=&color: rgb(0, 42, 84);&&theme_set&/span&(&span style=&color: rgb(0, 42, 84);&&theme_bw&/span&())
+ &span style=&color: rgb(0, 42, 84);&&geom_point&/span&()
如果还要建立其他映射,比如用钻石颜色(color)分类数据确定点的颜色,图形外观就会发生变化(为方便说明,先去掉图例):
+ &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&color&/span&=color) + &span style=&color: rgb(0, 42, 84);&&theme&/span&(&span style=&color: rgb(0, 84, 0);&&legend.position&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&2&/span&,&span style=&color: rgb(184, 93, 0);&&2&/span&))
ggplot2映射的过程可以用plot函数作图步骤进行分解,它包含三方面的操作(不包括图形页面设置):
数据分组设定颜色标尺按颜色标尺指定每组数据的颜色
&span style=&color: rgb(0, 0, 195);&&# 设定颜色标尺&/span&
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&levels&/span&(d.sub$color)
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span&
&span style=&color: rgb(0, 42, 84);&&rainbow&/span&(&span style=&color: rgb(0, 42, 84);&&length&/span&(levs))
&span style=&color: rgb(0, 0, 195);&&# 页面设置&/span&
&span style=&color: rgb(0, 42, 84);&&par&/span&(&span style=&color: rgb(0, 84, 0);&&mar&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&3&/span&,&span style=&color: rgb(184, 93, 0);&&3&/span&,&span style=&color: rgb(184, 93, 0);&&0.5&/span&,&span style=&color: rgb(184, 93, 0);&&0.5&/span&), &span style=&color: rgb(0, 84, 0);&&mgp&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&1.5&/span&, &span style=&color: rgb(184, 93, 0);&&0.5&/span&, &span style=&color: rgb(184, 93, 0);&&0&/span&), &span style=&color: rgb(0, 84, 0);&&bg&/span&=&span style=&color: rgb(209, 0, 0);&&&white&&/span&)
&span style=&color: rgb(0, 42, 84);&&plot&/span&(d.sub$carat, d.sub$price, &span style=&color: rgb(0, 84, 0);&&type&/span&=&span style=&color: rgb(209, 0, 0);&&'n'&/span&, &span style=&color: rgb(0, 84, 0);&&xlab&/span&=&span style=&color: rgb(209, 0, 0);&&&carat&&/span&, &span style=&color: rgb(0, 84, 0);&&ylab&/span&=&span style=&color: rgb(209, 0, 0);&&&price&&/span&)
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(184, 93, 0);&&1&/span&
&span style=&color: rgb(0, 0, 127);&&for&/span&(lev &span style=&color: rgb(0, 0, 127);&&in&/span& levs){
&span style=&color: rgb(0, 0, 195);&&# 数据分组&/span&
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& d.sub[d.sub$color==lev, ]
&span style=&color: rgb(0, 0, 195);&&# 作图并指定数据点颜色&/span&
&span style=&color: rgb(0, 42, 84);&&points&/span&(datax$carat, datax$price, &span style=&color: rgb(0, 84, 0);&&pch&/span&=&span style=&color: rgb(184, 93, 0);&&20&/span&, &span style=&color: rgb(0, 84, 0);&&col&/span&=cl[i])
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& i + &span style=&color: rgb(184, 93, 0);&&1&/span&
上面对数据的分组只设定了一个变量,如果增加数据分组的变量,ggplot2中只需要增加映射的类型就可以了,比如在颜色分类的基础上加钻石切工(cut)进行分类:
+ &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&color&/span&=color, &span style=&color: rgb(0, 84, 0);&&shape&/span&=cut) + &span style=&color: rgb(0, 42, 84);&&theme&/span&(&span style=&color: rgb(0, 84, 0);&&legend.position&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&2&/span&,&span style=&color: rgb(184, 93, 0);&&2&/span&))
用plot函数处理起来要考虑的问题就多一些,可以自己试试。
2.2&映射的标尺
用plot函数作图我们得自己考虑使用什么颜色表示不同组的数据,也就是使用什么标尺(或比例尺)。ggplot2则自动应用标尺,这是一个隐含过程。标尺大体可以分为两类:
离散型(或枚举型)标尺。罗列出所有数据分类并将其与美学属性一一对应,处理过程包含数据分类,如上例。连续型(或区间型)标尺。看下面例子:
+ &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&size&/span&= x*y*z ) + &span style=&color: rgb(0, 42, 84);&&theme&/span&(&span style=&color: rgb(0, 84, 0);&&legend.position&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&2&/span&,&span style=&color: rgb(184, 93, 0);&&2&/span&))
上图中点的大小反映钻石的x*y*z值,相当于钻石的大小。用plot函数也可以实现:
cex &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& d.sub[,&span style=&color: rgb(209, 0, 0);&&&x&&/span&]*d.sub[,&span style=&color: rgb(209, 0, 0);&&&y&&/span&]*d.sub[,&span style=&color: rgb(209, 0, 0);&&&z&&/span&]
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& cex/&span style=&color: rgb(0, 42, 84);&&max&/span&(cex)*&span style=&color: rgb(184, 93, 0);&&4&/span&
&span style=&color: rgb(0, 42, 84);&&par&/span&(&span style=&color: rgb(0, 84, 0);&&mar&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&3&/span&,&span style=&color: rgb(184, 93, 0);&&3&/span&,&span style=&color: rgb(184, 93, 0);&&0.5&/span&,&span style=&color: rgb(184, 93, 0);&&0.5&/span&), &span style=&color: rgb(0, 84, 0);&&mgp&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&1.5&/span&, &span style=&color: rgb(184, 93, 0);&&0.5&/span&, &span style=&color: rgb(184, 93, 0);&&0&/span&), &span style=&color: rgb(0, 84, 0);&&bg&/span&=&span style=&color: rgb(209, 0, 0);&&&white&&/span&)
&span style=&color: rgb(0, 42, 84);&&plot&/span&(d.sub$carat, d.sub$price, &span style=&color: rgb(0, 84, 0);&&type&/span&=&span style=&color: rgb(209, 0, 0);&&'n'&/span&, &span style=&color: rgb(0, 84, 0);&&xlab&/span&=&span style=&color: rgb(209, 0, 0);&&&carat&&/span&, &span style=&color: rgb(0, 84, 0);&&ylab&/span&=&span style=&color: rgb(209, 0, 0);&&&price&&/span&)
&span style=&color: rgb(0, 42, 84);&&points&/span&(d.sub[, &span style=&color: rgb(209, 0, 0);&&&carat&&/span&], d.sub[, &span style=&color: rgb(209, 0, 0);&&&price&&/span&], &span style=&color: rgb(0, 84, 0);&&pch&/span&=&span style=&color: rgb(184, 93, 0);&&20&/span&, &span style=&color: rgb(0, 84, 0);&&cex&/span&=cex)
可以看到作图过程也需要手动建立数据和图形属性间的对应关系,但没有对数据分类。
ggplot2对映射应用的标尺可以修改,ggplot提供了一大批 scale_xxxxxxxx 类型的函数,比如 scale_color_xxxx 类型函数用户修改颜色标尺,scale_shape_xxxx 修改形状,scale_linetype_xxxx 修改线型等。按照数据的类型,这些函数还有4种基本类型:
continuous:连续型discrete:离散型identity:和数据取值相同manual:手工指定
有关颜色和坐标轴标尺设定的函数较多,适应不同需要。
&span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&terrain.colors&/span&(&span style=&color: rgb(0, 42, 84);&&length&/span&(&span style=&color: rgb(0, 42, 84);&&levels&/span&(d.sub$color)))
+ &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&color&/span&=color) + &span style=&color: rgb(0, 42, 84);&&scale_color_manual&/span&(&span style=&color: rgb(0, 84, 0);&&values&/span&=cls)
2.3&映射与图例
映射还有一个作用:产生图例。这在ggplot2也是自动的隐含过程,但在plot函数作图中是一个体力活,制作过程就不举例说明了,可以参考legend函数。
2.4&图形颜色和形状的非映射设置
除了通过映射设置几何图形的图形颜色和形状属性外,ggplot2还提供了直接设定方式。和映射方式设置不一样的是:直接设定方式不会在图例上有反映。比较下面两图:
+ &span style=&color: rgb(0, 42, 84);&&geom_boxplot&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=cut, &span style=&color: rgb(0, 84, 0);&&fill&/span&=cut)) +
&span style=&color: rgb(0, 42, 84);&&scale_fill_manual&/span&(&span style=&color: rgb(0, 84, 0);&&values&/span&=&span style=&color: rgb(0, 42, 84);&&rep&/span&(&span style=&color: rgb(209, 0, 0);&&&cyan&&/span&, &span style=&color: rgb(0, 42, 84);&&length&/span&(&span style=&color: rgb(0, 42, 84);&&levels&/span&(d.sub$cut))))
+ &span style=&color: rgb(0, 42, 84);&&geom_boxplot&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=cut), &span style=&color: rgb(0, 84, 0);&&fill&/span&=&span style=&color: rgb(209, 0, 0);&&&cyan&&/span&)
虽然填充色都是青色,但前者用的映射对数据进行了分组,所以会出现分组图例。注意:直接设定方法的参数名称和映射设定是一样的,但是不放在aes函数内部。
在qplot函数中如果要进行美学属性的非映射设定得用 “I” 函数,否则将被当长映射设置。
&span style=&color: rgb(0, 42, 84);&&qplot&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=cut, &span style=&color: rgb(0, 84, 0);&&y&/span&=price, &span style=&color: rgb(0, 84, 0);&&data&/span&=d.sub, &span style=&color: rgb(0, 84, 0);&&geom&/span&=&span style=&color: rgb(209, 0, 0);&&&boxplot&&/span&, &span style=&color: rgb(0, 84, 0);&&fill&/span&=&span style=&color: rgb(209, 0, 0);&&&cyan&&/span&)
&span style=&color: rgb(0, 42, 84);&&qplot&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=cut, &span style=&color: rgb(0, 84, 0);&&y&/span&=price, &span style=&color: rgb(0, 84, 0);&&data&/span&=d.sub, &span style=&color: rgb(0, 84, 0);&&geom&/span&=&span style=&color: rgb(209, 0, 0);&&&boxplot&&/span&, &span style=&color: rgb(0, 84, 0);&&fill&/span&=&span style=&color: rgb(0, 42, 84);&&I&/span&(&span style=&color: rgb(209, 0, 0);&&&cyan&&/span&))
“I”函数表示设为固定值,如果不是在qplot函数中可以不用它。
透明度属性虽然包含在映射类型中,但一般情况下都是直接设定而非映射设定:
p + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&(&span style=&color: rgb(0, 84, 0);&&data&/span&=diamonds, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&alpha&/span&=carat/&span style=&color: rgb(184, 93, 0);&&100&/span&))
p + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&(&span style=&color: rgb(0, 84, 0);&&data&/span&=diamonds, &span style=&color: rgb(0, 84, 0);&&alpha&/span&=&span style=&color: rgb(184, 93, 0);&&0.05&/span&)
第一张图就达不到设置透明度显示点密集度的效果
3&位置类型映射
x和y映射的用法很明确,就不再罗嗦了。xmin, xmax, ymin, ymax, xend, yend这几种映射属于特殊类型,H.W.在ggplot2的说明档里面写得很清楚(赞,H.W.不仅是写R软件的高手,还是普及教育的牛人),下面就照搬他的例子简单说明一下用法。
3.1&ymin/ymax映射的用法
下面代码通过直线拟合产生了钻石切工和价格的关系数据(预测的价格和标准差)
dmod &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&lm&/span&(price ~ cut, &span style=&color: rgb(0, 84, 0);&&data&/span& = diamonds)
cuts &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&data.frame&/span&(&span style=&color: rgb(0, 84, 0);&&cut&/span& = &span style=&color: rgb(0, 42, 84);&&unique&/span&(diamonds$cut), &span style=&color: rgb(0, 42, 84);&&predict&/span&(dmod, &span style=&color: rgb(0, 42, 84);&&data.frame&/span&(&span style=&color: rgb(0, 84, 0);&&cut&/span& =
&span style=&color: rgb(0, 42, 84);&&unique&/span&(diamonds$cut)), &span style=&color: rgb(0, 84, 0);&&se&/span& = &span style=&color: rgb(184, 93, 0);&&TRUE&/span&)[&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(209, 0, 0);&&&fit&&/span&, &span style=&color: rgb(209, 0, 0);&&&se.fit&&/span&)])
fit se.fit
## 4 Very Good
通过设定ymin/ymax映射,用pointrange几何类型可以直接做出带误差线的散点图,无需使用errorbar设置:
se &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&ggplot&/span&(cuts, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span& = cut, &span style=&color: rgb(0, 84, 0);&&y&/span& = fit, &span style=&color: rgb(0, 84, 0);&&ymin&/span& = fit - se.fit, &span style=&color: rgb(0, 84, 0);&&ymax&/span& = fit + se.fit, &span style=&color: rgb(0, 84, 0);&&colour&/span& = cut))
se + &span style=&color: rgb(0, 42, 84);&&geom_pointrange&/span&()
当然也可以先画点再做误差线,这样思路明确些。或者做其他类型的图如柱形图:
se + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&)
se + &span style=&color: rgb(0, 42, 84);&&geom_bar&/span&(&span style=&color: rgb(0, 84, 0);&&stat&/span&=&span style=&color: rgb(209, 0, 0);&&&identity&&/span&, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&fill&/span&=cut)) + &span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&)
ymin/ymax可以用来改变几何类型的坐标轴范围,但&这不是标准用法&,最简单的是用ylim函数:
se + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&ymin&/span& = &span style=&color: rgb(184, 93, 0);&&3000&/span&, &span style=&color: rgb(0, 84, 0);&&ymax&/span& = &span style=&color: rgb(184, 93, 0);&&4800&/span&)) + &span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&)
se + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&) + &span style=&color: rgb(0, 42, 84);&&ylim&/span&(&span style=&color: rgb(184, 93, 0);&&3000&/span&,&span style=&color: rgb(184, 93, 0);&&4800&/span&)
&span style=&color: rgb(0, 0, 195);&&# 或者用: scale_y_continuous(limits=c())&/span&
但以上方法都不适用于柱形图,得用下面的方法,具体原因以后章节再说:
se + &span style=&color: rgb(0, 42, 84);&&geom_bar&/span&(&span style=&color: rgb(0, 84, 0);&&stat&/span&=&span style=&color: rgb(209, 0, 0);&&&identity&&/span&, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&fill&/span&=cut)) +
&span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&) + &span style=&color: rgb(0, 42, 84);&&coord_cartesian&/span&(&span style=&color: rgb(0, 84, 0);&&ylim&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&3000&/span&,&span style=&color: rgb(184, 93, 0);&&4800&/span&))
如果几何类型的设置参数中就包含位置类型映射,情况是很不一样的。所谓“太极”,有点道理:
px &span style=&color: rgb(84, 0, 84); &&&strong&&-&/strong&&/span& &span style=&color: rgb(0, 42, 84);&&ggplot&/span&(mtcars, &span style=&color: rgb(0, 42, 84);&&aes&/span&(wt, mpg)) + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&()
px + &span style=&color: rgb(0, 42, 84);&&annotate&/span&(&span style=&color: rgb(209, 0, 0);&&&rect&&/span&, &span style=&color: rgb(0, 84, 0);&&xmin&/span& = &span style=&color: rgb(184, 93, 0);&&2&/span&, &span style=&color: rgb(0, 84, 0);&&xmax&/span& = &span style=&color: rgb(184, 93, 0);&&3.5&/span&, &span style=&color: rgb(0, 84, 0);&&ymin&/span& = &span style=&color: rgb(184, 93, 0);&&2&/span&, &span style=&color: rgb(0, 84, 0);&&ymax&/span& = &span style=&color: rgb(184, 93, 0);&&25&/span&, &span style=&color: rgb(0, 84, 0);&&fill&/span& = &span style=&color: rgb(209, 0, 0);&&&dark grey&&/span&, &span style=&color: rgb(0, 84, 0);&&alpha&/span& = &span style=&color: rgb(184, 93, 0);&&.5&/span&)
&span style=&color: rgb(0, 42, 84);&&library&/span&(grid)
px + &span style=&color: rgb(0, 42, 84);&&geom_segment&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span& = &span style=&color: rgb(184, 93, 0);&&5&/span&, &span style=&color: rgb(0, 84, 0);&&y&/span& = &span style=&color: rgb(184, 93, 0);&&30&/span&, &span style=&color: rgb(0, 84, 0);&&xend&/span& = &span style=&color: rgb(184, 93, 0);&&3.5&/span&, &span style=&color: rgb(0, 84, 0);&&yend&/span& = &span style=&color: rgb(184, 93, 0);&&25&/span&), &span style=&color: rgb(0, 84, 0);&&arrow&/span& = &span style=&color: rgb(0, 42, 84);&&arrow&/span&(&span style=&color: rgb(0, 84, 0);&&length&/span& = &span style=&color: rgb(0, 42, 84);&&unit&/span&(&span style=&color: rgb(184, 93, 0);&&0.5&/span&, &span style=&color: rgb(209, 0, 0);&&&cm&&/span&)))
4&特殊类型映射
在ggplot2作图过程中,映射的处理在先于其他绘图步骤,也先于统计运算。颜色和形状类型的映射会对数据进行分组,统计运算使用的也是分组后的数据:
&span style=&color: rgb(0, 42, 84);&&ggplot&/span&(d.sub, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=carat, &span style=&color: rgb(0, 84, 0);&&y&/span&=price, &span style=&color: rgb(0, 84, 0);&&color&/span&=cut)) +
&span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&geom_smooth&/span&(&span style=&color: rgb(0, 84, 0);&&method&/span&=&span style=&color: rgb(209, 0, 0);&&&lm&&/span&, &span style=&color: rgb(0, 84, 0);&&se&/span&=&span style=&color: rgb(184, 93, 0);&&FALSE&/span&)
上面图中平滑曲线的统计处理是按cut进行分类后的数据进行的,分别作出了每一类数据的曲线。但如果要按分组前的数据做统计,就得把数据设为一个整体组;如果要按其他分组方式进行统计,也可以灵活设置:
&span style=&color: rgb(0, 42, 84);&&ggplot&/span&(d.sub, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=carat, &span style=&color: rgb(0, 84, 0);&&y&/span&=price, &span style=&color: rgb(0, 84, 0);&&color&/span&=cut)) +
&span style=&color: rgb(0, 42, 84);&&geom_point&/span&() + &span style=&color: rgb(0, 42, 84);&&geom_smooth&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&group&/span&=&span style=&color: rgb(184, 93, 0);&&1&/span&), &span style=&color: rgb(0, 84, 0);&&method&/span&=&span style=&color: rgb(209, 0, 0);&&&lm&&/span&, &span style=&color: rgb(0, 84, 0);&&se&/span&=&span style=&color: rgb(184, 93, 0);&&FALSE&/span&)
&span style=&color: rgb(0, 42, 84);&&ggplot&/span&(d.sub, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&x&/span&=carat, &span style=&color: rgb(0, 84, 0);&&y&/span&=price, &span style=&color: rgb(0, 84, 0);&&color&/span&=cut)) + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&() +
&span style=&color: rgb(0, 42, 84);&&geom_smooth&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&group&/span&=color, &span style=&color: rgb(0, 84, 0);&&linetype&/span&=color), &span style=&color: rgb(0, 84, 0);&&method&/span&=&span style=&color: rgb(209, 0, 0);&&&lm&&/span&, &span style=&color: rgb(0, 84, 0);&&se&/span&=&span style=&color: rgb(184, 93, 0);&&FALSE&/span&, &span style=&color: rgb(0, 84, 0);&&color&/span&=&span style=&color: rgb(209, 0, 0);&&&black&&/span&)
特殊类型映射有特殊应用。曲线图和柱形图对于x轴数据类型的要求是不一样的,曲线图要用连续型数据,而柱形图要用因子型(或离散型)数据,这两类图形如果不经特殊处理就不能放在一起。group映射可以轻松搞定它:
se + &span style=&color: rgb(0, 42, 84);&&geom_bar&/span&(&span style=&color: rgb(0, 84, 0);&&stat&/span&=&span style=&color: rgb(209, 0, 0);&&&identity&&/span&, &span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&fill&/span&=cut)) + &span style=&color: rgb(0, 42, 84);&&geom_errorbar&/span&(&span style=&color: rgb(0, 84, 0);&&width&/span&=&span style=&color: rgb(184, 93, 0);&&0.2&/span&) +
&span style=&color: rgb(0, 42, 84);&&coord_cartesian&/span&(&span style=&color: rgb(0, 84, 0);&&ylim&/span&=&span style=&color: rgb(0, 42, 84);&&c&/span&(&span style=&color: rgb(184, 93, 0);&&3000&/span&,&span style=&color: rgb(184, 93, 0);&&4800&/span&)) + &span style=&color: rgb(0, 42, 84);&&geom_line&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&group&/span&=&span style=&color: rgb(184, 93, 0);&&1&/span&), &span style=&color: rgb(0, 84, 0);&&color&/span&=&span style=&color: rgb(209, 0, 0);&&&black&&/span&)
order映射用于改变数据类型的排序。注意是在图层中的排列顺序而不是图例的先后顺序。看下面图形颜色的差别:
p + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&color&/span&=cut, &span style=&color: rgb(0, 84, 0);&&order&/span&=cut))
&span style=&color: rgb(0, 42, 84);&&library&/span&(plyr) &span style=&color: rgb(0, 0, 195);&&# 使用desc函数&/span&
p + &span style=&color: rgb(0, 42, 84);&&geom_point&/span&(&span style=&color: rgb(0, 42, 84);&&aes&/span&(&span style=&color: rgb(0, 84, 0);&&color&/span&=cut, &span style=&color: rgb(0, 84, 0);&&order&/span&=&span style=&color: rgb(0, 42, 84);&&desc&/span&(cut)))
aes_string太过另类,不说了。
5&SessionInfo
&span style=&color: rgb(0, 42, 84);&&sessionInfo&/span&()
## R version 3.1.0 ()
## Platform: x86_64-pc-linux-gnu (64-bit)
## locale:
[1] LC_CTYPE=zh_CN.UTF-8
LC_NUMERIC=C
[3] LC_TIME=zh_CN.UTF-8
LC_COLLATE=zh_CN.UTF-8
[5] LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES=zh_CN.UTF-8
[7] LC_PAPER=zh_CN.UTF-8
[9] LC_ADDRESS=C
LC_TELEPHONE=C
## [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
## attached base packages:
## [1] grid
grDevices utils
## [8] methods
## other attached packages:
## [1] plyr_1.8.1
ggplot2_0.9.3.1 zblog_0.1.0
## loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4
evaluate_0.5.3
formatR_0.10
[5] gtable_0.1.2
labeling_0.2
MASS_7.3-31
[9] munsell_0.4.2
proto_0.3-10
Rcpp_0.11.1
reshape2_1.2.2
## [13] scales_0.2.4
stringr_0.6.2
tools_3.1.0
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:320068次
积分:3850
积分:3850
排名:第8896名
原创:15篇
转载:373篇
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'}

我要回帖

更多关于 lan tern 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信