dan.weavesham
2010-Jun-24 07:44 UTC
[R] Correctly plotting bar and scatter chart on 2-y axis plot with par(new=T)
Hello Nabble users, A question about having two y axes on a chart. I'm trying to show on the left (first) y axis a measure used in an experiment; the x axis shows the experiment number, that has been sorted in a specific manner -- i.e. not Experiments 1,2,3,n, but could be Experiment 6,1,18,n; the right (second) y axis shows an experimental variable. The idea of the chart is to show how results of the different experiments compare with one of the key variables set in the experiment. The bar chart uses sorted data for its x axis, which have been determined by experimental settings. So instead of the values on the x axis increasing 1,2,3,n, they instead refer to "Experiment 2", "Experiment 17", "Experiment 1", and so on. However, when I come to plot the second y axis, the scatter plot does not work as anticipated. Instead of plotting the second axis data against "Experiment 2", "Experiment 17", the data is plotted in order -- i.e. 1,2,n. The R procedure I'm using is: 1/ plot the bar chart 2/ use par(new=T) to allow me to produce the second y axis 3/ plot the scatter chart, on said second axis Now, I'm assuming that this is due to my use of the par(new=T) function, which I must admit I found on the internet, and am not completely sure what it does. From what I can tell, however, it resets the plot area, so that when I go to plot my scatter chart, the x axis set up in step 1/ is reset. As such, does anyone have any suggestions on how to make this work? Thanks in asvance. -- View this message in context: http://r.789695.n4.nabble.com/Correctly-plotting-bar-and-scatter-chart-on-2-y-axis-plot-with-par-new-T-tp2266589p2266589.html Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2010-Jun-24 08:17 UTC
[R] Odp: Correctly plotting bar and scatter chart on 2-y axis plot with par(new=T)
Hi r-help-bounces at r-project.org napsal dne 24.06.2010 09:44:49:> > Hello Nabble users, > > A question about having two y axes on a chart. I'm trying to show on the > left (first) y axis a measure used in an experiment; the x axis showsthe> experiment number, that has been sorted in a specific manner -- i.e. not > Experiments 1,2,3,n, but could be Experiment 6,1,18,n; the right(second) y> axis shows an experimental variable. The idea of the chart is to showhow> results of the different experiments compare with one of the keyvariables> set in the experiment. > > The bar chart uses sorted data for its x axis, which have beendetermined by> experimental settings. So instead of the values on the x axis increasing > 1,2,3,n, they instead refer to "Experiment 2", "Experiment 17","Experiment> 1", and so on. > > However, when I come to plot the second y axis, the scatter plot doesnot> work as anticipated. Instead of plotting the second axis data against > "Experiment 2", "Experiment 17", the data is plotted in order -- i.e.1,2,n.> > The R procedure I'm using is: > > 1/ plot the bar chart > 2/ use par(new=T) to allow me to produce the second y axis > 3/ plot the scatter chart, on said second axis > > Now, I'm assuming that this is due to my use of the par(new=T) function, > which I must admit I found on the internet, and am not completely surewhat> it does. From what I can tell, however, it resets the plot area, so that > when I go to plot my scatter chart, the x axis set up in step 1/ isreset. Yes. You need to reset x values in desired order. But the exact code depends on what date you are plotting, how they are organised and what shall be desired order. Or you can use of values which are returned by barplot and use them in further line adding after rescaling to fit y axis, something like bbb<-barplot(VADeaths, beside = TRUE) lines(bbb[,1], rnorm(5)+20) Regards Petr> > As such, does anyone have any suggestions on how to make this work? > > Thanks in asvance. > -- > View this message in context: http://r.789695.n4.nabble.com/Correctly- >plotting-bar-and-scatter-chart-on-2-y-axis-plot-with-par-new-T-tp2266589p2266589.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Greg Snow
2010-Jun-24 16:24 UTC
[R] Correctly plotting bar and scatter chart on 2-y axis plot with par(new=T)
Using par(new=T) is dangerous and tricky for those people who understand what it does and how to use it. Trying to use it without fully understanding it will be much worse. I would use the updateusr function from the TeachingDemos package instead. The first example on the help page may give you the answer that you need (but it is hard to tell from your description without code). If that is not enough, send the actual reproducible code (either include the data in a copy/pasteable way, or use builtin or simulated data) that you tried and explain how it differs from what you want the output to look like, then we will have a chance of being able to help. -- 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 dan.weavesham > Sent: Thursday, June 24, 2010 1:45 AM > To: r-help at r-project.org > Subject: [R] Correctly plotting bar and scatter chart on 2-y axis plot > with par(new=T) > > > Hello Nabble users, > > A question about having two y axes on a chart. I'm trying to show on > the > left (first) y axis a measure used in an experiment; the x axis shows > the > experiment number, that has been sorted in a specific manner -- i.e. > not > Experiments 1,2,3,n, but could be Experiment 6,1,18,n; the right > (second) y > axis shows an experimental variable. The idea of the chart is to show > how > results of the different experiments compare with one of the key > variables > set in the experiment. > > The bar chart uses sorted data for its x axis, which have been > determined by > experimental settings. So instead of the values on the x axis > increasing > 1,2,3,n, they instead refer to "Experiment 2", "Experiment 17", > "Experiment > 1", and so on. > > However, when I come to plot the second y axis, the scatter plot does > not > work as anticipated. Instead of plotting the second axis data against > "Experiment 2", "Experiment 17", the data is plotted in order -- i.e. > 1,2,n. > > The R procedure I'm using is: > > 1/ plot the bar chart > 2/ use par(new=T) to allow me to produce the second y axis > 3/ plot the scatter chart, on said second axis > > Now, I'm assuming that this is due to my use of the par(new=T) > function, > which I must admit I found on the internet, and am not completely sure > what > it does. From what I can tell, however, it resets the plot area, so > that > when I go to plot my scatter chart, the x axis set up in step 1/ is > reset. > > As such, does anyone have any suggestions on how to make this work? > > Thanks in asvance. > -- > View this message in context: http://r.789695.n4.nabble.com/Correctly- > plotting-bar-and-scatter-chart-on-2-y-axis-plot-with-par-new-T- > tp2266589p2266589.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.