计算针对R的Date和POSIXct计算得出的美学差异

时间:2018-11-16 09:22:38

标签: r ggplot2 posixct

在处理DatePOSIXct对象时,我注意到一些非常奇怪的东西。请参见以下代码:

library(tidyverse)
library(Rmisc)

test <- structure(list(
  date = structure(c(
    16863, 16866, 16862, 16743,
    16741, 16819, 16820, 16969, 16896, 16636, 16855, 16715, 16842,
    16899, 16859, 16860, 16827, 16823, 16912, 16878, 16848, 16839,
    16901, 16833, 16896, 16841, 16735, 16800, 16781, 16903
  ), class = "Date"),
  group = structure(c(
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
    2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
    2L, 1L, 2L, 2L, 2L
  ), .Label = c("0", "1"), class = "factor")
), row.names = c(
  NA,
  -30L
), class = c("tbl_df", "tbl", "data.frame"))

test$posix <- as.POSIXct(test$date)

p1 <- ggplot(
  test, 
  aes(x = date, group = group, colour = group, fill = group)
) + 
  stat_density(aes(y = ..count..), alpha = 0.4)

p2 <- ggplot(
  test, 
  aes(x = posix, group = group, colour = group, fill = group)
) + 
  stat_density(aes(y = ..count..), alpha = 0.4)

multiplot(p1, p2)

这将导致以下绘图:请参见y轴。 (计数<1,因为样本量很小。)

enter image description here

当调用geom_density..count..时,为什么这两张图的比例会不同?调用..density..时相同。两种情节之间的唯一区别是使用xDate来呼吁POSIXct美学。我很困惑。

1 个答案:

答案 0 :(得分:0)

啊,这是一个非常愚蠢的问题。愚蠢的我。

> as.numeric(test$date)
 [1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903

同时

POSIXct

因此,正如@hrbrmstr所说,这只是单位问题。

我将保留此职位作为警告,以表明转换为scale_x_datetime类可能会带来意想不到的后果。我当时(不必要)尝试使用 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <system.data> <DbProviderFactories> <remove invariant="Oracle.ManagedDataAccess.Client"/> <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </DbProviderFactories> </system.data> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <publisherPolicy apply="no"/> <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/> <bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/> </dependentAssembly> </assemblyBinding> </runtime> <oracle.manageddataaccess.client> <version number="*"> <settings> <setting name="TNS_ADMIN" value="asd" /> </settings> </version> </oracle.manageddataaccess.client> </configuration>