【问题标题】:How to create multiple line chart in r如何在r中创建多个折线图
【发布时间】:2018-12-02 16:19:02
【问题描述】:

我在这个结构中有一个数据框

Date          x1      x2      x3    x4
1/2/2018  500000   10000     10     80000 
1/3/2018  600000   15000     13     70000
1/4/2018  300000   8000      7      40000

如何在同一个图表中创建一个带有 4 个 x 变量的 ggplot 折线图,而且由于 x3 相对于其他值而言是如此之小,它可能会在图表中丢失,有什么技巧可以解决这个问题吗?

谢谢。

【问题讨论】:

    标签: r ggplot2 plyr


    【解决方案1】:

    首先是数据集。

    df1 <- read.table(text = "
    Date          x1      x2      x3    x4
    1/2/2018  500000   10000     10     80000 
    1/3/2018  600000   15000     13     70000
    1/4/2018  300000   8000      7      40000                  
    ", header = TRUE)
    df1$Date <- as.Date(df1$Date, "%m/%d/%Y")
    

    下面将使用log10 比例绘制三条线。

    library(ggplot2)
    
    long <- reshape2::melt(df1, id.vars = "Date")
    ggplot(long, aes(x = Date, y = value, 
                     group = variable, colour = variable)) +
      geom_line() +
      scale_y_log10() 
    

    【讨论】:

      【解决方案2】:
      data <- airquality %>%
          group_by(Month) %>%
          summarise(mean_Ozone = mean(Ozone, na.rm=TRUE),
                   mean_Solar.R = mean(Solar.R, na.rm=TRUE),
                   mean_Wind = mean(Wind),
                   mean_Temp = mean(Temp))
      
      data2 <- reshape2::melt(data, id.vars = "Month")
      ggplot(data2, aes(x = Month, y = value, 
                       group = variable, colour = variable)) +geom_line() 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-28
        • 1970-01-01
        • 2018-10-19
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        相关资源
        最近更新 更多