在共同的Y轴上绘制2个折线图

时间:2019-07-20 22:41:21

标签: r ggplot2

我有以下数据集:

我正在尝试在共享相同Y轴的同一图上绘制LOS和UCL,并且我需要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轴

1 个答案:

答案 0 :(得分:0)

如果您没有在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-07-20创建