简体   繁体   中英

Multiple Y-axis in a R plot

I am trying to plot a time series data using "line" type. but it is not coming out properly. I have thirteen variables to plot in a single graph with different y axis ranges and datetime in x axis.

> head(eg)
              datetime       x1       x2        x3        x4       x5        x6
24 2017-06-14 12:00:00 8446.755 7648.031 1111.6707 0.9045327 414.2691 11.778449
25 2017-06-14 13:00:00 6295.053 5660.273  882.6834 0.7028812 415.9553  8.754347
26 2017-06-14 14:00:00 6086.147 5498.443  833.2998 0.6438458 416.5267  8.459638
27 2017-06-14 15:00:00 1850.083 1644.708  254.0441 0.2337391 416.7503  2.569269
28 2017-06-14 16:00:00 5267.201 4773.258  768.3997 0.5249667 416.4483  7.320368
29 2017-06-14 17:00:00 5907.030 5302.488  752.4784 0.6931478 409.5094  8.348385
         x7       x8       x9      x10       x11       x12      x13
24 50.00656 57990800 51.76610 1169.014 0.4784138 0.5478276 1.193828
25 50.01681 57994800 51.92203 1199.004 0.4809830 0.6833898 1.174119
26 49.99918 57999183 53.23167 1194.763 0.4223333 0.6339667 1.166983
27 50.01026 58003118 53.97018 1068.251 0.3476316 0.2754211 1.260561
28 50.00910 58007020 49.94800 1188.888 0.4477200 0.5084000 1.171740
29 50.15805 58011111 46.86375 1144.759 0.3844375 0.5530125 1.131637

when i plot single variable against x axis, it is coming like this

plot(eg$x1, x = eg$datetime, type = "l")

图片1

May i know why it is not showing line curve?

Thanks.

What is the type of the column datetime ? I would assume it's a factor or string. R will not draw lines with categorical x-axes. You can use lubridate to transform the datetime values into something that plots more nicely:

df <- data.frame(datetime= c("2017-06-14 12:00:00", "2017-06-14 13:00:00", 
"2017-06-14 14:00:00", "2017-06-14 15:00:00", "2017-06-14 16:00:00",
"2017-06-14 17:00:00"), x1 = c(8446.755, 6295.053, 6086.147, 1850.083,
5267.201, 5907.030))

library(lubridate)

plot (df$datetime, df$x1, type="l") # does not work
df$date <- ymd_hms(df$datetime)
plot (df$date, df$x1, type="l") # works

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