2013-02-21 207 views
4

我想绘制同一个面板中某个数据框中某些变量的多个直方图。下面是一些下面的代码:在同一个面板中绘制多个直方图

library(lattice) 
dd <- data.frame(gp = factor(rep(paste('Group', 1:6, sep = ''), each = 
100)), x = rnorm(600)) 
histogram(~ x | gp, data = dd) 
histogram(~ x | gp, data = dd, as.table = TRUE) 

这是把数据X到组1至6.在一个给定的数据帧,我们已经在特定类别的数字。例如,假设我想在同一个面板上绘制高度,体重和平均血压(日期框架中的变量)直方图。我如何做到这一点,而不必形成一个新的数据集和组1至3?

回答

8

无需在此处重新整理数据。

histogram(~ height +age +weight ,data = dd) 

您可以再与帘布层layout改变面板的显示顺序。例如:

histogram(~ height +age +weight ,layout=c(1,3),data = dd) 

这将在3个面板中生成3个直方图。

编辑

添加标题,您可以使用main

histogram(~ height +age +weight ,layout=c(1,3),data = dd, 
      main='PLEASE READ LATTICE HELP')  

边注:设置参数不同的格子功能共享。例如xlab的输入:See xyplot。当你去xyplot帮助你可以阅读:

main: 
Typically a character string or expression describing the main 
     title to be placed on top of each page. Defaults to NULL 
+0

这是假设列名是身高,年龄和体重? – proton 2013-02-21 15:43:35

+0

是的。 Lattice使用你的data.frame作为一个环境。 – agstudy 2013-02-21 15:44:41

+0

无论如何要为直方图添加标题吗? – proton 2013-02-21 15:50:47