【问题标题】:Plot 2 line graphs on a common Y axis在公共 Y 轴上绘制 2 个折线图
【发布时间】:2019-07-20 22:41:21
【问题描述】:

我有以下数据集:

我试图在同一个图表上绘制 LOS 和 UCL,共享同一个 Y 轴,我需要 enter image description here 来更改 Y 轴的限制应该是动态的。问题是第二条线没有正确绘制在 Y 轴上。

orderno   LOS Moving Range    LCL UCL
1 7.313873776 0   -0.913600998    19.359629
2 15.54207077 8.228196989 -0.913600998    19.359629
3 9.792033819 5.750036947 -0.913600998    19.359629
4 4.244835588 5.54719823  -0.913600998    19.359629
5 9.013500345 4.768664756 -0.913600998    19.359629
6 6.264738986 2.748761358 -0.913600998    19.359629
7 12.0482677  5.783528714 -0.913600998    19.359629
8 5.156349619 6.891918081 -0.913600998    19.359629
9 11.12905351 5.97270389  -0.913600998    19.359629
10    7.689381194 3.439672315 -0.913600998    19.359629
11    6.420359658 1.269021535 -0.913600998    19.359629
12    13.652095   7.231735346 -0.913600998    19.359629
13    11.17130802 2.480786982 -0.913600998    19.359629
14    11.04367016 0.127637864 -0.913600998    19.359629
15    6.804112643 4.239557515 -0.913600998    19.359629
16    7.148401019 0.344288376 -0.913600998    19.359629
17    11.51509024 4.366689225 -0.913600998    19.359629
18    10.13792628 1.377163962 -0.913600998    19.359629
19    10.07211623 0.065810049 -0.913600998    19.359629
20    8.301095504 1.771020729 -0.913600998    19.359629
library(ggplot2)
LOS<-dataset$LOS
Order<-dataset$orderno
UCL<-dataset$UCL
LCL<-dataset$LCL

ggplot(data = dataset, aes(x = Order, y = LOS,colour='blue')) +
    coord_cartesian(ylim = c(min(LOS)-3, max(LOS)*1.3))+
    geom_line() +geom_point()+guides(colour= FALSE)+
    geom_line(aes(x = Order, y = UCL,colour='deeppink3'))+geom_point()+guides(colour= FALSE)

当我使用以下代码时,UCL 线位于图形的顶部边缘。

我希望两条线都跟随 Y 轴

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如果您没有在ggplot 命令中定义 x,y,则可以避免混淆,但稍后在 geom_line 和 geom_point 中为您想要的每条线定义。

    如果您以“长”格式重新构造数据,效率会更高。如果您不想要所有行,则可以使用filter(variable == "LOS"|variable == "UCL") 过滤掉一些行

    library(tidyverse)
    
    dataset <-
      read.table(text = "
    orderno   LOS Moving_Range    LCL UCL
    1 7.313873776 0   -0.913600998    19.359629
    2 15.54207077 8.228196989 -0.913600998    19.359629
    3 9.792033819 5.750036947 -0.913600998    19.359629
    4 4.244835588 5.54719823  -0.913600998    19.359629
    5 9.013500345 4.768664756 -0.913600998    19.359629
    6 6.264738986 2.748761358 -0.913600998    19.359629
    7 12.0482677  5.783528714 -0.913600998    19.359629
    8 5.156349619 6.891918081 -0.913600998    19.359629
    9 11.12905351 5.97270389  -0.913600998    19.359629
    10    7.689381194 3.439672315 -0.913600998    19.359629
    11    6.420359658 1.269021535 -0.913600998    19.359629
    12    13.652095   7.231735346 -0.913600998    19.359629
    13    11.17130802 2.480786982 -0.913600998    19.359629
    14    11.04367016 0.127637864 -0.913600998    19.359629
    15    6.804112643 4.239557515 -0.913600998    19.359629
    16    7.148401019 0.344288376 -0.913600998    19.359629
    17    11.51509024 4.366689225 -0.913600998    19.359629
    18    10.13792628 1.377163962 -0.913600998    19.359629
    19    10.07211623 0.065810049 -0.913600998    19.359629
    20    8.301095504 1.771020729 -0.913600998    19.359629",
                 sep = "",
                 header = T)
    
    # your example data
    LOS<-dataset$LOS
    Order<-dataset$orderno
    UCL<-dataset$UCL
    LCL<-dataset$LCL
    
    # dataset %>% 
    ggplot(data = dataset) +
      coord_cartesian(ylim = c(min(LOS)-3, max(LOS)*1.3))+
      geom_line(aes(x=orderno, y=LOS), color = "blue") +
     geom_point(aes(x=orderno, y=LOS), color = "blue") +
      geom_line(aes(x = orderno, y = UCL),colour='deeppink3')+
      geom_point(aes(x = orderno, y = UCL),colour='deeppink3')
    

    # more efficient with restructured data
    dataset %>% 
      gather(variable, value, -orderno) %>% 
      ggplot(aes(orderno, value, color = variable)) +
      geom_line() +
      scale_color_manual(values = c("black", "red", "blue", "deeppink3", "deeppink3"))
    

    reprex package (v0.3.0) 于 2019 年 7 月 20 日创建

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多