[R] Comination of two barcharts and one xyplot

ravi rv15i at yahoo.se
Wed Aug 13 17:17:04 CEST 2008


Hi Rhelpers,
Thanks a lot, Stephen, for showing me the way to get a data frame into a pasteable format with the dput command. 
My code is given below with the new correction. This should work, as Stephen says, right off the bat :-)
## df1 is the first data frame
df1 <-structure(list(Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 8L, 
7L), .Label = c("2003", "2005", "2007", "2009", "2011", "2013", 
"2015K", "2015M"), class = "factor"), KI = c(15.53, 15.64, 16.18, 
17.09, 22.39, 33.83, 44.91, 52.22), G48 = c(0.3, 0.29, 0.49, 
0.67, 0.93, 1.29, 1.83, 2.14), AvCell = c(0.24, 0.33, 0.59, 0.91, 
1.24, 1.87, 2.71, 3.15), HB = c(37.45, 34.64, 30.32, 29.47, 38.03, 
58.37, 75.54, 87.71), Htens = c(0.76, 1.12, 1.63, 2.27, 3.11, 
4.43, 6.28, 7.34), Impact = c(1.16, 1.78, 4.23, 6.76, 9.17, 14.06, 
20.57, 23.88), Struct = c(3.02, 4.2, 6.67, 9.68, 13.18, 19.41, 
27.51, 31.98), Tens = c(34.05, 32.88, 30.06, 29.25, 37.84, 57.6, 
74.5, 86.57), Year.ord = structure(1:8, .Label = c("2003", "2005", 
"2007", "2009", "2011", "2013", "2015M", "2015K"), class = c("ordered", 
"factor"))), .Names = c("Year", "KI", "G48", "AvCell", "HB", 
"Htens", "Impact", "Struct", "Tens", "Year.ord"), row.names = c(NA, 
-8L), class = "data.frame")
## L1 is the second data frame
L1<-structure(list(Year = c(2009L, 2011L, 2013L), KIL = c(20, 24, 
30), G48L = c(1, 1, 1), AvCellL = c(1, 1.5, 2), HBL = c(30, 35, 
40), HtensL = c(2, 3, 4), ImpactL = c(10, 12, 14), StructL = c(10, 
13, 16), TensL = c(35, 38, 45)), .Names = c("Year", "KIL", "G48L", 
"AvCellL", "HBL", "HtensL", "ImpactL", "StructL", "TensL"), class = "data.frame", row.names = c(NA, 
-3L))
# # Use the reshape package to melt the data frame
library(reshape)
df1m<-melt(df1,id=c("Year","Year.ord"))
## Use the lattice package to plot the barchart
library(lattice)
attach(df1m)
barchart(value~Year.ord|variable,scales=list(y="free",x=list(rot=90)),xlab="Year",ylab="No. of Tests *1000",col="blue")
This plot works just fine. But I want to go beyond this.What I want, in each panel of the lattice barchart, is to plot histograms of the relevant variable (KI, G48 etc) in one colour for the years 2003 to 2007, and in another colour for the other years. On top of this, I want to have a line plot in each panel with the limits for different years given in the second data frame L1 (as bold lines). 
I would like to have information on the following points :
1. How can I get a combination of these plots in every panel (two histograms and one line plot)? Is it possible?
2. Is it easier to do this with ggplot?
3. I would like to know how I can present the legend also.
Will appreciate any help that I can get.
Thanking You,
Ravi


----- Original Message ----
From: stephen sefick <ssefick at gmail.com>
To: ravi <rv15i at yahoo.se>
Cc: r-help at r-project.org
Sent: Wednesday, 13 August, 2008 3:14:54 PM
Subject: Re: [R] Comination of two barcharts and one xyplot

not reproducible

On Wed, Aug 13, 2008 at 9:07 AM, ravi <rv15i at yahoo.se> wrote:
> Hi Rhelpers,
> I would like to have some help with a plot which is beyond my capabilities. This plot that I am seeking involves an overlay of two different barcharts and one xyplot.
> The code that I have used is the following :
> #save(df1,file="M:\\KBR\\df1.RData")
> load(file="M:\\KBR\\df1.RData")
> # df1$Year.ord created to obtain the right order i.e. 2015M < 2015K
> Year.ord<-ordered(Year,levels=c('2003','2005','2007','2009','20011','2013','2015M','2015K'))
> # Use reshape package to melt the data frame
> library(reshape)
> df1m<-melt(df1,id=c("Year","Year.ord"))
> library(lattice)
> attach(df1m)
> barchart(value~Year.ord|variable,scales=list(y="free",x=list(rot=90)),xlab="Year",ylab="No. of Tests *1000",col="blue")
> This plot works just fine. But I want to go beyond this. My first data frame (df1) is :
> "Year","KI","G48","AvCell","HB","Htens","Impact","Struct","Tens","Year.ord"
> "1","2003",15.53,0.3,0.24,37.45,0.76,1.16,3.02,34.05,"2003"
> "2","2005",15.64,0.29,0.33,34.64,1.12,1.78,4.2,32.88,"2005"
> "3","2007",16.18,0.49,0.59,30.32,1.63,4.23,6.67,30.06,"2007"
> "4","2009",17.09,0.67,0.91,29.47,2.27,6.76,9.68,29.25,"2009"
> "5","2011",22.39,0.93,1.24,38.03,3.11,9.17,13.18,37.84,"2011"
> "6","2013",33.83,1.29,1.87,58.37,4.43,14.06,19.41,57.6,"2013"
> "7","2015M",44.91,1.83,2.71,75.54,6.28,20.57,27.51,74.5,"2015M"
> "8","2015K",52.22,2.14,3.15,87.71,7.34,23.88,31.98,86.57,"2015K"
> My second data frame is (L1) is :
> "Year","KIL","G48L","AvCellL","HBL","HtensL","ImpactL","StructL","TensL"
> "1",2009,20,1,1,30,2,10,10,35
> "2",2011,24,1,1.5,35,3,12,13,38
> "3",2013,30,1,2,40,4,14,16,45
> What I want, in each panel of the lattice barchart, is to plot histograms of the relevant variable (KI, G48 etc) in one colour for the years 2003 to 2007, and in another colour for the other years. On top of this, I want to have a line plot in each panel with the limits for different years given in the second data frame L1 (as bold lines).
> I would like to have information on the following points :
> 1. How can I get a combination of these plots in every panel (two histograms and one line plot)? Is it possible?
> 2. Is it easier to do this with ggplot?
> 3. I would like to know how I can present the legend also.
> Will appreciate any help that I can get.
>
> Thanking You,
> Ravi
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods. We are mammals, and have not exhausted the
annoying little problems of being mammals.

    -K. Mullis




More information about the R-help mailing list