1

在此处输入图像描述我想在 R 中创建一个茎图。我有一个 matlab 代码,但不知道如何在 R 中编写相同的代码。matlab 代码如下

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).
4

1 回答 1

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

在此处输入图像描述

于 2017-05-04T11:07:45.027 回答