Simulation of a Poisson Process I am trying to simulate the compound Poisson process using the next algorithm that I found in a textbook on stochastic processes. 

  
*
  
*Let $S_0 = 0$.  
  
*Generate i.i.d. exponential random variables $X_1, X_2, \ldots$  
  
*Let $S_n=X_1+\cdots + X_n,$ for $n = 1, 2, \ldots$  
  
*For each $k = 0, 1, \ldots,$ let $N_t = k,$ for $S_k\le t\le S_{k+1}$  
  

S <- vector(mode="integer", length=100)
S[1] = 0
## Generation of Exponential random variables with parameter lambda
X <- rexp(n=100, rate=0.1)

for(n in 1:100){
  S[n] = sum(X[1:n])
}    

But, I am not clear about how to write the step $4$, maybe I need to put the integer between two $S_k$ (the arrival times)? I am interested in the counting process $N_t$.
Furthermore, how do you plot it? 
I have something like that, but I am not clear if the arrival times are ok, because, in the first plot on the left, I see that every arrival time is not necessarily in the vertical line of the process, and on the right all the arrival times have the same length,  I think that it contradicts the independence of the arrival times.    
nro<-10
S<-vector(mode="integer",length = nro)
S[1]=0

##Generation of Exponential random variables with parameter lambda
X<-rexp(n =nro,rate = 2)
for(n in 1:nro)
S[n]=sum(X[1:n])
S<-cumsum(X)
n_func <- function(t, S) sapply(t, function(t) sum(S < t))
t_series <- seq(0, max(S), by = max(S)/nro)

#Plot of the trajectory and add lines in the arrival times
par(mfrow=c(1,2))
plot(t_series, n_func(t_series, S),type = 
"s",ylab=expression(N[t]),xlab="t",las=1,cex.lab=0.8,main="Poisson 
   Process",cex.axis=0.8)
grid()
abline(v = S,col="red",lty=2)
plot(t_series, n_func(t_series, S),type = 
"s",ylab=expression(N[t]),xlab="t",las=1,cex.lab=0.8,main="Poisson 
 Process",cex.axis=0.8)
grid()
abline(v = t_series,col="blue",lty=4)


 A: You want to get a function of $t$ that gives the count of events. So simply do
n_func <- function(t, S) sapply(t, function(t) sum(S <= t))
t_series <- seq(0, max(S), by = max(S)/100)
plot(t_series, n_func(t_series, S)

$S$ is basically the time stamps of each Poisson events in your sample. So you want to just count the number of events that have time stamped before $t$.
A: The Poisson process is describing how events occur over time.  The inter-arrival times (i.e., the times between each successive event) are distributed as an exponential.  In the algorithm provided, $S_n$ is the total amount of time that has elapsed since you started recording until the $n$th event occurred.  Since your $X_i$s are the times between each event, you sum them from the start up to the event of interest to get the total elapsed time until that event.  The $N_t$s are the number of events that have occurred up to any given point in time.  Thus, the plot would be a set of horizontal lines at successive integers, where the x-axis was time.  
Here is a simple example, coded in R:  
set.seed(3535)             # this makes the example exactly reproducible
                           # there's no need for an explicit s[0]
X = rexp(n=100, rate=0.1)  # step 2
S = cumsum(X)              # step 3
N = 0:100                  # step 4

windows()
  plot(stepfun(x=S, y=N), main="Counting Process",
       xlab="Time", ylab="Number of events")


