mmyag
mmyag

Reputation: 43

Crazy lines using ggplot2 line plot

The dataframe 'datasim' I'm trying to plot is as below

     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. enter image description here

Upvotes: 1

Views: 181

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

Another option without resorting to 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():

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

enter image description here

Upvotes: 1

Dan
Dan

Reputation: 12074

You need to change your x and y coordinates and then use coord_flip. Also, you need to reverse the x axis with scale_x_reverse. Note that size is outside of aes.

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

enter image description here

Upvotes: 1

Related Questions