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
Why do you need the line to overlay the bars? Which bars are touched by the line is just a quirk of scaling and could easily change with the scales. All the overlay does is to make it harder to read, why not jut have 2 panels aligned on the x-axis but with the line plot above the bar plot? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Mario Beolco > Sent: Tuesday, January 25, 2011 5:31 PM > To: r-help at r-project.org > Subject: [R] plot with 2 y axes > > 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=non > e,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 > > ______________________________________________ > 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.
I hope this is what you are looking for. you will have to add your own colors and such. year = c(1966:2008) tempur c(2.9,4.5,1.9,1,2.9,4.3,3.9,4.3,4.9,4.4,4.5,2,2.8,-.4,2.3,3,.3,1.7,3.3,.8,-1.1,.8,4.9,5.2,4.9,1.5,3.7,3.6,3.2,4.8,2.3,2.5,5.2,5.3,4.9,3.2,3.6,3.9,4.8,4.3,3.7,5.8,4.9) indx c(1,1.24,1.46,1.37,1.87,2.66,3.07,3.91,4.16,4.32,2.52,2.44,2.18,1.18,1.93,2.13,1.92,2.24,2.01,1.89,.66,1.01,1.5,2.11,2.02,.7,.75,1.28,1.37,2.01,1.54,2,2.07,2.11,2.42,2.29,2.15,2.21,2.14,2.33,1.89,2.03,2.58) data1 = data.frame(year,tempur,indx) # create new graphing window windows() # create margins for variable names par(mar = c(5,5,5,5)) # create bar plot barplot(data1$tempur, names = data1$year, xlab = "year", ylab = "Temp") # Allows for plotting on same charts (kinda like overlay) par(new=T) # plot line points plot(data1$year,data1$indx, xaxt = "n", yaxt = "n", xlab = "", ylab = "") # add lines lines(data1$year,data1$indx) # adds axis for second plot to right hand side axis(side = 4) # adds second y axis variable name to right hand side mtext("Index",side = 4, line = 3) # quits plotting on the current plotting window par(new = F) -- View this message in context: http://r.789695.n4.nabble.com/plot-with-2-y-axes-tp3237418p3238368.html Sent from the R help mailing list archive at Nabble.com.