[R] Correctly plotting bar and scatter chart on 2-y axis plot with par(new=T)

Jim Lemon jim at bitwrit.com.au
Fri Jun 25 12:13:53 CEST 2010


On 06/25/2010 05:47 AM, dan.weavesham wrote:
>
> Hello,
>
> Thanks for the advice so far -- still struggling with it, I must admit.
>
> Here is some sample data, which I hope helps:
>
> # y axis #1 -- data for the bar chart
> -30353.382 -21693.519   -7049.923  -72968.722  -10267.584 -269432.795
> -19847.670 -686283.171 -376231.754 -597800.080 -274637.587 -112663.167
> -39550.445 -133916.431
>
> # x axis -- in specific order, so cannot be tampered with !! ;-)
> 1  7 13  2  8 14  3  9  4 10  5 11  6 12
>
> # y axis #2 -- scatter chart
> 50  25   5  25   5 100   5 100 100  75  75  50  50  50
>
> Does this help explain what I'm looking to do? If not, is there a way I can
> get plot() to not change the order of the x axis data points -- so instead
> of plotting 1,2,3,n as per my original post, it plots 1,7,13,n? (I've tried
> coercing the data into character format with no luck)
>
Hi Dan,
Does this do what you want?

# y axis #1 -- data for the bar chart
y1<-c(-30353.382,-21693.519,-7049.923,-72968.722,-10267.584,-269432.795,
  -19847.670,-686283.171,-376231.754,-597800.080,-274637.587,-112663.167,
  -39550.445,-133916.431)
# x axis -- in specific order, so cannot be tampered with !! ;-)
# these are really the x tick labels
x<-c(1,7,13,2,8,14,3,9,4,10,5,11,6,12)
# y axis #2 -- scatter chart
y2<-c(50,25,5,25,5,100,5,100,100,75,75,50,50,50)
library(plotrix)
twoord.plot(1:14,y1,1:14,y2,type=c("bar","p"),lcol=2,rcol=4,
  lylim=c(-70000,70000),rylim=c(-100,100),xtickpos=1:14,xticklab=x)

Jim



More information about the R-help mailing list