Hi, I am just starting to learn R, sorry for asking a simple question. How can plot a line x <= 0 y = 0, x > 0 y = 1? Thank you. - j [[alternative HTML version deleted]]
On 06/02/2016 7:11 AM, jupiter wrote:> Hi, > > I am just starting to learn R, sorry for asking a simple question. How can > plot a line x <= 0 y = 0, x > 0 y = 1? >There are lots of ways. One is curve(ifelse(x < 0, 0, 1), from=-2, to=2) This isn't perfectly vertical at x=0; a more accurate approach would be to work out the coordinates of the endpoints and two corners in the appropriate order, and join them by lines. For example, plot(c(-2,0,0,2), c(0,0,1,1), type="l") In either case you'll probably want to change axis labels using xlab or ylab arguments. Duncan Murdoch
> On Feb 6, 2016, at 4:11 AM, jupiter <jupiter.hce at gmail.com> wrote: > > Hi, > > I am just starting to learn R, sorry for asking a simple question. How can > plot a line x <= 0 y = 0, x > 0 y = 1?There is a stepfun function and an associated plotting method: y0 <- c(rep(0,3),rep(1,3)) sfun0 <- stepfun(-2:2, y0, right=TRUE) plot(sfun0) -- David Winsemius Alameda, CA, USA
On 07/02/16 01:11, jupiter wrote:> Hi, > > I am just starting to learn R, sorry for asking a simple question. How can > plot a line x <= 0 y = 0, x > 0 y = 1?One way: plot(c(-1,0,1),c(0,1,1),type="s",xlab="x",ylab="y") cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
All of which should suggest: 1. Before posting further, spend some time with an R tutorial or two. "An Intro to R" ships with R; and links to some of the many excellent web resources can be found here: https://www.rstudio.com/resources/training/online-learning/#R 2. Search! A web search of "plot step function in R" would have brought up stepfun() and other relevant links. rseek.org or the sos package -- or even the RSiteSearch() function -- are other ways to find about R and R package functionality. See also the CRAN task view pages. Typically, answers to basic questions like the OP's can be found more quickly and easily through such means; and with the added benefit of providing examples and connections to related material. Of course, if **after** consulting such resources questions still remain, asking here is usually helpful. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Feb 6, 2016 at 10:29 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:> On 07/02/16 01:11, jupiter wrote: >> >> Hi, >> >> I am just starting to learn R, sorry for asking a simple question. How can >> plot a line x <= 0 y = 0, x > 0 y = 1? > > > One way: > > plot(c(-1,0,1),c(0,1,1),type="s",xlab="x",ylab="y") > > cheers, > > Rolf Turner > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
On 2016-02-06 19:23, David Winsemius wrote:> >> On Feb 6, 2016, at 4:11 AM, jupiter <jupiter.hce at gmail.com> wrote: >> >> Hi, >> >> I am just starting to learn R, sorry for asking a simple question. How can >> plot a line x <= 0 y = 0, x > 0 y = 1? > > There is a stepfun function and an associated plotting method: > > y0 <- c(rep(0,3),rep(1,3)) > sfun0 <- stepfun(-2:2, y0, right=TRUE) > plot(sfun0)I would like to suggest the correct way: Replace the last line by > plot(sfun0, verticals = FALSE) G?ran Brostr?m
Thank you for the all response, how can the point y (0.0) on the same x axis, and X increases 1 between [-4, 4]? On Sun, Feb 7, 2016 at 5:29 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:> On 07/02/16 01:11, jupiter wrote: > >> Hi, >> >> I am just starting to learn R, sorry for asking a simple question. How can >> plot a line x <= 0 y = 0, x > 0 y = 1? >> > > One way: > > plot(c(-1,0,1),c(0,1,1),type="s",xlab="x",ylab="y") > > cheers, > > Rolf Turner > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 >[[alternative HTML version deleted]]