rplotggplot2exponential-distribution

How do I use the rug function for an exponential distribution in R?


I am just starting out in R. I want to plot interval of times (the distribution is exponential) on x axis, with a tick mark in place every time the interval ends. If I have a string of times say (0.2, 0.8, 0.9 , 1.0) then the tick marks on the x axis would be on 0.2, 0.8, 0.9 and 1 respectively. With big data samples, I want my graph to look something like:

Spike Train

So after using,

set.seed(1)
x <- rexp(50, 0.2)

How can I go about it further, might I have to use rug function (which I am trying to learn how to use)? Can I also put time stamps on this graph?

Edit

So I have modified my command and used:

x <- c(cumsum(rexp(50, 0.2)))
y <- rep(0, length(x))
plot(x,y)
rug(x)

and I have been able to get this:

Revised Plot

This result does the work, if it's just about that. However, is there a line of command I can use to edit this result as shown in the second picture, and get a result as shown in the first picture? I would like to just get these tick marks on a horizontal line instead of the whole plot. Or it's not possible?


Solution

  • With ggplot you can use geom_rug() to .add a rug to the plot. First the data need to be made into a data.frame

    library("tidyverse")
    set.seed(1)
    x <- rexp(50, 0.2)
    
    ggplot(data.frame(x), aes(x = x)) + geom_rug()
    

    The rug is rather short (it seems to be a proportion of the graph height and not controllable).

    The opposite would be to use geom_vline which will give lines the full length of the y-axis

    #ggplot(data.frame(x), aes(xintercept = x)) + geom_vline() #doesn't work
    ggplot(data.frame(x)) + geom_vline(aes(xintercept = x))