简体   繁体   English

R图中的多个Y轴

[英]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. 我有13个变量可以在一张具有不同y轴范围和x轴datetime时间的图形中绘制。

> 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 当我针对x轴绘制单个变量时,它像这样来

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 ? datetime的类型是什么? I would assume it's a factor or string. 我认为这是一个因素或字符串。 R will not draw lines with categorical x-axes. R不会绘制带有分类x轴的线。 You can use lubridate to transform the datetime values into something that plots more nicely: 您可以使用lubridatedatetime值转换为更美观的图形:

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

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

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