I am trying to plot a figure that shows the relationship between x
and y
as well as the direction of the curve. The following are sample data saved as data.csv
:
date,x,y
2000-01-01,0.373,0
2000-01-02,0.370,0
2000-01-03,0.370,0
2000-01-04,0.370,0
2000-01-05,0.370,0
2000-01-06,0.370,0
2000-01-07,0.370,0
2000-01-08,0.370,0
2000-01-09,0.368,0
2000-01-10,0.365,0
2000-01-11,0.362,0
2000-01-12,0.360,0
2000-01-13,0.357,0
2000-01-14,0.355,0
2000-01-15,0.353,0
2000-01-16,0.351,0
2000-01-17,0.347,0
2000-01-18,0.344,0
2000-01-19,0.340,0
2000-01-20,0.338,0
2000-01-21,0.334,0
2000-01-22,0.331,0
2000-01-23,0.328,0
2000-01-24,0.324,0
2000-01-25,0.325,0.002
2000-01-26,0.323,0
2000-01-27,0.323,0
2000-01-28,0.321,0
2000-01-29,0.321,0
2000-01-30,0.327,0.009
2000-01-31,0.325,0
2000-02-01,0.325,0.001
2000-02-02,0.349,0.037
2000-02-03,0.350,0.039
2000-02-04,0.351,0.041
2000-02-05,0.353,0.043
2000-02-06,0.355,0.046
2000-02-07,0.363,0.059
2000-02-08,0.372,0.072
2000-02-09,0.377,0.079
2000-02-10,0.380,0.083
2000-02-11,0.381,0.086
2000-02-12,0.383,0.087
2000-02-13,0.384,0.089
2000-02-14,0.385,0.091
2000-02-15,0.386,0.092
2000-02-16,0.387,0.094
2000-02-17,0.388,0.095
2000-02-18,0.391,0.1
2000-02-19,0.393,0.102
2000-02-20,0.393,0.103
2000-02-21,0.394,0.104
2000-02-22,0.394,0.105
2000-02-23,0.395,0.105
2000-02-24,0.395,0.106
2000-02-25,0.395,0.106
2000-02-26,0.396,0.107
2000-02-27,0.396,0.107
2000-02-28,0.404,0.119
2000-02-29,0.405,0.12
2000-03-01,0.405,0.12
2000-03-02,0.405,0.121
2000-03-03,0.405,0.121
2000-03-04,0.405,0.121
2000-03-05,0.405,0.121
2000-03-06,0.406,0.122
2000-03-07,0.408,0.126
2000-03-08,0.413,0.133
2000-03-09,0.414,0.134
2000-03-10,0.416,0.138
2000-03-11,0.417,0.138
2000-03-12,0.417,0.138
2000-03-13,0.417,0.138
2000-03-14,0.417,0.138
2000-03-15,0.418,0.14
2000-03-16,0.422,0.147
2000-03-17,0.427,0.153
2000-03-18,0.431,0.159
2000-03-19,0.434,0.164
2000-03-20,0.439,0.172
2000-03-21,0.445,0.18
2000-03-22,0.445,0.18
2000-03-23,0.445,0.18
2000-03-24,0.445,0.18
2000-03-25,0.445,0.18
2000-03-26,0.445,0.18
2000-03-27,0.445,0.18
2000-03-28,0.445,0.18
2000-03-29,0.447,0.184
2000-03-30,0.448,0.185
2000-03-31,0.448,0.185
2000-04-01,0.448,0.185
2000-04-02,0.448,0.185
2000-04-03,0.449,0.185
2000-04-04,0.449,0.185
2000-04-05,0.449,0.185
2000-04-06,0.449,0.185
2000-04-07,0.449,0.186
2000-04-08,0.449,0.187
2000-04-09,0.450,0.188
2000-04-10,0.451,0.19
2000-04-11,0.452,0.191
2000-04-12,0.453,0.192
2000-04-13,0.454,0.193
2000-04-14,0.454,0.194
2000-04-15,0.455,0.195
2000-04-16,0.456,0.196
2000-04-17,0.456,0.197
2000-04-18,0.457,0.197
2000-04-19,0.457,0.198
2000-04-20,0.457,0.199
2000-04-21,0.458,0.199
2000-04-22,0.458,0.2
2000-04-23,0.458,0.2
2000-04-24,0.459,0.2
2000-04-25,0.459,0.201
2000-04-26,0.459,0.201
2000-04-27,0.459,0.201
2000-04-28,0.459,0.202
2000-04-29,0.460,0.202
2000-04-30,0.460,0.202
2000-05-01,0.460,0.202
2000-05-02,0.460,0.203
2000-05-03,0.461,0.203
2000-05-04,0.463,0.207
2000-05-05,0.463,0.207
2000-05-06,0.463,0.207
2000-05-07,0.464,0.208
2000-05-08,0.464,0.209
2000-05-09,0.464,0.209
2000-05-10,0.464,0.209
2000-05-11,0.464,0.209
2000-05-12,0.465,0.209
2000-05-13,0.465,0.209
2000-05-14,0.466,0.211
2000-05-15,0.469,0.215
2000-05-16,0.469,0.215
2000-05-17,0.470,0.217
2000-05-18,0.470,0.217
2000-05-19,0.470,0.217
2000-05-20,0.470,0.217
2000-05-21,0.470,0.217
2000-05-22,0.470,0.217
2000-05-23,0.470,0.217
2000-05-24,0.470,0.217
2000-05-25,0.470,0.218
2000-05-26,0.473,0.221
2000-05-27,0.474,0.223
2000-05-28,0.474,0.223
2000-05-29,0.474,0.223
2000-05-30,0.474,0.223
2000-05-31,0.474,0.223
2000-06-01,0.474,0.223
2000-06-02,0.474,0.223
2000-06-03,0.474,0.223
2000-06-04,0.474,0.223
2000-06-05,0.474,0.223
2000-06-06,0.474,0.223
2000-06-07,0.474,0.223
2000-06-08,0.474,0.224
2000-06-09,0.476,0.226
2000-06-10,0.476,0.226
2000-06-11,0.477,0.228
2000-06-12,0.479,0.23
2000-06-13,0.480,0.233
2000-06-14,0.482,0.236
2000-06-15,0.484,0.238
2000-06-16,0.486,0.241
2000-06-17,0.488,0.244
2000-06-18,0.490,0.247
2000-06-19,0.492,0.25
2000-06-20,0.494,0.253
2000-06-21,0.496,0.256
2000-06-22,0.498,0.259
2000-06-23,0.500,0.261
2000-06-24,0.501,0.263
2000-06-25,0.502,0.265
2000-06-26,0.504,0.267
2000-06-27,0.505,0.269
2000-06-28,0.507,0.272
2000-06-29,0.514,0.283
2000-06-30,0.517,0.287
2000-07-01,0.519,0.29
2000-07-02,0.521,0.293
2000-07-03,0.523,0.297
2000-07-04,0.539,0.321
2000-07-05,0.548,0.333
2000-07-06,0.556,0.346
2000-07-07,0.564,0.358
2000-07-08,0.574,0.372
2000-07-09,0.582,0.385
2000-07-10,0.594,0.403
2000-07-11,0.611,0.429
2000-07-12,0.624,0.448
2000-07-13,0.637,0.469
2000-07-14,0.653,0.492
2000-07-15,0.664,0.509
2000-07-16,0.673,0.524
2000-07-17,0.682,0.536
2000-07-18,0.689,0.547
2000-07-19,0.698,0.56
2000-07-20,0.720,0.595
2000-07-21,0.732,0.614
2000-07-22,0.742,0.628
2000-07-23,0.749,0.638
2000-07-24,0.754,0.646
2000-07-25,0.759,0.653
2000-07-26,0.764,0.661
2000-07-27,0.770,0.669
2000-07-28,0.773,0.674
2000-07-29,0.775,0.676
2000-07-30,0.777,0.679
2000-07-31,0.784,0.691
2000-08-01,0.787,0.695
2000-08-02,0.788,0.696
2000-08-03,0.787,0
2000-08-04,0.791,0.019
2000-08-05,0.801,0.069
2000-08-06,0.807,0.097
2000-08-07,0.811,0.118
2000-08-08,0.812,0.121
2000-08-09,0.812,0
2000-08-10,0.810,0
2000-08-11,0.806,0
2000-08-12,0.801,0
2000-08-13,0.794,0
2000-08-14,0.795,0.004
2000-08-15,0.791,0
2000-08-16,0.786,0
2000-08-17,0.782,0
2000-08-18,0.777,0
2000-08-19,0.774,0
2000-08-20,0.773,0
2000-08-21,0.771,0
2000-08-22,0.768,0
2000-08-23,0.765,0
2000-08-24,0.761,0
2000-08-25,0.756,0
2000-08-26,0.749,0
2000-08-27,0.744,0
2000-08-28,0.738,0
2000-08-29,0.735,0
2000-08-30,0.731,0
2000-08-31,0.727,0
2000-09-01,0.723,0
2000-09-02,0.718,0
2000-09-03,0.713,0
2000-09-04,0.707,0
2000-09-05,0.706,0
2000-09-06,0.704,0
2000-09-07,0.698,0
2000-09-08,0.691,0
2000-09-09,0.684,0
2000-09-10,0.676,0
2000-09-11,0.670,0
2000-09-12,0.665,0
2000-09-13,0.663,0
2000-09-14,0.664,0.002
2000-09-15,0.660,0
2000-09-16,0.660,0
2000-09-17,0.665,0.014
2000-09-18,0.661,0
2000-09-19,0.656,0
2000-09-20,0.650,0
2000-09-21,0.642,0
2000-09-22,0.634,0
2000-09-23,0.629,0
2000-09-24,0.632,0.008
2000-09-25,0.627,0
2000-09-26,0.623,0
2000-09-27,0.632,0.026
2000-09-28,0.625,0
2000-09-29,0.621,0
2000-09-30,0.618,0
2000-10-01,0.612,0
2000-10-02,0.606,0
2000-10-03,0.600,0
2000-10-04,0.594,0
2000-10-05,0.587,0
2000-10-06,0.580,0
2000-10-07,0.571,0
2000-10-08,0.563,0
2000-10-09,0.556,0
2000-10-10,0.549,0
2000-10-11,0.542,0
2000-10-12,0.534,0
2000-10-13,0.530,0
2000-10-14,0.522,0
2000-10-15,0.516,0
2000-10-16,0.509,0
2000-10-17,0.501,0
2000-10-18,0.498,0
2000-10-19,0.496,0
2000-10-20,0.488,0
2000-10-21,0.480,0
2000-10-22,0.474,0
2000-10-23,0.469,0
2000-10-24,0.468,0
2000-10-25,0.466,0
2000-10-26,0.459,0
2000-10-27,0.452,0
2000-10-28,0.444,0
2000-10-29,0.437,0
2000-10-30,0.430,0
2000-10-31,0.427,0
2000-11-01,0.419,0
2000-11-02,0.410,0
2000-11-03,0.401,0
2000-11-04,0.392,0
2000-11-05,0.383,0
2000-11-06,0.374,0
2000-11-07,0.367,0
2000-11-08,0.361,0
2000-11-09,0.355,0
2000-11-10,0.348,0
2000-11-11,0.342,0
2000-11-12,0.335,0
2000-11-13,0.327,0
2000-11-14,0.320,0
2000-11-15,0.313,0
2000-11-16,0.306,0
2000-11-17,0.301,0
2000-11-18,0.294,0
2000-11-19,0.287,0
2000-11-20,0.279,0
2000-11-21,0.271,0
2000-11-22,0.262,0
2000-11-23,0.252,0
2000-11-24,0.244,0
2000-11-25,0.237,0
2000-11-26,0.230,0
2000-11-27,0.222,0
2000-11-28,0.215,0
2000-11-29,0.209,0
2000-11-30,0.204,0
2000-12-01,0.197,0
2000-12-02,0.190,0
2000-12-03,0.183,0
2000-12-04,0.175,0
2000-12-05,0.168,0
2000-12-06,0.163,0
2000-12-07,0.159,0
2000-12-08,0.157,0
2000-12-09,0.156,0
2000-12-10,0.152,0
2000-12-11,0.149,0
2000-12-12,0.146,0
2000-12-13,0.143,0
2000-12-14,0.139,0
2000-12-15,0.135,0
2000-12-16,0.130,0
2000-12-17,0.125,0
2000-12-18,0.121,0
2000-12-19,0.117,0
2000-12-20,0.117,0
2000-12-21,0.114,0
2000-12-22,0.118,0.005
2000-12-23,0.117,0
2000-12-24,0.115,0
2000-12-25,0.111,0
2000-12-26,0.106,0
2000-12-27,0.100,0
2000-12-28,0.094,0
2000-12-29,0.087,0
2000-12-30,0.083,0
2000-12-31,0.079,0
I used the following code to plot the relationship between x
and y
with arrows to show the direction of the curve
library(ggplot2)
data <- read.csv('data.csv')
ggplot(data,aes(x = x,y = y)) +
geom_segment(aes(xend=c(tail(x, n=-1), NA), yend=c(tail(y, n=-1), NA)),size=0.5,
arrow=arrow(length=unit(0.1,"in"),type = 'open'))
The resulting figure looks like this:
The number of arrows is quite a lot, which makes it a bit hard to visualize, especially when more facets (more data points) are included. I need to reduce the number of arrows to make the curve a bit neat. I know that I can reduce the number of data points, but I need all the data points and just draw the arrow each
n
point.
Is there a neat way of doing this?
Here's a way to draw only every tenth arrow. The segment colour is mapped to a variable that is missing 9 times then black once, which then repeats for the length of x
. The geom_path
then adds the original line back over the top.
I did try using a subset of the data, but that moved the line near the corners.
Hopefully you can refine this to choose a set of arrows that looks nice.
ggplot(dat,aes(x = x,y = y)) +
scale_color_identity()+
geom_segment(aes(col=rep(c(rep(NA,9),"black"),100)[seq_along(x)],
xend=c(tail(x, n=-1), NA), yend=c(tail(y, n=-1), NA)),
size=0.5,
arrow=arrow(length=unit(0.1,"in"),type = 'open'))+
geom_path()