[R] plot 2 graphs on the same x-y plane

R. Michael Weylandt michael.weylandt at gmail.com
Wed Apr 11 06:03:49 CEST 2012


You were told before this isn't a Mac question so please don't cc R-SIG-Mac.

I'm not sure what this bit of your reply means "My question is to find
any command to plot the data I got from the field;" but your reply
later suggests that your problem is that you are overriding previous
plots on a given device. By default, plot() does this -- if you want
to add elements, you need to use points() or lines() (or some function
that calls those, like abline() as I demonstrated)

E.g.,

# Removes first plot
plot(1:20)
plot(0:19, col = 2)

# Adds to plot
plot(1:20)
points(0:19, col = 2)

Michael

2012/4/10 Tawee Laoitichote <ohowow2007 at hotmail.com>:
> Dear Michael (and Davis),
>
> Your answer is not what I want to know. My question is to find any command
> to plot the data I got from the field; such as a set of (x,y) data ( I
> actually have these data) and together withe the derived ones . I brought
> these data to plot on x-y plane,  getting a graph showing some
> relation. Then, I wanted to find some linear relation, I would use least
> square method to solve having a simple function such as; y = ax + b, solving
> the a and b. So, I could plot a straight line using this function, or
> perhaps forecast some data of y which I know the value x.
>
> My problem is when I did a scatter plot by command "plot(x,y)", I got a
> graph. Whilst I plotted another graph using the above function the existing
> graph disappeared replaced be the latter function.
>
> Unfortunately, after searching a while to find the solution command, I can
> not find the command. I asked the question as to request some help not the
> example you shown.
>
> Any way, thanks for you effort.
>
> Tawee
>
> Mac OS10.7.3
>
>> From: michael.weylandt at gmail.com
>> Date: Tue, 10 Apr 2012 23:31:08 -0400
>> Subject: Re: [R] plot 2 graphs on the same x-y plane
>> To: ohowow2007 at hotmail.com
>> CC: r-help at r-project.org
>>
>> This is the same malformatted message you posted on R-SIG-Mac even
>> after David specifically asked for clarification.... not to reward bad
>> behavior, but perhaps this will enlighten:
>>
>> # Minimal reproducible data!
>> x <- runif(15, 0, 5)
>> y <- 3*x - 2 + runif(15)
>>
>> dat <- data.frame(x = x, y = y)
>> rm(list = c("x", "y"))
>>
>> # Base graphics plot using the formula interface
>> plot(y ~ x, data = dat)
>> abline(lm(y~x, data = dat), col = "red3", lwd = 2)
>>
>> Alternatively
>>
>> library(ggplot2)
>> ggplot(dat, aes(x = x, y = y)) + geom_point() + stat_smooth(method =
>> "lm", color = I("red3"))
>>
>> which is perhaps overkill in this situation.
>>
>>
>> Michael
>>
>> On Tue, Apr 10, 2012 at 11:03 PM, Tawee Laoitichote
>> <ohowow2007 at hotmail.com> wrote:
>> >
>> >
>> >
>> >
>> > hi,  I'm doing some data on least square curve fitting. What I like to
>> > have is to compare the scatter plot whilst having the fitting curve on the
>> > same coordinates. Any suggestting command besides plot(x,y).  TaweeMac OSX
>> > 10.7.3
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.



More information about the R-help mailing list