2013-02-21 211 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