简体   繁体   中英

Plot 2 line graphs on a common Y axis

I have below dataset:

I am trying to plot LOS and UCL on the same graph sharing the same Y Axis and I need enter image description here to change the limits of the Y axis should be dynamic. The issue is the second line is not correctly plotted against the Y axis.

 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)

When i used the following code, the UCL line is on the top edge of the graph.

I expected the both lines to follow the Y axis

You may avoid confusion if you don't define the x,y in the ggplot command, but do it later in the geom_line and geom_point for each of the lines you want.

It is more efficient if you restructure the data in "long" format. If you don't want all lines, then some could be filtered out with 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"))

Created on 2019-07-20 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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