我想在图表中绘制两个函数。
r <- rnorm(20,0,1)
z <- c(1,1,1,1,1,-1,-1,-1,1,-1,1,1,1,-1,1,1,-1,-1,1,-1)
data <- as.data.frame(na.omit(cbind(z, r)))
series1 <- ts(cumsum(c(1,data[,2]*data[,1])))
series2 <- ts(cumsum(c(1,data[,2])))
series2的情节看起来像这样。
将一条图中的两条曲线与
组合matplot(cbind(series1, series2), xaxt = "n", xlab = "Time", ylab = "Value", col = 1:3, ann = FALSE, type = 'l')
给出了这个:
series2的曲线现在是虚线。
我有两个问题:
1)如何将虚线更改为实线?
2)轴的标题未显示在任一图表中。我该如何解决这个问题?
提前致谢。
答案 0 :(得分:1)
作为matplot
的替代方案,您可以将plot
与lines
结合使用:
plot(series1, xlab="Time", ylab="Value", xaxt="n")
lines(series2, col="red")
这给出了:
旁注:使用随机生成的值时,请始终使用set.seed()
。你可以这样做:
set.seed(1)
r <- rnorm(20,0,1)
z <- c(1,1,1,1,1,-1,-1,-1,1,-1,1,1,1,-1,1,1,-1,-1,1,-1)
data <- as.data.frame(na.omit(cbind(z, r)))
series1 <- ts(cumsum(c(1,data[,2]*data[,1])))
series2 <- ts(cumsum(c(1,data[,2])))
这使其他人可以更轻松地复制您的示例数据集。
答案 1 :(得分:1)
下面的代码可以为您提供所需的结果:
2015-05-31 11:39:11.433 Quiz Cup[10486:3083839] [Fabric] Unable to locate application icon
2015-05-31 11:39:11.488 Quiz Cup[10486:3083839] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x170088840 FBSDKLoginButton:0x124e12690'Log in'.width == TWTRLogInButton:0x124e15840.width>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-05-31 11:39:11.490 Quiz Cup[10486:3083839] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x170088840 FBSDKLoginButton:0x124e12690'Log in'.width == TWTRLogInButton:0x124e15840.width>
Container hierarchy:
<FBSDKLoginButton: 0x124e12690; baseClass = UIButton; frame = (296.5 497; 175 30); opaque = NO; layer = <CALayer: 0x170036d80>>
| <UIButtonLabel: 0x124e147a0; frame = (0 0; 0 0); text = 'Log in'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x170087f80>>
View not found in container hierarchy: <TWTRLogInButton: 0x124e15840; baseClass = UIButton; frame = (244 542; 280 40); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1740322e0>>
That view's superview: <UIView: 0x124e11960; frame = (0 0; 768 1024); autoresize = W+H; layer = <CALayer: 0x170036c00>>
2015-05-31 11:39:11.497 Quiz Cup[10486:3083839] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x170088840 FBSDKLoginButton:0x124e12690'Log in'.width == TWTRLogInButton:0x124e15840.width> view:<FBSDKLoginButton: 0x124e12690; baseClass = UIButton; frame = (296.5 497; 175 30); opaque = NO; layer = <CALayer: 0x170036d80>>'
关键选项是:
matplot(cbind(series1, series2), xaxt = "n", xlab = "Time",
ylab = "Value", col = 1:3, ann = TRUE, type = 'l',
lty = 1)
ann = TRUE