[R] How to plot this simple step function?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Jan 17 07:43:13 CET 2013


Base Graphics:

plot(x,y,type="n")
segments(x[-length(x)],y[-length(x)],x[-1],y[-length(x)])
points(x[-length(x)],y[-length(x)],pch=16)
points(x[-1],y[-length(x)],pch=1)

Ggplot graphics:

library(ggplot2)
dta <- data.frame( x= x[-length(x)],y=y[-length(x)], xend=x[-1], yend=y[-length(x)] )
ggplot( dta, aes( x=x, y=y, xend=xend, yend=yend )) +
geom_segment()+
geom_point( shape=16, size=4 ) +
geom_point( aes( x=xend, y=yend, shape=1, size=4 )

There is also lattice graphics, but I don't feel like putting that example together right now.

As for how to use RStudio, please understand that RStudio has its own support forums. Please direct questions regarding that user interface there, and remember that the people answering questions here may be using completely different tools and operating systems than you are. I strongly recommend reading the R-help posting guide and reading other people's questions here to get the flavor of the kinds of question and answer format is preferred around here. Hint: this is a mailing list, not Nabble.

Easy tutorials? There are lots of them, but they don't necessarily get you where you want to go. I recommend reading the Introduction to R document that comes with R, with particular focus on vectors, lists,  matrices, data frames, and indexing. The apply family of functions will also be useful, but vectors and indexing are first priority. They all work together to make a very powerful combination, and learning those fundamentals will pay off in helping you decipher examples such as that ones above.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Bornin1992 <ian at mcparland.ca> wrote:

>Hi everyone, 
>
>I am trying to graph a step function in R Studio. I just learned about
>R
>today and decided to try it!  The following is what I want it to look
>like.
>I graphed it using 
>
>x <- 0:5
>y <- c(0, .2, .3, .6, .9, 1.0)
>plot(x, y, type = "s")
> 
>And used Microsoft Paint to get it to how I wanted it to look, but I
>want to
>do it in R Studio completely. 
>
><http://r.789695.n4.nabble.com/file/n4655792/plot.png> 
>
>How can I replicate this image? Also, when do I use the window above
>the
>Console?
>
>Any incredibly easy tutorials out there to get me started?
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-plot-this-simple-step-function-tp4655792.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.



More information about the R-help mailing list