Dear all, how can I plot a line graph and a bar graph in one single figure? I tried to combine "barplot" and "plot". Even though they both have the same x-values (1 to 55), it just doesnt look as if they match in their scale (the barplot is much wider than the "plot"....even though I tried to put limits on the x-axis). Here is an example of what I did: barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) ... par(new=TRUE) plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) Another question: how can I make sure that the "0"-values from the barchart are displayed as well? Thank you so much! Anne-Katrin -- Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games! http://games.entertainment.gmx.net/de/entertainment/games/free [[alternative HTML version deleted]]
Dear Anne-Katrin, You could use ggplot to do this. The example below works, although it generates some warnings. library(ggplot2) dataset <- data.frame(x = 0:55, y = rnorm(56, 10), z = runif(56, 9, 11)) ggplot(data = dataset) + geom_bar(aes(x = factor(x), y = y), position = "dodge") + geom_line(aes(x = x, y = z)) HTH, Thierry ---------------------------------------------------------------------------- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Anne-Katrin Link Verzonden: donderdag 10 april 2008 15:18 Aan: R Help Onderwerp: [R] two graphs in one figure? Dear all, how can I plot a line graph and a bar graph in one single figure? I tried to combine "barplot" and "plot". Even though they both have the same x-values (1 to 55),? it just doesnt look as if they match in their scale (the barplot is much wider than the "plot"....even though I tried to put limits on the x-axis). Here is an example of what I did: barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) ... par(new=TRUE) plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) Another question: how can I make sure that the "0"-values from the barchart are displayed as well? Thank you so much! Anne-Katrin -- Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games! http://games.entertainment.gmx.net/de/entertainment/games/free [[alternative HTML version deleted]]
On Thu, Apr 10, 2008 at 8:36 AM, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:> Dear Anne-Katrin, > > You could use ggplot to do this. The example below works, although it generates some warnings. > > library(ggplot2) > dataset <- data.frame(x = 0:55, y = rnorm(56, 10), z = runif(56, 9, 11)) > ggplot(data = dataset) + geom_bar(aes(x = factor(x), y = y), position = "dodge") + geom_line(aes(x = x, y = z))ggplot(data = dataset, aes(factor(x), y)) + geom_bar() + geom_line(aes(y=z, group=1)) does basically the same thing, but without warnings. Hadley -- http://had.co.nz/
Using par(new=TRUE) can be tricky. A better approach is to create one plot, then add the other information to it. You can add bars to an existing graph using barplot with add=TRUE, you can add lines to an existing plot using the lines function. If you give more detail of what you want (examples of x, y, and ynew) then we may be able to give more help. ________________________________ From: r-help-bounces@r-project.org on behalf of Anne-Katrin Link Sent: Thu 4/10/2008 7:18 AM To: R Help Subject: [R] two graphs in one figure? Dear all, how can I plot a line graph and a bar graph in one single figure? I tried to combine "barplot" and "plot". Even though they both have the same x-values (1 to 55), it just doesnt look as if they match in their scale (the barplot is much wider than the "plot"....even though I tried to put limits on the x-axis). Here is an example of what I did: barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) ... par(new=TRUE) plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) Another question: how can I make sure that the "0"-values from the barchart are displayed as well? Thank you so much! Anne-Katrin -- Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games! http://games.entertainment.gmx.net/de/entertainment/games/free [[alternative HTML version deleted]] [[alternative HTML version deleted]]
Anne-Katrin Link wrote:> Dear all, > > how can I plot a line graph and a bar graph in one single figure? I tried > to combine "barplot" and "plot". Even though they both have the same > x-values (1 to 55), it just doesnt look as if they match in their scale > (the barplot is much wider than the "plot"....even though I tried to put > limits on the x-axis). > Here is an example of what I did: > > barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) > ... > par(new=TRUE) > plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) > > Another question: how can I make sure that the "0"-values from the barchart > are displayed as well? >Hi Anne-Katrin, This can be tricky, particularly getting the line plot positioned nicely relative to the bars. The barp function in plotrix may be useful, as it centers the bars on integer values, thereby lining up with the default integer x values when none are specified. Try: library(plotrix) par(mar=c(5,4,4,4)) barp(y,width=0.5,ylim=c(-1,65),...) lines(ynew+45,col="red") axis(4,at=c(40,50,60),labels=c(0,10,20),col="red") This puts your line plot over the bars with a separate axis. I'm not sure exactly what you mean by the "0" values (x or y axis?), but it can be done. Jim
Dear Greg, dear all, thank you for your reply! To clarify, here is an example of what I want to do (but better, of course!): x<- c(1,2,3,4,5) y<- 0:4 y2<- c(0,0,7,8,9) barplot(y ,ylim=c(-1,10), ylab="", xaxt="n",yaxt="n", main="") axis(4,at=c(1,2,3,4,5,6)) text(6,2.9, "2nd y-axis", srt = 270, xpd = TRUE) par(new=TRUE) plot(x, y2, lty=2,type="l", xlab="x-axis", ylab= "y-axis") I dont think "barplot" is the right thing to use here since I cant specify the x-values....what can I do to make this graph look better? Regards, Anne-Katrin Using par(new=TRUE) can be tricky. A better approach is to create one plot, then add the other information to it. You can add bars to an existing graph using barplot with add=TRUE, you can add lines to an existing plot using the lines function. If you give more detail of what you want (examples of x, y, and ynew) then we may be able to give more help. From: r-help-bounces@r-project.org on behalf of Anne-Katrin Link Sent: Thu 4/10/2008 7:18 AM To: R Help Subject: [R] two graphs in one figure? Dear all, how can I plot a line graph and a bar graph in one single figure? I tried to combine "barplot" and "plot". Even though they both have the same x-values (1 to 55), it just doesnt look as if they match in their scale (the barplot is much wider than the "plot"....even though I tried to put limits on the x-axis). Here is an example of what I did: barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) ... par(new=TRUE) plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) Another question: how can I make sure that the "0"-values from the barchart are displayed as well? Thank you so much! Anne-Katrin -- Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx [[alternative HTML version deleted]]
Dear Greg, thank you again for your help. The graph looks much better now. However, I need the bars to be assigned to the left y-axis, and the lines should be assigned to the right y-axis (the y-axes have different ranges,e.g. left y-axis 0-20, and right y-axis 0-1). Also would be nice if the bars had contour lines (more like the barplot)... would that be possible? Thank you so much again. Anne-Katrin -------------------------------------------------------------------------------------------------------- Barplot is probably not the best way here (it is possible, but more work than it is worth). Here is one approach that gives something similar to what you want: plot(x, y, lend=1, lwd=25, ylim=c(-1,10), xlim=c(0.75,5.25),type='h', col='grey') lines(x, y2) Is that good enough? or do you need the bars to look much more like the barplot? From: Anne-Katrin Link [mailto:anne.link@gmx.de] Sent: Sat 4/12/2008 7:11 AM To: Greg Snow Cc: r-help@r-project.org Subject: RE: [R] two graphs in one figure? Dear Greg, dear all, thank you for your reply! To clarify, here is an example of what I want to do (but better, of course!): x<- c(1,2,3,4,5) y<- 0:4 y2<- c(0,0,7,8,9) barplot(y ,ylim=c(-1,10), ylab="", xaxt="n",yaxt="n", main="") axis(4,at=c(1,2,3,4,5,6)) text(6,2.9, "2nd y-axis", srt = 270, xpd = TRUE) par(new=TRUE) plot(x, y2, lty=2,type="l", xlab="x-axis", ylab= "y-axis") I dont think "barplot" is the right thing to use here since I cant specify the x-values....what can I do to make this graph look better? Regards, Anne-Katrin Using par(new=TRUE) can be tricky. A better approach is to create one plot, then add the other information to it. You can add bars to an existing graph using barplot with add=TRUE, you can add lines to an existing plot using the lines function. If you give more detail of what you want (examples of x, y, and ynew) then we may be able to give more help. From: r-help-bounces@r-project.org on behalf of Anne-Katrin Link Sent: Thu 4/10/2008 7:18 AM To: R Help Subject: [R] two graphs in one figure? Dear all, how can I plot a line graph and a bar graph in one single figure? I tried to combine "barplot" and "plot". Even though they both have the same x-values (1 to 55), it just doesnt look as if they match in their scale (the barplot is much wider than the "plot"....even though I tried to put limits on the x-axis). Here is an example of what I did: barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55)) ... par(new=TRUE) plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55)) Another question: how can I make sure that the "0"-values from the barchart are displayed as well? Thank you so much! Anne-Katrin -- Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx [[alternative HTML version deleted]]