# How to plot this simple step function?

5 messages
Open this post in threaded view
|

## How to plot this simple step function?

 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. 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?
Open this post in threaded view
|

## Re: How to plot this simple step function?

 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:<[hidden email]>        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 <[hidden email]> 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. > > > >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. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: How to plot this simple step function?

 Thank you for your reply! When I copy and paste the code into the Console, I receive several errors! ```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) ```1) Error in plot(x, y, type = "n") : object 'x' not found > segments(x[-length(x)],y[-length(x)],x[-1],y[-length(x)]) Error in segments(x[-length(x)], y[-length(x)], x[-1], y[-length(x)]) :   object 'x' not found 2)Error in points(x[-length(x)], y[-length(x)], pch = 16) :   object 'x' not found points(x[-1],y[-length(x)],pch=1) 3) Error in points(x[-1], y[-length(x)], pch = 1) : object 'x' not found. What am I doing wrong? :'(