From: Anne-Katrin Link <anne.link_at_gmx.de>

Date: Sat, 12 Apr 2008 15:11:12 +0200

To: R Help

Subject: [R] two graphs in one figure?

Date: Sat, 12 Apr 2008 15:11:12 +0200

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_at_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]]Received on Sat 12 Apr 2008 - 13:16:01 GMT______________________________________________ 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.

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Sat 12 Apr 2008 - 17:30:28 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*