如果观察次数不相等,如何在同一张图中绘制两条线?

时间:2019-05-28 14:35:32

标签: r plot

我有两个数据框,每个数据框包含一个“日期”和一个“价格”列。 我现在想根据这两个数据帧的观察结果生成一个带有两个图的图形。我的问题是日期不相等。我的数据如下:

Date1 <- seq.Date(from = as.Date("2015-08-07"), to = as.Date("2015-08-16"), by = "days") 
Price1 <- c(2.5,6.3,1.2,2.4,4.2,5.1,7.3,9.2,12.5,8.7) 
df1 <- data.frame(Date1, Price1)

Date2 <- seq.Date(from=as.Date("2015-08-11"), to = as.Date ("2015-08-16"), by = "days") 
Price2 <- c(24.3,21.2,18.4,16.2,17.1,15.6) 
df2 <- data.frame(Date2, Price2)


df1
#         Date1 Price1
# 1  2015-08-07    2.5
# 2  2015-08-08    6.3
# 3  2015-08-09    1.2
# 4  2015-08-10    2.4
# 5  2015-08-11    4.2
# 6  2015-08-12    5.1
# 7  2015-08-13    7.3
# 8  2015-08-14    9.2
# 9  2015-08-15   12.5
# 10 2015-08-16    8.7


df2
#        Date2 Price2
# 1 2015-08-11   24.3
# 2 2015-08-12   21.2
# 3 2015-08-13   18.4
# 4 2015-08-14   16.2
# 5 2015-08-15   17.1
# 6 2015-08-16   15.6

要在一个图中创建两个图,我使用以下代码:

par(mar = c(5, 5, 9, 5))
plot(df1[,1],df1[,2], log = "y", type ="l", col = "orange")
par(new = TRUE)
plot(df2[,2],log = "y", type = "l",col = "blue")

我想收到的是一张图,其中df1的图从头开始,而df2的图在以后的某个时间点开始。

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以在一个数据帧中传递它们,以便日期的长度相同:

import Vue from "vue";
import HelloComponent from "./components/Hello.vue";
import MathComponent from "./components/Math.vue";

let v = new Vue({
    el: "#app",
    template: `
    <div>
        Name: <input v-model="name" type="text"/> <br/>
        <hello-component :name="name" :initialEnthusiasm="1" />
        <p></p>
        Math: 
        <input v-model="firstNumber" type="Number"/> 
            <select v-model="mathSymbol" type="text">
                <option>+</option>
                <option>-</option>
            </select> 
         <input v-model="secondNumber" type="Number"/>
        <math-component :firstNumber="firstNumber" :secondNumber="secondNumber" :mathSymbol="mathSymbol"/>
    </div>
    `,
    data: { name: "World", firstNumber:3, secondNumber:4, mathSymbol:"+"},
    components: {
        HelloComponent,
        MathComponent
    }
});

然后使用df3 <- merge(df1, df2, by.x = 'Date1', by.y = 'Date2', all.x = TRUE) 进行绘制以覆盖第二行:

lines

请确保相应地添加限制,因为绘图调用是设置轴的一个。

enter image description here

答案 1 :(得分:0)

使用const express = require('express'); const jwt = require('jsonwebtoken'); const passport = require('passport'); const router = express.Router(); require('../config/passport')(passport); const User = require('../models').User; router.post('/signin', function(req, res) { User .find({ where: { username: req.body.username } }) .then((user) => { if (!user) { return res.status(401).send({ message: 'Authentication failed. User not found.', }); } user.comparePassword(req.body.password, (err, isMatch) => { if(isMatch && !err) { var token = jwt.sign(JSON.parse(JSON.stringify(user)), 'nodeauthsecret', {expiresIn: 86400 * 30}); jwt.verify(token, 'nodeauthsecret', function(err, data){ console.log(err, data); }) res.json({success: true, token: 'JWT ' + token}); } else { res.status(401).send({success: false, msg: 'Authentication failed. Wrong password.'}); } }) }) .catch((error) => res.status(400).send(error)); }); 而不是lines()可以将其添加到基本图形中。并且在执行此操作时,您要确保第一个图绘制了整个值范围,因此我们明确设置了par(add=TRUE)xlim=属性。

ylim=

enter image description here

答案 2 :(得分:0)

如果您对tidyverse中的方法感兴趣:

df1 %>% 
full_join(df2, by = c("Date1" = "Date2")) %>% 
gather(key, value, -Date1) %>% 
ggplot(aes(x = Date1, y = value, group = key, col = key)) + 
geom_line()

这是结果:

enter image description here