ggplot似乎以不同的方式绘制日期对象

时间:2015-03-18 17:14:37

标签: r ggplot2

嗨我的数据如下。我试图在时间序列之上绘制离散数据。

df1 <- structure(list(sample_date = structure(c(1418233320, 1418233320, 
1418233320, 1418233320, 1418233320, 1418233320, 1418233320, 1418233320, 
1418233320, 1418233320, 1418233320, 1418233320, 1418233320, 1418233320, 
1418233320, 1418233320), tzone = "", class = c("POSIXct", "POSIXt"
)), start_depth = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 
9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5), chemical_name = c("Salinity (field)", 
"Salinity (field)", "Salinity (field)", "Salinity (field)", "Salinity (field)", 
"Salinity (field)", "Salinity (field)", "Salinity (field)", "Salinity (field)", 
"Salinity (field)", "Salinity (field)", "Salinity (field)", "Salinity (field)", 
"Salinity (field)", "Salinity (field)", "Salinity (field)"), 
    result_value = c(2.25, 2.16, 2.26, 3.41, 14.96, 19.17, 21.66, 
    22.18, 22.96, 23.16, 23.32, 23.54, 23.6, 23.66, 23.67, 23.67
    )), .Names = c("sample_date", "start_depth", "chemical_name", 
"result_value"), class = "data.frame", row.names = c(NA, -16L
))

df2 <- structure(list(datetime = structure(c(1418198455, 1418199355, 
1418200255, 1418201155, 1418202054, 1418202955, 1418203854, 1418204754, 
1418205655, 1418206555, 1418207454, 1418208354, 1418209255, 1418210155, 
1418211054, 1418211954, 1418212854, 1418213754, 1418214654, 1418215554, 
1418216455, 1418217355, 1418218254, 1418219154, 1418220054, 1418220954, 
1418221854, 1418222754, 1418223655, 1418224554, 1418225455, 1418226355, 
1418227255, 1418228155, 1418229054, 1418229954, 1418230854), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Depth = c(1.15, 1.23, 1.29, 1.32, 1.33, 1.39, 1.47, 
1.5, 1.49, 1.54, 1.6, 1.61, 1.67, 1.76, 1.78, 1.71, 1.68, 1.74, 
1.76, 1.62, 1.46, 1.41, 1.39, 1.32, 1.24, 1.18, 1.13, 1.07, 0.99, 
0.91, 0.86, 0.83, 0.75, 0.66, 0.58, 0.5, 0.44), Sal1 = c(5.97000000000001, 
9.61000000000001, 12.08, 12.81, 12.31, 12.74, 14.27, 13.42, 9.09000000000001, 
13.24, 12.11, 13.58, 12.89, 13.85, 14.56, 14.06, 13.56, 13.27, 
13.69, 14.81, 14.02, 12.49, 11.79, 12.31, 11.88, 10.7, 11.5, 
10.2, 9.77000000000001, 7.43000000000001, 6.81000000000001, 5.94000000000001, 
5.36000000000001, 3.75000000000001, 1.78000000000001, 1.36000000000001, 
1.27000000000001)), class = "data.frame", row.names = c(NA, -37L
), .Names = c("datetime", "Depth", "Sal1"))

绘制数据

ggplot(df2) + geom_line(aes(datetime, Sal1)) + 
  geom_point(aes(as.POSIXct(sample_date), as.numeric(result_value)), data = df1)

还使用了以下代码,但输出看起来类似:

ggplot(df2) + geom_line(aes(datetime, Sal1)) + 
  geom_point(aes(sample_date, result_value), data = df1)

输出如下:

enter image description here

这些点似乎没有正确排列。

但是,在excel上绘制时,数据似乎正确排列。它如下:

enter image description here

基于joran的评论 我更改了POSIXct对象的属性并使它们都是GMT但仍然没有排队。我做错什么了吗 ?

attr(df2$datetime, "tzone") <- "GMT"
attr(df1$sample_date, "tzone") <- "GMT"

ggplot(df2) + geom_line(aes(datetime, Sal1)) + 
  geom_point(aes(sample_date, result_value), data = df1)

0 个答案:

没有答案