【问题标题】:ggplot2 one plot with two graphs from CSV in Rggplot2 一张图,两张图来自 R 中的 CSV
【发布时间】:2017-11-01 11:14:56
【问题描述】:

我正在尝试读取 CSV:

5.0;72.0;
6.0;72.0;
4.0;72.0;
5.0;72.0;
4.0;72.0;

...并且有 one ggplot2two 功能:第一列和第二列,两者颜色不同。

到目前为止我已经尝试过:

>dt <- fread('C:\\Users\\csvFile.txt')  
>print(dt)
    V1 V2 V3
 1:  5 72 NA
 2:  6 72 NA
 3:  4 72 NA
 4:  5 72 NA
 5:  4 72 NA

...现在我被困住了。如何在同一个图中同时绘制 V1 和 V2?

我知道如何使绘图着色和连续,但我不知道如何实际绘制值:

>ggplot(dt, aes(x=x, y=y)) + geom_line() +geom_area(fill="blue")

我想要的图表看起来像这样(除了我没有 X 轴(“密度”),因为我的值是一个简单的时间序列):

【问题讨论】:

    标签: r csv ggplot2


    【解决方案1】:

    我会做这样的事情......

    library(tidyr) #for the gather
    df$time = seq_along(df$V1) #add your time variable
    df2 <- df %>% gather(key=type,value=value,-time) #convert to long format
    ggplot(df2,aes(x=time,y=value,fill=type))+
              geom_area(alpha=0.2,position="identity")
    

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多