lattice 包高级绘图函数

来源:互联网 发布:成都软件专修学院 编辑:程序博客网 时间:2024/05/21 06:22

1.条形图

barchart()

> barchart(Titanic,layout=c(4,1),auto.key=TRUE,scales=list(x="free")#scales=free 表示各组的x轴的刻度存在不同。如果想要y轴不同时,则要设置成y="free"。


还可以写成如下形式:

barchart(Class~Freq|Sex+Age,data=as.data.frame(Titanic),
         groups=Survived,stack=TRUE,layout=c(4,1),
         auto.key=TRUE,scales=list(x="free"))# stack=TRUE 表示叠加显示,stack=FALE 表示对比显示即分成两条。

auto.key 表示显示图例。

也可以命名成为函数,然后调用时更改。

> mygraph<-barchart(Freq~Class|Sex+Age,data=as.data.frame(Titanic),
+          groups=Survived,stack=TRUE,layout=c(4,1),
+          auto.key=TRUE,scales=list(x="free"))#这时画出来的是柱状图。

然后对函数进行修改。

update(mygraph,
       panel=function(...){
  panel.grid(h=0,v=1)
  panel.barchart(...,border="transparent")
})# 此处的“...”表示默认,可以在原来的基础上增加一些线条等操作。


2.点图 dotplot

library("lattice")

> head(VADeaths)
      Rural Male Rural Female Urban Male Urban Female
50-54       11.7          8.7       15.4          8.4
55-59       18.1         11.7       24.3         13.6
60-64       26.9         20.3       37.0         19.3
65-69       41.0         30.9       54.6         35.1
70-74       66.0         54.3       71.1         50.0

dotplot(VADeaths,pch=1:4,col=1:4,
        main=list("Death Rates in Virginia"),
        xlab="Rate(per 1000)",
        auto.key=TRUE)


如图所示:上面的图例中,自动的图例auto.key 是不准确的,都是以圆点的形式表示。图中的三角等符号并未进行注释,因此要选择其他的注释符号。

dotplot(VADeaths,pch=1:4,col=1:4,
        main=list("Death Rates in Virginia"),
        xlab="Rate(per 1000)",
        key=list(colunm=4,text=list(colnames(VADeaths)),points=list(pch=1:4,col=1:4)))

# 用list()来分别定义图例:多少列,表示什么内容,用什么符号。


但是上图仍然不是太清楚,我们能不能分面板进行呢?

dotplot(VADeaths,groups=FALSE,pch=1:4,col=1:4,
        main=list("Death Rates in Virginia"),
        xlab="Rate(per 1000)",
        key=list(colunm=4,text=list(colnames(VADeaths)),points=list(pch=1:4,col=1:4)))

# 添加groups=FALSE 来实现。 


发现图例中的显示并不正确,优化一下。

dotplot(VADeaths,groups=FALSE,layout=c(1,4),
        aspect=0.3,origin=0,type=c("p","h"),
        main=list("Death Rates in Virginia"),
        xlab="Rate(per 1000)")

# aspect 表示图的长宽比(x/y),origin=0 表示下面的坐标轴从0开始,type=("p")只有点,若type=c("p","h")则有点和线。


注意优化时的写法。

0 0
原创粉丝点击