2017-05-04 55 views
1

enter image description here我想創建R的幹情節我有一個MATLAB代碼,但不知道如何寫在R相同的代碼的MATLAB代碼如下如何在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

你用Google搜索? https://www.r-bloggers.com/matlab-style-stem-plot-with-r/ – MichaelChirico

+0

堆棧溢出不是代碼翻譯服務。您應該將數據包含在[可重現的R格式](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)中,並更精確地描述所需輸出(鏈接以示例圖片將是有益的)。 – MrFlick

+0

是的,我做了Google搜索,我看到了代碼。但是,我有兩個組(A和B),我想繪製一段時間內的鉛水平(y軸)(x軸 - 例如0,1,2,3) – browndynamite

回答

0

這裏是一個起點你的代碼。

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

非常感謝......這正是我正在尋找.... – browndynamite