2

enter image description hereI want to create a stem plot in R. I do have a matlab code but do not know how to write the same code in R. The matlab code as follow

x = 0:25;
y = [exp(-.07*x).*cos(x);exp(.05*x).*cos(x)]';
h = stem(x,y);
set(h(1),'MarkerFaceColor','blue')
set(h(2),'MarkerFaceColor','red','Marker','square')

h(1) is the handle to the stemseries object plotting the expression exp(-.07*x).*cos(x).
h(2) is the handle to the stemseries object plotting the expression exp(.05*x).*cos(x).
browndynamite
  • 133
  • 1
  • 7
  • 2
    have you googled? https://www.r-bloggers.com/matlab-style-stem-plot-with-r/ – MichaelChirico May 04 '17 at 03:14
  • Stack Overflow is not a code translation service. You should include data in a [reproducible R format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and more precisely describe the desired output (a link to an example picture would be helpful). – MrFlick May 04 '17 at 04:53
  • Yes I did googled and I saw that code. However, I have two groups (A and B) and I want to plot Lead levels (y axis) over time (x axis - for example 0 ,1 ,2, 3) – browndynamite May 04 '17 at 05:26

1 Answers1

0

Here is a starting point for your code.

x <- 0:25
y <- cbind(exp(-.07*x)*cos(x), exp(.05*x)*cos(x))

df <-  data.frame(y=c(exp(.05*x)*cos(x),exp(-.07*x)*cos(x)),
   x=rep(x,2), grp=factor(rep(c(1,2),each=length(x))))

library(ggplot2)
p <- ggplot(aes(group=grp, col=grp, shape=grp), data=df) + 
    geom_hline(aes(yintercept=0)) +
    geom_segment(aes(x,y,xend=x,yend=y-y)) + 
    geom_point(aes(x,y),size=3) 
p

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58