简体   繁体   English

使用ggplot2线图的疯狂线条

[英]Crazy lines using ggplot2 line plot

The dataframe 'datasim' I'm trying to plot is as below 我试图绘制的数据框“ datasim”如下

     Depth     Date       Temp
7702   0.0 19980601 -0.6577905
7703   0.1 19980601 -0.1840248
7704   0.2 19980601  0.5996339
7705   0.3 19980601  3.3119625
7706   0.4 19980601  3.9217309
7707   0.5 19980601  4.0388018
7708   0.6 19980601  4.0257338
7709   0.7 19980601  3.9543796
7710   0.8 19980601  3.8595199
7711   0.9 19980601  3.7635552
7712   1.0 19980601  3.6735954
7713   1.1 19980601  3.5917693
7714   1.2 19980601  3.5193204
7715   1.3 19980601  3.4559904  
7716   1.4 19980601  3.4009090
7717   1.5 19980601  3.3530415
7718   1.6 19980601  3.3113879
7719   1.7 19980601  3.2750543
7720   1.8 19980601  3.2432676
7721   1.9 19980601  3.2153632
7722   2.0 19980601  3.1907631
7723   2.1 19980601  3.1689482
7724   2.2 19980601  3.1494289
7725   2.3 19980601  3.1317168
7726   2.4 19980601  3.1153033
7727   2.5 19980601  3.0996626
7728   2.6 19980601  3.0843152
7729   2.7 19980601  3.0690151
7730   2.8 19980601  3.0541184
7731   2.9 19980601  3.0410244
7732   3.0 19980601  3.0322053
7733   3.1 19980601  3.0302451
7734   3.2 19980601  3.0363760
7735   3.3 19980601  3.0500890
7736   3.4 19980601  3.0698218
7737   3.5 19980601  3.0937937
7738   3.6 19980601  3.1204821
7739   3.7 19980601  3.1487569
7740   3.8 19980601  3.1778400
7741   3.9 19980601  3.2072096
7742   4.0 19980601  3.2365085
7743   4.1 19980601  3.2654762
7744   4.2 19980601  3.2939086
7745   4.3 19980601  3.3216428
7746   4.4 19980601  3.3485619
7747   4.5 19980601  3.3746048
7748   4.6 19980601  3.3998141
7749   0.0 19980601        NaN
7750   0.0 19980601        NaN
7751   0.0 19980601        NaN
7752   0.0 19980601        NaN

And I'm using the code 我正在使用代码

ggplot() + 
geom_line(data=na.omit(subset(datasim, Date == obsDate[t])),
          aes(y=Depth, x=Temp, size=1.5)) + 
geom_point(data=subset(dataobs, Date == obsDate[t]),
          aes(y=Depth, x=Temp, size=2)) +

However, it gives plot like this, but it should just be a clean line. 然而,它给出了这样的情节,但它应该只是一条干净的线。 Could anyone tell me how to get rid of these extra weird lines? 谁能告诉我如何摆脱这些额外的怪异行? I've tried sorting the arrays but it didn't help. 我尝试对数组进行排序,但没有帮助。 在此处输入图片说明

You need to change your x and y coordinates and then use coord_flip . 您需要更改xy坐标,然后使用coord_flip Also, you need to reverse the x axis with scale_x_reverse . 另外,您需要使用scale_x_reverse反转x轴。 Note that size is outside of aes . 注意size不在aes

ggplot() + 
  geom_line(data=na.omit(subset(datasim, Date == "19980601")),
            aes(x=Depth, y=Temp), size = 1) + 
  coord_flip() +
  scale_x_reverse()

在此处输入图片说明

Another option without resorting to coord_flip() : 不求助于coord_flip()另一种选择:

Data: 数据:

read.table(text="Depth     Date       Temp
0.0 19980601 -0.6577905
0.1 19980601 -0.1840248
0.2 19980601  0.5996339
0.3 19980601  3.3119625
0.4 19980601  3.9217309
0.5 19980601  4.0388018
0.6 19980601  4.0257338
0.7 19980601  3.9543796
0.8 19980601  3.8595199
0.9 19980601  3.7635552
1.0 19980601  3.6735954
1.1 19980601  3.5917693
1.2 19980601  3.5193204
1.3 19980601  3.4559904  
1.4 19980601  3.4009090
1.5 19980601  3.3530415
1.6 19980601  3.3113879
1.7 19980601  3.2750543
1.8 19980601  3.2432676
1.9 19980601  3.2153632
2.0 19980601  3.1907631
2.1 19980601  3.1689482
2.2 19980601  3.1494289
2.3 19980601  3.1317168
2.4 19980601  3.1153033
2.5 19980601  3.0996626
2.6 19980601  3.0843152
2.7 19980601  3.0690151
2.8 19980601  3.0541184
2.9 19980601  3.0410244
3.0 19980601  3.0322053
3.1 19980601  3.0302451
3.2 19980601  3.0363760
3.3 19980601  3.0500890
3.4 19980601  3.0698218
3.5 19980601  3.0937937
3.6 19980601  3.1204821
3.7 19980601  3.1487569
3.8 19980601  3.1778400
3.9 19980601  3.2072096
4.0 19980601  3.2365085
4.1 19980601  3.2654762
4.2 19980601  3.2939086
4.3 19980601  3.3216428
4.4 19980601  3.3485619
4.5 19980601  3.3746048
4.6 19980601  3.3998141
0.0 19980601        NaN
0.0 19980601        NaN
0.0 19980601        NaN
0.0 19980601        NaN", header=TRUE) -> xdf

Arrange the points and use geom_path() : 排列点并使用geom_path()

dplyr::arrange(xdf, Depth) %>% 
  ggplot(aes(Temp, Depth)) +
  geom_path() +
  scale_y_reverse()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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