Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple histograms in same panel

Tags:

r

lattice

I am trying to plot multiple histograms of some variables in a data frame in the same panel. Here is some following code:

 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) 

This is putting the data x into the groups 1 to 6. In a given dataframe, we already have the numbers in particular categories. For example, suppose I want to plot a histogram of height, weight and average blood pressure (variables in a date frame) in the same panel. How would I do this without having to form a new data set and groups 1 to 3?

like image 901
proton Avatar asked Feb 18 '23 10:02

proton


1 Answers

No need to reshape data here.

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

You can then ply with layout to change the display order of panels. For example:

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

This will produce 3 histograms in 3 panels.

EDIT

to add a title you can use main

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

Side note: Settings parameters are shared between different lattice functions. For example the entry of xlab: See xyplot. when you go to xyplot help you can read :

main:
Typically a character string or expression describing the main 
       title to be placed on top of each page. Defaults to NULL
like image 96
agstudy Avatar answered Feb 27 '23 16:02

agstudy