2017-05-04 1 views
1

enter image description here Je veux créer un terrain de tige dans R. J'ai un code Matlab, mais ne savent pas comment écrire le même code dans R. Le code Matlab comme suitComment créer un tracé de tige dans R?

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). 
+1

avez-vous googlé? https://www.r-bloggers.com/matlab-style-stem-plot-with-r/ – MichaelChirico

+0

Stack Overflow n'est pas un service de traduction de code. Vous devez inclure les données dans un [format R reproductible] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) et décrire plus précisément la sortie désirée (un lien à une image d'exemple serait utile). – MrFlick

+0

Oui, je l'ai fait googlé et j'ai vu ce code. Cependant, j'ai deux groupes (A et B) et je veux tracer des niveaux de plomb (axe y) au fil du temps (axe des x - par exemple 0, 1, 2, 3) – browndynamite

Répondre

0

Voici un point de départ pour votre 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

+0

Merci beaucoup ... C'est exactement moi cherchait .... – browndynamite