Count aesthetics differently calculated for R's Date and POSIXct












1















I noticed something very strange while working with Date and POSIXct objects. See the following code:



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)


This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)



enter image description here



Why would the scales differ on those two graphs when geom_density with ..count.. is called? Same when ..density.. is called. The only difference between the two plots is calling upon x aesthetics either with Date or with POSIXct. I'm quite puzzled.










share|improve this question




















  • 2





    You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

    – hrbrmstr
    Nov 16 '18 at 11:03
















1















I noticed something very strange while working with Date and POSIXct objects. See the following code:



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)


This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)



enter image description here



Why would the scales differ on those two graphs when geom_density with ..count.. is called? Same when ..density.. is called. The only difference between the two plots is calling upon x aesthetics either with Date or with POSIXct. I'm quite puzzled.










share|improve this question




















  • 2





    You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

    – hrbrmstr
    Nov 16 '18 at 11:03














1












1








1








I noticed something very strange while working with Date and POSIXct objects. See the following code:



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)


This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)



enter image description here



Why would the scales differ on those two graphs when geom_density with ..count.. is called? Same when ..density.. is called. The only difference between the two plots is calling upon x aesthetics either with Date or with POSIXct. I'm quite puzzled.










share|improve this question
















I noticed something very strange while working with Date and POSIXct objects. See the following code:



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)


This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)



enter image description here



Why would the scales differ on those two graphs when geom_density with ..count.. is called? Same when ..density.. is called. The only difference between the two plots is calling upon x aesthetics either with Date or with POSIXct. I'm quite puzzled.







r ggplot2 posixct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:54









Henrik

42.4k994110




42.4k994110










asked Nov 16 '18 at 9:22









KimKim

1,5001228




1,5001228








  • 2





    You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

    – hrbrmstr
    Nov 16 '18 at 11:03














  • 2





    You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

    – hrbrmstr
    Nov 16 '18 at 11:03








2




2





You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

– hrbrmstr
Nov 16 '18 at 11:03





You should look at output (and then str()s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date))) then ?density and also consider that the area under the curve is 1 but both width and height are important when considering said area.

– hrbrmstr
Nov 16 '18 at 11:03












1 Answer
1






active

oldest

votes


















0














Ah, it was a pretty silly question. Silly me.



> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200


while



> 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


So as @hrbrmstr said, it is just a matter of unit.



I'll leave this post to serve as a warning, to show that conversion to POSIXct class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime at the time.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334833%2fcount-aesthetics-differently-calculated-for-rs-date-and-posixct%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Ah, it was a pretty silly question. Silly me.



    > as.numeric(test$posix)
    [1] 1456963200 1457222400 1456876800 1446595200 1446422400
    [6] 1453161600 1453248000 1466121600 1459814400 1437350400
    [11] 1456272000 1444176000 1455148800 1460073600 1456617600
    [16] 1456704000 1453852800 1453507200 1461196800 1458259200
    [21] 1455667200 1454889600 1460246400 1454371200 1459814400
    [26] 1455062400 1445904000 1451520000 1449878400 1460419200


    while



    > 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


    So as @hrbrmstr said, it is just a matter of unit.



    I'll leave this post to serve as a warning, to show that conversion to POSIXct class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime at the time.






    share|improve this answer




























      0














      Ah, it was a pretty silly question. Silly me.



      > as.numeric(test$posix)
      [1] 1456963200 1457222400 1456876800 1446595200 1446422400
      [6] 1453161600 1453248000 1466121600 1459814400 1437350400
      [11] 1456272000 1444176000 1455148800 1460073600 1456617600
      [16] 1456704000 1453852800 1453507200 1461196800 1458259200
      [21] 1455667200 1454889600 1460246400 1454371200 1459814400
      [26] 1455062400 1445904000 1451520000 1449878400 1460419200


      while



      > 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


      So as @hrbrmstr said, it is just a matter of unit.



      I'll leave this post to serve as a warning, to show that conversion to POSIXct class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime at the time.






      share|improve this answer


























        0












        0








        0







        Ah, it was a pretty silly question. Silly me.



        > as.numeric(test$posix)
        [1] 1456963200 1457222400 1456876800 1446595200 1446422400
        [6] 1453161600 1453248000 1466121600 1459814400 1437350400
        [11] 1456272000 1444176000 1455148800 1460073600 1456617600
        [16] 1456704000 1453852800 1453507200 1461196800 1458259200
        [21] 1455667200 1454889600 1460246400 1454371200 1459814400
        [26] 1455062400 1445904000 1451520000 1449878400 1460419200


        while



        > 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


        So as @hrbrmstr said, it is just a matter of unit.



        I'll leave this post to serve as a warning, to show that conversion to POSIXct class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime at the time.






        share|improve this answer













        Ah, it was a pretty silly question. Silly me.



        > as.numeric(test$posix)
        [1] 1456963200 1457222400 1456876800 1446595200 1446422400
        [6] 1453161600 1453248000 1466121600 1459814400 1437350400
        [11] 1456272000 1444176000 1455148800 1460073600 1456617600
        [16] 1456704000 1453852800 1453507200 1461196800 1458259200
        [21] 1455667200 1454889600 1460246400 1454371200 1459814400
        [26] 1455062400 1445904000 1451520000 1449878400 1460419200


        while



        > 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


        So as @hrbrmstr said, it is just a matter of unit.



        I'll leave this post to serve as a warning, to show that conversion to POSIXct class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime at the time.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 21:29









        KimKim

        1,5001228




        1,5001228
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334833%2fcount-aesthetics-differently-calculated-for-rs-date-and-posixct%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values