简体   繁体   English

用我的数据框的几何线制作 plot

[英]Making a plot with geom lines of my data frame

I've started using R and I'm learning how to use it,我已经开始使用 R 并且正在学习如何使用它,

I have a data frame of a stock, I want to show with ggplot/plot how the lock and open gate changes by the date, I would like to have x- as the date while y1-Open Gate,y2-Lock Gate,y3-the Change.我有一个股票的数据框,我想用 ggplot/plot 显示锁和开门如何按日期变化,我想将 x- 作为日期,而 y1-Open Gate,y2-Lock Gate,y3 -改变。

Date      Lock.Gate  Change Open.Gate
<fctr>    <dbl>      <dbl>  <dbl>
26/09/2019    237.4  1.15   234.7   
25/09/2019    234.7  0.73   233.0   
24/09/2019    233.0  0.13   232.7   
23/09/2019    232.7  0.00   232.7   
22/09/2019    232.7  1.26   229.8   

thanks谢谢

you may use ggplot contained in the tidyverse package.您可以使用ggplot中包含的 ggplot。 Unfortunately since the Change values are smaller than the other two, the plot lacks good readability.不幸的是,由于Change值小于其他两个值,因此 plot 缺乏良好的可读性。

# prepare the initial dataframe
df <- as.data.frame(
    cbind(

        c(237.4,234.7,233.0,232.7,232.7),
        c(1.15,0.73,0.13,0.00,1.26),
        c(234.7,233.0,232.7,232.7,229.8)
    )
)

df <- cbind(c('26/09/2019','25/09/2019','24/09/2019','23/09/2019','22/09/2019'), df)

colnames(df) <- c("Date","Lock.Gate","Change","Open.Gate")

df

        Date Lock.Gate Change Open.Gate
1 26/09/2019     237.4   1.15     234.7
2 25/09/2019     234.7   0.73     233.0
3 24/09/2019     233.0   0.13     232.7
4 23/09/2019     232.7   0.00     232.7
5 22/09/2019     232.7   1.26     229.8

#install & load the required package if not available
install.packages("tidyverse")
library(tidyverse)

#plot
ggplot(data=df, mapping= aes(x=Date)) + 
    geom_point(mapping=aes(y=Open.Gate, color = "Open.Gate")) +
    geom_point(mapping=aes(y=Lock.Gate, color = "Lock.Gate")) +
    geom_point(mapping=aes(y=Change, color = "Change")) +
    ylab(" ") +
    scale_colour_manual("", 
                    breaks = c("Lock.Gate", "Open.Gate", "Change"),
                    values = c("green", "red", "black")
                    )    

在此处输入图像描述

EDIT 编辑

To obtain the desired output you should run:要获得所需的 output,您应该运行:

 #change date format df$Date <- as.Date(df$Date, format="%d/%m/%Y") #plot ggplot(data=df, mapping= aes(x=Date)) + geom_line(mapping=aes(y=Open.Gate, color = "Open.Gate")) + geom_line(mapping=aes(y=Lock.Gate, color = "Lock.Gate")) + ylab(" ")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM