[R] plot with 2 y axes

Mario Beolco mario.beolco at gmail.com
Wed Jan 26 01:31:01 CET 2011


Dear R users,

apologies for the total beginner's question. I would like to create a
barchart for some temperature values with the y axis on the right hand
side of the plot. On this plot would like to overlay some time series
data  (in the form of a line) for some other variable called Index.
The y axis for this latter variable should be on the left hand side of
the plot.

An example of what I would like to obtain:

https://sites.google.com/site/graphtests1/

I have tried to do this using ggplot2 and this where I have got (for
data see at the bottom of the e-mail):

none<-theme_blank()
p<-ggplot(tmp3,aes(x=year,y=Temperature))
p1<-p+geom_bar(stat="identity",fill="#9ACD32",colour="#000000")
p1 + geom_line(data=tmp3, aes(x=year, y=Index),
colour="black",size=1)+opts(legend.position="none",panel.grid.major=none,panel.grid.minor=none)+opts(panel.border=none)+theme_bw(base_size=20)

This code does not do what I want because the Temperature y axis
should be on the left hand side and the the y axis for the other
variable called Index is not even there (should in theory be on the
left hand side). I also get the following warning message when I run
that code "I get Warning message:Stacking not well defined when ymin
!= 0". (Should I worry about this?).

I do not know whether ggplot2 can is the best package for creating the
type of plot that I want. I would, however, be very grateful for any
suggestions on to improve the above code or on how I could use other
packages to create the plot I want.

thanks!

Mario



"year","Temperature","Index"
1966,2.9,1
1967,4.5,1.24
1968,1.9,1.46
1969,1,1.37
1970,2.9,1.87
1971,4.3,2.66
1972,3.9,3.07
1973,4.3,3.91
1974,4.9,4.16
1975,4.4,4.32
1976,4.5,2.52
1977,2,2.44
1978,2.8,2.18
1979,-0.4,1.18
1980,2.3,1.93
1981,3,2.13
1982,0.3,1.92
1983,1.7,2.24
1984,3.3,2.01
1985,0.8,1.89
1986,-1.1,0.66
1987,0.8,1.01
1988,4.9,1.5
1989,5.2,2.11
1990,4.9,2.02
1991,1.5,0.7
1992,3.7,0.75
1993,3.6,1.28
1994,3.2,1.37
1995,4.8,2.01
1996,2.3,1.54
1997,2.5,2
1998,5.2,2.07
1999,5.3,2.11
2000,4.9,2.42
2001,3.2,2.29
2002,3.6,2.15
2003,3.9,2.21
2004,4.8,2.14
2005,4.3,2.33
2006,3.7,1.89
2007,5.8,2.03
2008,4.9,2.58



More information about the R-help mailing list